exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
sequencelengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
ee42852f6bbf90a5fd82a842e3e25165
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class D{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); Set<Long> set = new HashSet<>(); ArrayList<Long> set1 = new ArrayList<>(); long num = (long)1e12+2; int c = 0; while (true) { long nu = (long)1<<c; if(nu>num) break; set.add(nu); c+=1; } long fact = (long)1; for(int i=2;i<=num;i++){ fact*=(long)i; if(fact>num) break; if(i==2) continue; set1.add(fact); } while (t-->0) { long n = fs.nextLong(); int ans = 100000; if(set.contains(n)){ ans = 1; } else{ int u = (int)Math.pow(2,13)-1; for(int i = 0;i<u;i++){ int cc = 0; long su = (long)0; for(int j = 0;j<12;j++){ int g = 1<<j; if((i&g)!=0){ su+=(long)set1.get(j); cc+=1; } } if(su==n){ ans = Math.min(ans,cc); } else if(n>su){ long dd = n-su; int ff = 0; while (dd>(long)0) { ff+=dd%(long)2; dd/=(long)2; } ans = Math.min(ans,cc+ff); } } } out.println(ans); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
54b1ddeef844f874a92cdf597fb4862f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.Scanner; public class C1646 { public static void main(String[] args) { long[] fact = new long[15]; fact[0] = 1; for (int i=1; i<fact.length; i++) { fact[i] = i*fact[i-1]; } int limit = 1 << fact.length; Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { long N = in.nextLong(); int min = Integer.MAX_VALUE; for (int mask=0; mask<limit; mask++) { long sum = 0; for (int bit=0; bit<fact.length; bit++) { if (((1 << bit) & mask) != 0) { sum += fact[bit]; } } if (sum <= N) { int cand = Integer.bitCount(mask) + Long.bitCount(N-sum); min = Math.min(min, cand); } } System.out.println(min); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
2c5424d8582b20c84d882964ba0bd5fd
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ long[] fact = new long[15]; fact[0] = 1; for(int i = 1; i <= 14; ++i){ fact[i] = fact[i - 1] * i; } long[] sum = new long[1 << 15]; for(int i = 0; i < 1 << 15; ++i){ long sm = 0; for(int j = i, lb = j & -j; lb != 0; j -= lb, lb = j & -j){ sm += fact[Integer.bitCount(lb - 1)]; } sum[i] = sm; } Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){ long n = sc.nextLong(); int ans = Long.bitCount(n); for(int mask = 0; mask < 1 << 15; ++mask){ long sm = sum[mask]; if(sm > n) continue; ans = Math.min(ans, Integer.bitCount(mask) + Long.bitCount(n - sm)); } System.out.println(ans); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7a1ba813f0b95faffa5e80a301b2f674
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class Main { public class MainSolution extends MainSolutionT { // global vars long[] F; public void init(int tests_count) { F = new long[12]; F[0] = 6; long j=4; for (int i=1; i<12; i++) { F[i] = F[i-1] * (j++); } } public int checkD(long n) { int r = 0; while (n>0) { if (n%2 == 1) { r ++; } n /= 2; } return r; } public class TestCase extends TestCaseT { public int mink(long n, int k) { if (n==0) { return 0; } if (k<0) { return checkD(n); } if (n < F[k]) { return mink(n, k-1); } return Math.min( mink(n, k-1) , 1+mink(n-F[k], k-1) ); } public Object solve() { long n =readLong(); return mink(n, 11); } } public void run() { int t = multiply_test ? readInt() : 1; this.init(t); for (int i = 0; i < t; i++) { TestCase T = new TestCase(); T.run(i + 1); } } public void loc_params() { this.log_enabled = false; } public void params(){ this.multiply_test = true; } } public class MainSolutionT extends MainSolutionBase { public class TestCaseT extends TestCaseBase { } } public class MainSolutionBase { public boolean is_local = false; public MainSolutionBase() { } public class TestCaseBase { public Object solve() { return null; } public int caseNumber; public TestCaseBase() { } public void run(int cn){ this.caseNumber = cn; Object r = this.solve(); if ((r != null)) { out.println(r); } } } public String impossible(){ return "IMPOSSIBLE"; } public String strf(String format, Object... args) { return String.format(format, args); } public BufferedReader in; public PrintStream out; public boolean log_enabled = false; public boolean multiply_test = true; public void params() { } public void loc_params() { } private StringTokenizer tokenizer = null; public int readInt() { return Integer.parseInt(readToken()); } public long readLong() { return Long.parseLong(readToken()); } public double readDouble() { return Double.parseDouble(readToken()); } public String readLn() { try { String s; while ((s = in.readLine()).length() == 0); return s; } catch (Exception e) { return ""; } } public String readToken() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (Exception e) { return ""; } } public int[] readIntArray(int n) { int[] x = new int[n]; readIntArray(x, n); return x; } public void readIntArray(int[] x, int n) { for (int i = 0; i < n; i++) { x[i] = readInt(); } } public long[] readLongArray(int n) { long[] x = new long[n]; readLongArray(x, n); return x; } public void readLongArray(long[] x, int n) { for (int i = 0; i < n; i++) { x[i] = readLong(); } } public void readLongArrayRev(long[] x, int n) { for (int i = 0; i < n; i++) { x[n-i-1] = readLong(); } } public void logWrite(String format, Object... args) { if (!log_enabled) { return; } out.printf(format, args); } public void readLongArrayBuf(long[] x, int n) { char[]buf = new char[1000000]; long r = -1; int k= 0, l = 0; long d; while (true) { try{ l = in.read(buf, 0, 1000000); } catch(Exception E){}; for (int i=0; i<l; i++) { if (('0'<=buf[i])&&(buf[i]<='9')) { if (r == -1) { r = 0; } d = buf[i] - '0'; r = 10 * r + d; } else { if (r != -1) { x[k++] = r; } r = -1; } } if (l<1000000) return; } } public void readIntArrayBuf(int[] x, int n) { char[]buf = new char[1000000]; int r = -1; int k= 0, l = 0; int d; while (true) { try{ l = in.read(buf, 0, 1000000); } catch(Exception E){}; for (int i=0; i<l; i++) { if (('0'<=buf[i])&&(buf[i]<='9')) { if (r == -1) { r = 0; } d = buf[i] - '0'; r = 10 * r + d; } else { if (r != -1) { x[k++] = r; } r = -1; } } if (l<1000000) return; } } public void printArray(long[] a, int n) { printArray(a, n, ' '); } public void printArray(int[] a, int n) { printArray(a, n, ' '); } public void printArray(long[] a, int n, char dl) { long x; int i, l = 0; for (i=0; i<n; i++) { x = a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { x = a[i]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printArray(double[] a, int n, char dl) { long x; double y; int i, l = 0; for (i=0; i<n; i++) { x = (long)a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1 + 10*n; char[] s = new char[l]; l--; boolean z; int j; for (i=n-1; i>=0; i--) { x = (long)a[i]; y = (long)(1000000000*(a[i]-x)); z = false; if (x<0) { x = -x; z = true; } for (j=0; j<9; j++) { s[l--] = (char)('0' + (y % 10)); y /= 10; } s[l--] = '.'; do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printArray(int[] a, int n, char dl) { int x; int i, l = 0; for (i=0; i<n; i++) { x = a[i]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { x = a[i]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (i>0) { s[l--] = dl; } } out.println(new String(s)); } public void printMatrix(int[][] a, int n, int m) { int x; int i,j, l = 0; for (i=0; i<n; i++) { for (j=0; j<m; j++) { x = a[i][j]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += m-1; } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { for (j=m-1; j>=0; j--) { x = a[i][j]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (j>0) { s[l--] = ' '; } } if (i>0) { s[l--] = '\n'; } } out.println(new String(s)); } public void printMatrix(long[][] a, int n, int m) { long x; int i,j, l = 0; for (i=0; i<n; i++) { for (j=0; j<m; j++) { x = a[i][j]; if (x<0) { x = -x; l++; } if (x==0) { l++; } else { while (x>0) { x /= 10; l++; } } } l += m-1; } l += n-1; char[] s = new char[l]; l--; boolean z; for (i=n-1; i>=0; i--) { for (j=m-1; j>=0; j--) { x = a[i][j]; z = false; if (x<0) { x = -x; z = true; } do{ s[l--] = (char)('0' + (x % 10)); x /= 10; } while (x>0); if (z) { s[l--] = '-'; } if (j>0) { s[l--] = ' '; } } if (i>0) { s[l--] = '\n'; } } out.println(new String(s)); } } public void run() { MainSolution S; try { S = new MainSolution(); S.in = new BufferedReader(new InputStreamReader(System.in)); //S.out = System.out; S.out = new PrintStream(new BufferedOutputStream( System.out )); } catch (Exception e) { return; } S.params(); S.run(); S.out.flush(); } public static void main(String args[]) { Locale.setDefault(Locale.US); Main M = new Main(); M.run(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
e365cc90c04a3f0affd1bd7bfe8ee1fd
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static PrintWriter pw; static Scanner sc; static final int inf = (int) 1e9, mod = inf + 7; static final long longInf = inf * 1l * inf; static final double eps = 1e-9; public static void main(String[] args) throws Exception { sc = new Scanner(System.in); pw = new PrintWriter(System.out); preCalc(); int t = sc.nextInt(); while (t-- > 0) testCase(); pw.close(); } static void testCase() throws Exception { long n = sc.nextLong(); int ans = 100; for (int msk = 0; msk < (1 << factorial.size()); msk++) { long currVal = n; for (int i = 0; i < factorial.size(); i++) { if(((1 << i) & msk) != 0) currVal -= factorial.get(i); } if(currVal >= 0) ans = Math.min(ans, countBits(msk) + countBits(currVal)); } pw.println(ans); } static int countBits(long x){ if(x == 0) return 0; return (int) (x & 1) + countBits(x >> 1); } static ArrayList<Long> factorial; static void preCalc(){ factorial = new ArrayList<>(); factorial.add(1l); for (int i = 2; true; i++) { long prev = factorial.get(factorial.size() - 1); long currFact = prev * i; if(currFact > 1e12) break; factorial.add(currFact); } } static long ceildiv(long x, long y) { return (x + y - 1) / y; } static int mod(long x, int m) { return (int) ((x % m + m) % m); } static void add(Map<Integer, Integer> map, Integer p) { if (map.containsKey(p)) map.replace(p, map.get(p) + 1); else map.put(p, 1); } static void rem(Map<Integer, Integer> map, Integer p) { if (map.get(p) == 1) map.remove(p); else map.replace(p, map.get(p) - 1); } static int Int(boolean x) { return x ? 1 : 0; } public static long gcd(long x, long y) { return y == 0 ? x : gcd(y, x % y); } static void printArr(int[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); } static void printArr(long[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); } static void printArr(double[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); } static void printArr(boolean[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] ? 1 : 0); pw.println(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextDigits() throws IOException { String s = nextLine(); int[] arr = new int[s.length()]; for (int i = 0; i < arr.length; i++) arr[i] = s.charAt(i) - '0'; return arr; } public int[] nextArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextInt(); return arr; } public Integer[] nextSort(int n) throws IOException { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public int[] nextArrMinus1(int n) throws IOException{ int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextInt() - 1; return arr; } public Pair nextPair() throws IOException { return new Pair(nextInt(), nextInt()); } public long[] nextLongArr(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public Pair[] nextPairArr(int n) throws IOException { Pair[] arr = new Pair[n]; for (int i = 0; i < n; i++) arr[i] = nextPair(); return arr; } public boolean hasNext() throws IOException { return (st != null && st.hasMoreTokens()) || br.ready(); } } static void printArr(Integer[] arr) { for (int i = 0; i < arr.length; i++) pw.print(arr[i] + " "); pw.println(); } static void printIter(Iterable list) { for (Object o : list) pw.print(o + " "); pw.println(); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public Pair(Map.Entry<Integer, Integer> a) { x = a.getKey(); y = a.getValue(); } public int hashCode() { return (int) ((this.x * 1l * 100003 + this.y) % mod); } public int compareTo(Pair p) { if (x != p.x) return x - p.x; return y - p.y; } public boolean equals(Object obj) { if (obj == null) { return false; } if (this.getClass() != obj.getClass()) { return false; } Pair p = (Pair) obj; return this.x == p.x && this.y == p.y; } public Pair clone() { return new Pair(x, y); } public String toString() { return this.x + " " + this.y; } public void subtract(Pair p) { x -= p.x; y -= p.y; } public void add(Pair p) { x += p.x; y += p.y; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
a0c94aa303e4605086d829a09d1d8c9d
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public final class Main { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = i(); while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { List<Long> list = new ArrayList<>(); long cur = 1; for (int i = 1; i < 20; i++) { cur *= i; if (cur > 1e12) { break; } list.add(cur); } long n = l(); int size = list.size(); int max = 1 << size; int ans = 100; for (int i = 0; i < max; i++) { long m = n; int res = 0; for (int j = 0; j < size; j++) { int k = 1 << j; if ((i & k) > 0) { res++; m -= list.get(j); } } if (m >= 0) { res += Long.bitCount(m); ans = Math.min(ans, res); } } out.println(ans); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s() { return in.nextLine(); } static String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int tmp = arr[i]; arr[arr.length - 1 - i] = tmp; arr[i] = arr[arr.length - 1 - i]; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
cd7fcc4460b3da8b9276bd776a479c4c
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static long[] arr; static TreeSet<String> tSet; static PrintWriter pw; public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static long fac(long i) { long res = 1; while (i > 0) { res = res * i--; } return res; } public static long combination(long x, long y) { return 1l * (fac(x) / (fac(x - y) * fac(y))); } public static long permutation(long x, long y) { return combination(x, y) * fac(y); } public static long sum(Long[] arr, long item) { long res = 0; for (int i = 0; i < arr.length; i++) { res += arr[i]; } res -= item; return res; } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { long target=sc.nextLong(); arr=new long[15]; for (int i = 1; i<=14; i++) { arr[i]=fac(i); } pw.println(count(target, 0, 0, 0)); pw.flush(); } } public static long count(long target,long acc,int c,int index) { if(index==15) { if(acc<=target) return c+Long.bitCount(target-acc); return 90000000; } long take=count(target,acc+arr[index],c+1,index+1); long leave=count(target,acc,c,index+1); return Math.min(take, leave); } static class Pair { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextlongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public Long[] nextLongArray(int n) throws IOException { Long[] array = new Long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public char[] nextCharArray(int n) throws IOException { char[] array = new char[n]; String string = next(); for (int i = 0; i < n; i++) { array[i] = string.charAt(i); } return array; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
6c5c4e076e529e155dddf878014f6ff2
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; public class c { public static void main(String[] args) { // fact[i] stores (i+3)! long[] fact = new long[12]; fact[0] = 6; for (int i=1; i<12; i++) fact[i] = (i+3)*fact[i-1]; long[] sums = new long[1<<12]; for (int i=1; i<(1<<12); i++) { int j = 0; while ((i & (1<<j)) == 0) j++; sums[i] = sums[i-(1<<j)] + fact[j]; } Scanner stdin = new Scanner(System.in); int nC = stdin.nextInt(); for (int loop=0; loop<nC; loop++) { long inp = stdin.nextLong(); int res = Long.bitCount(inp); for (int i=0; i<sums.length; i++) { if (inp < sums[i]) continue; res = Math.min(res, Integer.bitCount(i) + Long.bitCount(inp-sums[i])); } System.out.println(res); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
774f2ed531ba067df4e63cca5fb61d6c
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ArrayList<Long> factorials ; public static long factorial(int x) { if(x==2) return 2; return x*factorial(x-1); } public static void main(String[] args) throws Exception { int t = sc.nextInt(); while(t-->0) { long n = sc.nextLong(); factorials = new ArrayList<>(); factorials.add((long) 1); for(int i=2;i<15;i++) { factorials.add(factorial(i)); } int ans = (int)1e9; for(int i=0;i<1<<factorials.size();i++) { long x = 0; int ctr=0; for(int j=0;j<factorials.size();j++) { if((i&(1<<j))!=0) { x+=factorials.get(j); ctr++; } } ans = Math.min(ans, ctr+Long.bitCount((n-x))); } pw.println(ans); } pw.flush(); } static class SegmentTree { int[] arr, sTree, lazy; int N; public SegmentTree(int[] in) { arr = in; N = in.length - 1; sTree = new int[2 * N]; lazy = new int[2 * N]; build(1, 1, N); } public void build(int Node, int left, int right) { // O(n) if (left == right) sTree[Node] = arr[left]; else { int r = 2 * Node + 1; int l = 2 * Node; int mid = (left + right) / 2; build(l, left, mid); build(r, mid + 1, right); sTree[Node] = sTree[l] + sTree[r]; } } public int query(int i, int j) { return query(1, 1, N, i, j); } public int query(int Node, int left, int right, int i, int j) { if (right < i || left > j) return 0; if (right <= j && i <= left) { return sTree[Node]; } int mid = (left + right) / 2; int l = 2 * Node; int r = 2 * Node + 1; return query(l, left, mid, i, j) + query(r, mid + 1, right, i, j); } public void updatePoint(int idx, int value) { int node = idx + N - 1; arr[idx] = value; sTree[node] = value; while (node > 1) { node /= 2; int l = 2 * node; int r = 2 * node + 1; sTree[node] = sTree[l] + sTree[r]; } } public void updateRange(int i, int j, int val) { updateRange(1, 1, N, i, j, val); } public void updateRange(int Node, int left, int right, int i, int j, int val) { if (i > right || left > j) return; if (right <= j && i <= left) { sTree[Node] += val * (right - left + 1); lazy[Node] += val; } else { int l = 2 * Node; int r = 2 * Node + 1; int mid = (left + right) / 2; propagate(Node, left, right); updateRange(l, left, mid, i, j, val); updateRange(r, mid + 1, right, mid, j, val); sTree[Node] = sTree[l] + sTree[r]; } } public void propagate(int node, int left, int right) { int l = 2 * node; int r = 2 * node + 1; int mid = (left + right) / 2; lazy[l] = lazy[node]; lazy[r] = lazy[node]; sTree[l] += lazy[node] * (mid - left + 1); sTree[r] += lazy[node] * (right - mid); lazy[node] = 0; } } public static void sort(long[] in) { shuffle(in); Arrays.sort(in); } public static void shuffle(long[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); long tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return x - o.x; } public String toString() { return x + " " + y; } } static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } public static void display(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]); } System.out.println(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
a83384d4a172ce0746c033682ff4468b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
/* I am dead inside Do you like NCT, sKz, BTS? 5 4 3 2 1 Moonwalk Imma knock it down like domino Is this what you want? Is this what you want? Let's ttalkbocky about that */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class x1646C { public static void main(String hi[]) throws Exception { long[] fac = new long[15]; fac[1] = 1L; for(int v=2; v < 15; v++) fac[v] = fac[v-1]*v; long[] sums = new long[1<<14]; int[] bc = new int[1<<14]; for(int mask=0; mask < (1<<14); mask++) { for(int b=0; b < 14; b++) if((mask&(1<<b)) > 0) sums[mask] += fac[b+1]; bc[mask] = Integer.bitCount(mask); } BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); long N = Long.parseLong(st.nextToken()); int res = Long.bitCount(N); for(int mask=0; mask < (1<<14); mask++) if(sums[mask] <= N) { long temp = N-sums[mask]; if((mask&2) > 0 && (temp&2) > 0) continue; res = min(res, bc[mask]+Long.bitCount(temp)); } sb.append(res+"\n"); } System.out.print(sb); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
377bf443f6eca1362a375040c9851bcf
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class C { static StringBuilder sb; static long mod = (long) (1e9 + 7); static long[] fact; static int min = (int) 1e9; static int find(long n) { String bin = Long.toBinaryString(n); int cnt = 0; for (int i = 0; i < bin.length(); i++) { if (bin.charAt(i) == '1') cnt++; } return cnt; } static void solve(long tar, int idx, int cnt) { if (idx == fact.length) { min = Math.min(min,cnt + find(tar)); return; } if (tar >= fact[idx]) solve(tar - fact[idx], idx + 1, cnt + 1); solve(tar, idx + 1, cnt); } static void solve(long n) { min = (int) 1e9; solve(n, 0, 0); sb.append(min + "\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); fact = new long[15]; fact[1] = 1; for (int i = 2; i < 15; i++) { fact[i] = i * fact[i - 1]; } sortlong(fact); while (test-- > 0) { solve(l()); } out.printLine(sb); out.flush(); out.close(); } private static double log(long a, long b) { double ans = Math.log10(a) / Math.log10(b); return ans; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sortlong(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static int[] sortint(int[] a2) { int n = a2.length; ArrayList<Integer> l = new ArrayList<>(); for (int i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readLongArray(int n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
48fc20e99da49a3f534b2881e10aeff9
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int result = 50; public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static long pow(int number, long i) { if (i == 0) return 1; long sum = pow(number, i / 2); sum *= sum; if ((i & 1) == 1) sum *= number; return sum; } public static long fac(long i) { if (i == 0) return 1; return i * fac(i - 1); } public static long compliment(int number, int bits) { return number ^ (pow(2, bits) - 1); } public static void helper(long[] arr,int i,int c,long n,long sum){ if(i==arr.length){ if(sum<=n){ c+= Long.bitCount(n - sum); result = Math.min(result, c); } return; } helper(arr,i+1,c+1,n,sum+arr[i]); helper(arr,i+1,c,n,sum); } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); long[] arr = new long[15]; for(int i =0;i<15;i++){ arr[i] = fac(i+1); } while(t-->0){ result = 50; long n = sc.nextLong(); helper(arr,0,0,n,0); pw.println(result); } pw.flush(); } static class Pair { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public String toString() { return a + " " + b; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextlongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public Long[] nextLongArray(int n) throws IOException { Long[] array = new Long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public char[] nextCharArray(int n) throws IOException { char[] array = new char[n]; String string = next(); for (int i = 0; i < n; i++) { array[i] = string.charAt(i); } return array; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
27803c6f333b8fcfb5d32e62b61b3056
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } static long[] facts; static { facts = new long[16]; facts[1] = 1; for(int i = 2; i < 16; i++) { facts[i] = i*facts[i-1]; } } //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int[] vis = new int[16]; recurse(0, vis); int t = sc.ni();while(t-->0) solve(); w.close(); } static void recurse(int at, int[] taken) { if(at == taken.length) { long sum = 0; long freq = 0; for(int i = 0; i < taken.length; i++) { if(taken[i] == 1) { freq++; sum += facts[i]; } } combs.add(new pr(sum, freq)); return; } taken[at] = 1; recurse(at+1, taken); taken[at] = 0; recurse(at+1, taken); } static List<pr> combs = new ArrayList<>(); static void solve() throws IOException { long sum = sc.nl(); long min = ima; for(pr i: combs) { if(i.val > sum) continue; long curr = i.freq; long rem = sum-i.val; char[] strr = Long.toBinaryString(rem).toCharArray(); for(char ch: strr) if(ch=='1') curr++; min = Math.min(curr, min); } w.p(min); } static class pr { long val, freq; public pr(long val, long freq) { this.val = val; this.freq = freq; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
9bcc983e6aad801022f3035b263c7109
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class Main { static AReader scan = new AReader(); static int MOD = (int)1e9+7; static int N = 2000010; static List<Long> p = new ArrayList<>(); static void init(){ long sum = 1; for(int i = 1;i<=15;i++){ sum *=i; p.add(sum); } } static int get(long x){ int cnt = 0; while(x > 0){ cnt++; x -= x & -x; } return cnt; } static void slove() { long n = scan.nextLong(); Collections.sort(p); int m = 1 << p.size(); int res = 0x3f3f3f3f; for(int i = 0;i<m;i++){ long s = 0; for(int j = 0;j<p.size();j++){ if((i >> j & 1) == 1){ s +=p.get(j); } } if(s > n ) continue; res = Math.min(res,get(i)+get(n-s)); } System.out.println(res); } public static void main(String[] args) { init(); int T = scan.nextInt(); while (T-- > 0) { slove(); } } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
3c65469b2d02acab4eabcfbccd6fde4c
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
// package faltu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Map.Entry; public class Main { static long LowerBound(long[] a2, long x) { // x is the target value or key int l=-1,r=a2.length; while(l+1<r) { int m=(l+r)>>>1; if(a2[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static long getClosest(long val1, long val2,long target) { if (target - val1 >= val2 - target) return val2; else return val1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } public static int findIndex(long arr[], long t) { if (arr == null) { return -1; } int len = arr.length; int i = 0; while (i < len) { if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { int temp = a[left]; a[left] = a[right]; a[right] = temp; return a; } public static void swap(long x,long max1) { long temp=x; x=max1; max1=temp; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } return a; } static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) { int n =A.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(A.get(mid) > B) { second = mid; }else { first = mid+1; } } if(first < n && A.get(first) < B) { first++; } return first; //1 index } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // *******----segement tree implement---***** // -------------START-------------------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode) { if(start==end) { tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value) { if(start==end) { arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid) { updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); } else { updateTree(arr,tree,start,mid,2*treeNode,idx,value); } tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } // disjoint set implementation --start static void makeSet(int n) { parent=new int[n]; rank=new int[n]; for(int i=0;i<n;i++) { parent[i]=i; rank[i]=0; } } static void union(int u,int v) { u=findpar(u); v=findpar(v); if(rank[u]<rank[v])parent[u]=v; else if(rank[v]<rank[u])parent[v]=u; else { parent[v]=u; rank[u]++; } } private static int findpar(int node) { if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } static int parent[]; static int rank[]; // *************end static Long MOD=(long) (1e9+7); static boolean coprime(int a, long l){ return (gcd(a, l) == 1); } static long[][] dp; static long[]dpp; static ArrayList<Integer>arr; static boolean[] vis; static ArrayList<ArrayList<Integer>>adj; public static void main(String[] args) throws IOException { // sieve(); FastReader s = new FastReader(); ArrayList<Long>a=new ArrayList<>(); long val=1; a.add(val); for(int i=2;i<=14;i++) { val*=i; a.add(val); } long tt = s.nextLong(); while(tt-->0) { long n=s.nextLong(); int ans=Integer.MAX_VALUE; for(int i=0;i<(1<<14);i++) { long sum=n; for(int j=0;j<14;j++) { if((i&(1<<j))>0)sum-=a.get(j); } if(sum<0)continue; ans=Math.min(ans, Long.bitCount(i)+Long.bitCount(sum)); } System.out.println(ans); } } static void DFSUtil(int v, boolean[] visited) { visited[v] = true; Iterator<Integer> it = adj.get(v).iterator(); while (it.hasNext()) { int n = it.next(); if (!visited[n]) DFSUtil(n, visited); } } static long DFS(int n) { boolean[] visited = new boolean[n+1]; long cnt=0; for (int i = 1; i <= n; i++) { if (!visited[i]) { DFSUtil(i, visited); cnt++; } } return cnt; } public static String revStr(String str){ String input = str; StringBuilder input1 = new StringBuilder(); input1.append(input); input1.reverse(); return input1.toString(); } public static String sortString(String inputString){ char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } static long myPow(long n, long i){ if(i==0) return 1; if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD; return (n%MOD* myPow(n,i-i)%MOD)%MOD; } static void palindromeSubStrs(String str) { HashSet<String>set=new HashSet<>(); char[]a =str.toCharArray(); int n=str.length(); int[][]dp=new int[n][n]; for(int g=0;g<n;g++){ for(int i=0,j=g;j<n;j++,i++){ if(!set.contains(str.substring(i,i+1))&&g==0) { dp[i][j]=1; set.add(str.substring(i,i+1)); } else { if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) { dp[i][j]=1; set.add(str.substring(i,j+1)); } } } } int ans=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(dp[i][j]+" "); if(dp[i][j]==1)ans++; } System.out.println(); } System.out.println(ans); } static boolean isPalindrome(String str,int i,int j) { while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean sign(long num) { return num>0; } static boolean isSquare(long x){ if(x==1)return true; long y=(long) Math.sqrt(x); return y*y==x; } static long power1(long a,long b) { if(b == 0){ return 1; } long ans = power(a,b/2); ans *= ans; if(b % 2!=0){ ans *= a; } return ans; } static void swap(StringBuilder sb,int l,int r) { char temp = sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,temp); } // function to reverse the string between index l and r static void reverse(StringBuilder sb,int l,int r) { while(l < r) { swap(sb,l,r); l++; r--; } } // function to search a character lying between index l and r // which is closest greater (just greater) than val // and return it's index static int binarySearch(StringBuilder sb,int l,int r,char val) { int index = -1; while (l <= r) { int mid = (l+r)/2; if (sb.charAt(mid) <= val) { r = mid - 1; } else { l = mid + 1; if (index == -1 || sb.charAt(index) >= sb.charAt(mid)) index = mid; } } return index; } // this function generates next permutation (if there exists any such permutation) from the given string // and returns True // Else returns false static boolean nextPermutation(StringBuilder sb) { int len = sb.length(); int i = len-2; while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1)) i--; if (i < 0) return false; else { int index = binarySearch(sb,i+1,len-1,sb.charAt(i)); swap(sb,i,index); reverse(sb,i+1,len-1); return true; } } private static int lps(int m ,int n,String s1,String s2,int[][]mat) { for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1]; else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]); } } return mat[m][n]; } static String lcs(String X, String Y, int m, int n) { int[][] L = new int[m+1][n+1]; // Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] for (int i=0; i<=m; i++) { for (int j=0; j<=n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (X.charAt(i-1) == Y.charAt(j-1)) L[i][j] = L[i-1][j-1] + 1; else L[i][j] = Math.max(L[i-1][j], L[i][j-1]); } } // Following code is used to print LCS int index = L[m][n]; int temp = index; // Create a character array to store the lcs string char[] lcs = new char[index+1]; lcs[index] = '\u0000'; // Set the terminating character // Start from the right-most-bottom-most corner and // one by one store characters in lcs[] int i = m; int j = n; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (X.charAt(i-1) == Y.charAt(j-1)) { // Put current character in result lcs[index-1] = X.charAt(i-1); // reduce values of i, j and index i--; j--; index--; } // If not same, then find the larger of two and // go in the direction of larger value else if (L[i-1][j] > L[i][j-1]) i--; else j--; } return String.valueOf(lcs); // Print the lcs // System.out.print("LCS of "+X+" and "+Y+" is "); // for(int k=0;k<=temp;k++) // System.out.print(lcs[k]); } static long lis(long[] aa2, int n) { long lis[] = new long[n]; int i, j; long max = 0; for (i = 0; i < n; i++) lis[i] = 1; for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1) lis[i] = lis[j] + 1; for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } static boolean issafe(int i, int j, int r,int c, char ch) { if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false; else return true; } static long power(long a, long b) { a %=MOD; long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static long[] sieve; public static void sieve() { int nnn=(int) 1e6+1; long nn=(int) 1e6; sieve=new long[(int) nnn]; int[] freq=new int[(int) nnn]; sieve[0]=0; sieve[1]=1; for(int i=2;i<=nn;i++) { sieve[i]=1; freq[i]=1; } for(int i=2;i*i<=nn;i++) { if(sieve[i]==1) { for(int j=i*i;j<=nn;j+=i) { if(sieve[j]==1) { sieve[j]=0; } } } } } } class decrease implements Comparator<Long> { // Used for sorting in ascending order of // roll number public int compare(long a, long b) { return (int) (b - a); } @Override public int compare(Long o1, Long o2) { // TODO Auto-generated method stub return (int) (o2-o1); } } class pair{ long x; long y; long c; public pair(long x,long y) { this.x=x; this.y=y; } public pair(long x,long y,long c) { this.x=x; this.y=y; this.c=c; } } class Pair { int idx; String str; public Pair(String str,int idx) { this.str=str; this.idx=idx; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
2ace1b176f5e67c469746bd1c95493e4
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); List<Long> list = new ArrayList<>(); long fact = 6, num = 4; while(fact <= (long)1e12) { list.add(fact); fact *= num; num++; } int T = in.nextInt(); while(T-- > 0) { long n = in.nextLong(); int ans = -1; for(int mask = 0; mask<(1<<list.size()); mask++) { long sum = n; for(int i=0; i < list.size(); i++) { if((mask&(1<<i))!=0) sum -= list.get(i); } if(sum<0) continue; if(ans==-1) ans = Long.bitCount(sum)+Long.bitCount(mask); else ans = Math.min(ans, Long.bitCount(sum)+Long.bitCount(mask)); } out.println(ans); } out.close(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
c5bd0266c578020d8dbb6c2e72623373
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class main1{ static long[] inv=new long[16]; static void init(){ inv[0]=inv[1]=1; for(int i=1;i<=15;i++){ inv[i]=inv[i-1]*i; } } public static void main(String[] args){ Scanner scn=new Scanner(System.in); init(); int t=scn.nextInt(); while(t-->0){ solve(scn); } } static void solve(Scanner sc){ long n=sc.nextLong(); int ans=Integer.MAX_VALUE; for(int mask=0;mask<(1<<14);mask++){ long m=n; for(int i=0;i<15;i++){ if(((mask>>i)&1)==1){ m-=inv[i+1]; } } if(m>=0){ ans=Math.min(ans,Long.bitCount(mask)+Long.bitCount(m)); } } System.out.println(ans); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
173d36e0b5204284bee1e1a4be2b18fa
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f; public static int mod = 1000000007; public static long MAX = (long)1e12; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); FastScanner sc = new FastScanner(); boolean multiTest = true; List<Long> factLi = new ArrayList<>(); long fact = 6; int number = 4; while(fact <= MAX){ factLi.add(fact); fact *= number; number++; } int size = 1 << factLi.size(); List<long[]> li = new ArrayList<>(size); li.add(new long[]{0l, 0l}); for(int mask = 1;mask < size;mask++){ int firstBit = get_first_bit(mask); long fst = li.get(mask ^ (1 << firstBit))[0] + factLi.get(firstBit); long snd = countOne(mask); li.add(new long[]{fst, snd}); } if(multiTest) { int t = sc.nextInt(), loop = 0; while (loop < t) { loop++; solve(o, sc, li); } } else solve(o, sc, li); o.close(); } catch (Exception e){ e.printStackTrace(); } } static void solve(PrintWriter o, FastScanner sc, List<long[]> li){ try { long n = sc.nextLong(); long res = countOne(n); for(long[] a: li){ if(a[0] <= n){ res = Math.min(res, countOne(n - a[0]) + a[1]); } } o.println(res); } catch (Exception e){ e.printStackTrace(); } } public static int get_first_bit(long x){ for(int i=63;i>=0;i--){ if((x & 1l << i) > 0){ return i; } } return -1; } public static int countOne(long x){ int count = 0; for(int i=0;i<64;i++){ if((x & 1l << i) > 0){ count++; } } return count; } public static int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static boolean doubleEqual(double d1, double d2){ if(Math.abs(d1-d2) < 1e-6){ return true; } return false; } public static long fac(int n){ long ret = 1; while(n > 0){ ret = ret * n % mod; n--; } return ret; } public static long qpow(int n, int m){ long n_ = n, ret = 1; while(m > 0){ if((m&1) == 1){ ret = ret * n_ % mod; } m >>= 1; n_ = n_ * n_ % mod; } return ret; } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){ tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static int nextInt() throws IOException{ return Integer.parseInt(next()); } public static Long nextLong() throws IOException{ return Long.parseLong(next()); } public static double nextDouble() throws IOException{ return Double.parseDouble(next()); } public static String nextLine() throws IOException{ return reader.readLine(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
0fa942aa9317761fa589fe9081c81667
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import com.sun.jmx.remote.internal.ArrayQueue; import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = in.nextInt(); ArrayList<Long> list = new ArrayList<>(); long prod = 1, value = 1; while(prod <= 1_000_000_000_000L) { prod = prod*(value++); list.add(prod); } int size = list.size(); // System.out.println(size+" "+list); OUTER: while (T-->0) { Long n = in.nextLong(); int ans = findBit(n); for(int i=0; i<(1<<size); i++) { long sum = 0; int count = 0; for(int j=0; j<size; j++) if((i&(1<<j)) != 0){ sum += list.get(j); count += 1; } if(sum<=n) { count += findBit(n - sum); ans = Math.min(ans, count); } } out.append(ans).append("\n"); } System.out.print(out); } private static int findBit(long n) { int count = 0; while(n!=0) { if(n%2==1) { count += 1; } n/=2; } return count; } private static long gcd(long a, long b) { if (a==0) return b; return gcd(b%a, a); } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } private static void print(String s) { System.out.print(s); } private static void println(String s) { System.out.println(s); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
23babaf6d6c20888e57c4df6b9376b6d
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class C1646 { // public static void main(String[] args) throws IOException { long[] fac = new long[15]; long[] sub = new long[1 << 15]; int[] cnt = new int[sub.length]; fac[0] = 1; for (int i = 1; i < 15; i++) fac[i] = fac[i - 1] * (i + 1); for (int i = 1; i < sub.length; i++) { for (int j = 0; j < fac.length; j++) { sub[i] += ((i >> j) & 1) * fac[j]; cnt[i] += (i >> j) & 1; } } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int testCnt = Integer.parseInt(br.readLine()); for (int t = 0; t < testCnt; t++) { long n = Long.parseLong(br.readLine()); int min = popCnt(n); for (int i = 1; i < sub.length && sub[i] <= n; i++) { min = Math.min(min, popCnt(n - sub[i]) + cnt[i]); } dbug(""); bw.write(min + "\n"); } br.close(); bw.close(); } public static int popCnt(long l) { if (l < 0) return 20; long a = l; int ans = 0; while (l > 0) { ans += l & 1; l >>= 1; } dbug("<> <> ", a, ans); return ans; } public static void dbug(String s, long ... a) { /* String[] line = s.split("<>"); for (int i = 0; i < a.length; i++) { System.out.print(line[i] + a[i]); } System.out.println(line[a.length]); // */ } public static void print(long[] a) { // /* for (int i = 0; i < a.length; i++) { System.out.println(a[i] + " "); } System.out.println(); // */ } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
409ee77cc9257eafe22f03adec458368
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
// package CODECHEF; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class pracitce { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- >0){ long n=sc.nextLong(); long []a =new long[14]; long fac=1; for(int i=1;i<15;i++){ fac=fac*i*1l; a[i-1]=fac; } long res=14,bitcnt=0; long total=(1<<res); long ans=-1,sum=0,cnt=0; for(long i=0;i<total;i++){ sum=cnt=0; for(int j=0;j<res;j++){ if((i&(1<<j))!=0){ sum+=a[j]*1l; cnt++; } } if(sum>n)continue; else{ bitcnt=Long.bitCount((n-sum)); } if(ans==-1)ans=bitcnt+cnt; else ans=Math.min(ans,bitcnt+cnt); } System.out.println(ans); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
1c79ef9ca7e7a717ff6394e0ffa5a511
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CFactorialsAndPowersOfTwo solver = new CFactorialsAndPowersOfTwo(); solver.solve(1, in, out); out.close(); } static class CFactorialsAndPowersOfTwo { public void solve(int testNumber, FastReader in, PrintWriter out) { int t = in.nextInt(); ArrayList<Long> nums = new ArrayList<>(); long val = 1; long num = 2; while (val <= (long) 1e12) { nums.add(val); val *= num; num++; } while (t-- > 0) { long x = in.nextLong(); int ans = Integer.MAX_VALUE; for (int i = 0; i < (1L << 14); ++i) { long sum = x; for (int j = 0; j < 14; ++j) { if ((i & (1L << j)) > 0) { sum -= nums.get(j); } } if (sum < 0) { continue; } ans = Math.min(ans, Long.bitCount(sum) + Long.bitCount(i)); } out.println(ans); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
4391eb1498c7ec08ebbcf01b883e31ae
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CFactorialsAndPowersOfTwo solver = new CFactorialsAndPowersOfTwo(); solver.solve(1, in, out); out.close(); } static class CFactorialsAndPowersOfTwo { public void solve(int testNumber, FastReader in, PrintWriter out) { int t = in.nextInt(); ArrayList<Long> nums = new ArrayList<>(); long val = 1; long num = 2; while (val <= (long) 1e12) { nums.add(val); val *= num; num++; } while (t-- > 0) { long x = in.nextLong(); int ans = Integer.MAX_VALUE; for (int i = 0; i < (1L << 14); ++i) { long sum = x; for (int j = 0; j < 14; ++j) { if ((i & (1L << j)) > 0) { sum -= nums.get(j); } } if (sum < 0) { continue; } ans = Math.min(ans, Long.bitCount(sum) + Long.bitCount(i)); } out.println(ans); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
5778c6b9f9571d80594259bded480617
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class MyClass { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static long[] fact = new long[16]; static void init() { fact[0] = 1; for(int i=1; i<16; i++) fact[i] = (i*fact[i-1]); } public static void main (String[] args) throws java.lang.Exception { init(); int t=in.nextInt(); while(t-->0) solve(); } static void solve() { long n = in.nextLong(); long ans = Integer.MAX_VALUE; for(int mask=0; mask<(1<<15); mask++) { long sum = n; for(int i=0; i<16; i++) { if(((mask>>i)&1)==1) { sum -= fact[i+1]; } } if(sum<0) break; if(sum>=0) { ans = Math.min(ans, cntSetBit(mask)+cntSetBit(sum)); } } System.out.println(ans); } static long cntSetBit(long num) { long ans = 0; while(num>0) { if(num%2==1) ans++; num /= 2; } return ans; } static int max(int a, int b) { if(a<b) return b; return a; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static < E > void print(E res) { System.out.println(res); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
991107ca314b71acae1a951782ad21fd
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.io.BufferedReader; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Problem_C { private final static Long INF = 1000_000_000_005L; public static void main(String[] dummy) { MyScanner ms = new MyScanner(); PrintStream out = new PrintStream(System.out); ArrayList<Long> fact = new ArrayList<Long>(); Long x = 1l; for (int i = 1; x < INF; i++) { x *= i; fact.add(x); } int testcases = ms.nextInt(); while (testcases-- > 0) { Long N = ms.nextLong(); int ans = Long.bitCount(N); for (int mask = 0; mask < (1 << fact.size()); mask++) { Long curr = N; for (int i = 0; i < fact.size(); i++) if ((mask & (1 << i)) != 0) curr -= fact.get(i); if (curr >= 0) ans = min(ans, Integer.bitCount(mask) + Long.bitCount(curr)); } out.println(ans); } out.flush(); out.close(); } public static void reverseSort(int[] A) { // TODO : Collections.sort always uses Merge sort ArrayList<Integer> sorted = new ArrayList<>(); for (int x : A) sorted.add(x); Collections.sort(sorted, (x, y) -> Integer.compare(y, x)); for (int i = 0; i < A.length; i++) A[i] = sorted.get(i); } static class MyScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
69ce29628194a300ba57a4bb73447dd7
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class power { static class Node { int count = 0; Map<Character, Node> child = new HashMap<>(); } static BufferedReader bf; public static void main(String[] args) throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); int T = nextInt(); while(T-->0){ long n = Long.parseLong(bf.readLine()); long fact[] = new long[14]; fact[0] = 1; for(int i = 1;i<fact.length;i++){ fact[i] = (i+1)*fact[i-1]; } dfs(fact,0,0,0,n); System.out.println(min); min = Integer.MAX_VALUE; } } static int min = Integer.MAX_VALUE; public static void dfs(long[]fact,long sum,int i,int count,long n){ if(sum>n)return ; if(i == fact.length){ min = Math.min(min,counter(count, n, sum)); return; } dfs(fact,sum+fact[i],i+1,count+1,n); dfs(fact,sum,i+1,count,n); } public static int counter(int count,long n,long sum){ long val = n - sum; int bits = 0; while(val!=0){ bits += (val & 1); val = (val>>1); } return bits+count; } // code for input public static int nextInt() throws IOException { return Integer.parseInt(bf.readLine()); } public static long nextLong() throws IOException { return Long.parseLong(bf.readLine()); } public static String nextString() throws IOException { return bf.readLine(); } public static int[] nextIntArray(int n) throws IOException { String[] str = bf.readLine().split(" "); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } public static long[] nextLongArray(int n) throws IOException { String[] str = bf.readLine().split(" "); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r, int c) throws IOException { int[][] arr = new int[r][c]; for (int i = 0; i < r; i++) { String[] str = bf.readLine().split(" "); for (int j = 0; j < c; j++) { arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r, int c) throws IOException { long[][] arr = new long[r][c]; for (int i = 0; i < r; i++) { String[] str = bf.readLine().split(" "); for (int j = 0; j < c; j++) { arr[i][j] = Long.parseLong(str[j]); } } return arr; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
74b5601fe0b0e9314573f1cc0156c69f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class ferrisWheel { static PrintWriter pw; static Scanner sc; public static void main(String[] args) throws IOException, InterruptedException { pw = new PrintWriter(System.out); sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { long n=sc.nextLong(); ArrayList<Long> fact=new ArrayList<>(); fact.add(1l); for(int i=2;fact.get(i-2)<=n;i++) { fact.add(fact.get(i-2)*i); } int ans=(int)1e9; int msk=1<<fact.size(); for(int i=0;i<msk;i++) { long x=0; int cnt=0; for(int j=0;j<fact.size();j++) { if((i&(1<<j))!=0) { x+=fact.get(j); cnt++; } } if(x<=n) { ans=Math.min(ans, cnt+Long.bitCount(n-x)); } } pw.println(ans); } pw.flush(); } static class pair implements Comparable<pair> { // long x,y; int x, y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
41198023f3fa4d353f266ed732d0e1fb
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; public class cf { static long e97=1000000007; static HashMap<Long,Integer> h1=new HashMap(); static HashSet<Long> h2=new HashSet<>(); public static void main(String args[]) { long[] factor=new long[12]; Long[] res=new Long[4096]; long temp1=2; int t=0; for (int i = 3; i < 15; i++) { temp1*=i; h2.add(temp1); } for (Long a : h2) factor[t++]=a; Arrays.sort(factor); dfs(factor,0,0,0); Scanner in = new Scanner(System.in); int times=in.nextInt(); for (int i = 0; i < times; i++) { long tar=in.nextLong(); int k=9999; for (Map.Entry<Long, Integer> entry : h1.entrySet()){ long re=entry.getKey(); int time=entry.getValue(); if(k==1) break; int temp=0; String s=Long.toBinaryString(tar-re); for (char c : s.toCharArray()) { if(c=='1') temp++; } k=min(k,temp+time); } System.out.println(k); } } public static void dfs(long[] a, int i,long l,int nums){ h1.put(l+a[i],nums+1); h1.put(l,nums); if(i==a.length-1) return; dfs(a,i+1,l,nums); dfs(a,i+1,l+a[i],nums+1); } /*public static void main(String args[]) { cfReader in = new cfReader(System.in); int times=in.nextInt(); StringBuilder sb=new StringBuilder(); l:for (int i = 0; i < times; i++) { int n=in.nextInt(),k=n/2+1; int[] arr=new int[n]; for (int j = 0; j < arr.length; j++) arr[j]=in.nextInt(); Arrays.sort(arr); long[] first=new long[n]; first[0]=arr[0]; for (int j = 1; j < n; j++) { first[j]=first[j-1]+arr[j]; } for (int j = 0; j <= n/2; j++) { if(first[j+1]<first[n-1]-first[n-2-j]&&j+1<n-1-j) { sb.append("YES\n"); continue l; } } sb.append("NO\n"); } System.out.println(sb); }*/ } class cfPack { //含01、完全、多重背包,混合背包可并入多重,二维待定 int[] weight, value, multiple; int nums; public cfPack(int nums) { weight = new int[nums]; value = new int[nums]; } public cfPack(int[] w, int[] v) { weight = w; value = v; nums = weight.length; } public cfPack(int[] w, int[] v, int[] m) { weight = w; value = v; multiple = m; nums = weight.length; } public int ZeroOnePack(int volume) { //dp[i] = Math.max(dp[i-weight[k]]+value[k], dp[i]); //dp【i】为容积使用i时的最大价值,考虑前k种物品的部分由循环实现 //确保没有物品放入多次,i从大到小————由没有选择k物品的状态转移而来 int[] dp = new int[volume + 8],hash=new int[9003]; for (int k = 0; k < nums; k++) { for (int i = volume; i > 0; i--) { hash[max(0,dp[i])]=1; if(i>=weight[k]) hash[max(0,dp[i - weight[k]] + value[k])]=1; if (i >= weight[k]) dp[i] = max(dp[i - weight[k]] + value[k], dp[i]); } } for (int i =1; i < hash.length; i++) { if(hash[i]==0) {System.out.println(i);break;} } return dp[volume]; } public int CompletePack(int volume) { //与01的差别为i从小到大————每个状态可以由已经选中了k物品的状态转移而来 int[] dp = new int[volume + 1]; for (int k = 0; k < nums; k++) { for (int i = weight[k]; i < volume + 1; i++) { if (i >= weight[k]) dp[i] = max(dp[i - weight[k]] + value[k], dp[i]); } } return dp[volume]; } public int MultiplePack(int volume) { //多重背包,将每种物品可选的数量二进制化:用1/2/4/8……拼出,化为O(lgn)个新的物品 ArrayList<Integer> w = new ArrayList<>(); ArrayList<Integer> v = new ArrayList<>(); for (int i = 0; i < multiple.length; i++) { int k = multiple[i]&-multiple[i]; while (true) { if (k <= multiple[i]) { w.add(i, k * weight[i]); v.add(i, k * value[i]); multiple[i] -= k; k *= 2; } else { k = multiple[i]; if(k!=0) { w.add(i, k * weight[i]); v.add(i, k * value[i]); } break; } } } int[] w1 = new int[w.size()], v1 = new int[v.size()]; for (int i = 0; i < w1.length; i++) { w1[i] = w.get(i); v1[i] = v.get(i); } weight = w1; value = v1; nums = weight.length; return ZeroOnePack(volume); } } class cfReader{ private final static int BUF_SZ = 65536; BufferedReader in; StringTokenizer tokenizer; public cfReader(InputStream in) { super(); this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ); tokenizer = new StringTokenizer(""); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
485bd7660008d8ec60b2d7bf23548d8a
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//import java.io.IOException; import java.io.*; import java.util.*; public class FactorialsandPowersofTwo { static InputReader inputReader=new InputReader(System.in); static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static List<LinkedHashSet<Integer>>answer=new ArrayList<>(); static void solve() throws IOException { long n=inputReader.nextLong(); TreeSet<Long>twoPowerSet=new TreeSet<>(); // long counter=4; long fact=6; List<Long>factorials=new ArrayList<>(); for (int i=4;true;i++) { if (fact>n) { break; } factorials.add(fact); fact=fact*i; } long ans=Long.bitCount(n); for (int i=0;i<(1<<factorials.size());i++) { long sum=n; for (int j=0;j<factorials.size();j++) { if ((i&(1<<j))>0) { sum-= factorials.get(j); } } ans=Math.min(ans,Long.bitCount(i)+Long.bitCount(sum)); } out.println(ans); } static PrintWriter out=new PrintWriter((System.out)); static void SortDec(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } static void Sort(long arr[]) { List<Long>list=new ArrayList<>(); for(long ele:arr) { list.add(ele); } Collections.sort(list); for (int i=0;i<list.size();i++) { arr[i]=list.get(i); } } public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while (t-->0) { solve(); } long s = System.currentTimeMillis(); // out.println(System.currentTimeMillis()-s+"ms"); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
82cb4a4ada283b598dab514f28245fc7
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Round_774_Div2 { public static int MOD = 998244353; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); long[] pre = new long[16]; pre[0] = 1; for (int i = 1; i < pre.length; i++) { pre[i] = pre[i - 1] * i; } mainLoop: for (int z = 0; z < T; z++) { long n = in.nextLong(); int total = (1 << pre.length); int re = Long.bitCount(n); for (int i = 0; i < total; i++) { long cur = 0; for (int j = 0; j < pre.length; j++) { if (((1 << j) & i) == 0) { continue; } cur += pre[j]; } if (cur > n) { continue; } //System.out.println(cur); int tmp = Integer.bitCount(i) + Long.bitCount(n - cur); re = Integer.min(tmp, re); } out.println(re); } out.close(); } static class Move { int x, y, z; Move(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val); } else { return (val * ((val * a))); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
8bf7c7b6759d6c894add19f17c7138c8
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long [] fact = new long[17]; static long fact(int n) { if(fact[n]!=0) return fact[n]; if(n == 0 || n == 1) return 1; return n*fact(n-1); } public static void main(String [] args) { FastReader sc = new FastReader(); int test = sc.nextInt(); for(int i = 0; i < 17; i++) { fact[i] = fact(i); } while(test-- > 0) { long n = sc.nextLong(); long answer = Long.MAX_VALUE; for(long i = 0; i < (1 << 13); i++) { long temp = i, num = n, k = 0; int count = 3; while(temp != 0) { if((temp&1) == 1) { num-=fact[count]; if(num < 0) { num+=fact[count]; break; } k++; } count++; temp = temp >> 1; } while(num!=0) { if((num & 1) == 1) k++; num = num >> 1; } if(k < answer) answer = k; } System.out.println(answer); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
8efe2f5036b2a9a5f8aee7d464ffe78f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces774{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static ArrayList<Long> ar = new ArrayList<>(); static void solve() { long n = sc.nextLong(); long s = sc.nextLong(); long count = 0; count = s/(n*n); out.println(count); } static void fill(){ ar.add(1l); long j = 1; for(long i = 1;i<=14;i++){ ar.add(j*i); j*=i; } } static void solve2(){ int n = sc.nextInt(); int arr[] = sc.readIntArray(n); sort(arr); long pre = arr[0]; long suf = 0; int i = 1; int j = n-1; while(i<j){ pre += arr[i]; suf += arr[j]; if(pre<suf){ out.println("YES"); return; } i++; j--; } out.println("NO"); } static void solve3(){ long n = sc.nextLong(); long ans = Integer.MAX_VALUE; for(long i = 0;i<=(int)Math.pow(2,15);i++){ long count = 0; long sum = 0; for(int j = 0;j<15;j++){ if((i&(1<<j))!=0){ sum+= ar.get(j); count++; } } if(sum>n) continue; long temp = n-sum; while(temp>0){ temp = temp&(temp-1); count++; } ans = Math.min(ans,count); } out.println(ans); } static String fun(int n){ String str = ""; for(int i = 0;i<n;i++) str+= '0'; return str; } static String fun2(int n){ String str = ""; for(int i = 0;i<n;i++) str+= '1'; return str; } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } static class Pair implements Comparable<Pair>{ int val; int ind; int ans; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return p.val - this.val; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); fill(); // int t= 1; while(t-- >0){ // solve(); // solve2(); solve3(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
0d86023312950f192c16529f247eeb54
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.Scanner; public class Main { static long[] inv = new long[16]; static void init() { inv[0] = inv[1] = 1; for(int i = 1; i <= 15; i++) { inv[i] = inv[i - 1] * i; } } public static void main(String[] args) { init(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { solve(sc); } } static void solve(Scanner sc) { long n = sc.nextLong(); int ans = Integer.MAX_VALUE; for(int mask = 0; mask < (1 << 14); mask++) { long m = n; // 计算阶乘的和 for(int i = 0; i < 15; i++) { if(((mask >> i) & 1) == 1) { m -= inv[i + 1]; } } if(m >= 0) { ans = Math.min(ans, Long.bitCount(mask) + Long.bitCount(m)); } } System.out.println(ans); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
25cfef9b05b27f1cf94b09831e2b56b0
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class C_Factorials_and_Powers_of_Two { static int M = 1_000_000_007; static final PrintWriter out =new PrintWriter(System.out); static final FastReader fs = new FastReader(); static boolean prime[]; static long ans=Integer.MAX_VALUE; static ArrayList<Long> al; public static void main (String[] args) throws java.lang.Exception { int t = fs.nextInt(); al= new ArrayList<Long>(); al.add((long)6); for(int j=4;j<=14;j++){ al.add(al.get(j-4)*(long)j); } for(int j=0;j<t;j++){ ans=Integer.MAX_VALUE; long n=fs.nextLong(); int k=0; for(int jj=al.size()-1;jj>=0;jj--){ if(al.get(jj)<=n){ k=jj; break; } } rec(n,0,k); out.println(ans); } out.flush(); } static void rec(long n,long f,int j){ if(n<0) return; ans=Math.min(countSetBits(n)+f,ans); if(j<0) return; rec(n,f,j-1); rec(n-al.get(j),f+1,j-1); } static long countSetBits(long n) { long count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } public static long power(long x, long y) { long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return modMult(temp,temp); else { if (y > 0) return modMult(x,modMult(temp,temp)); else return (modMult(temp,temp)) / x; } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; if(1<=n) prime[1]=false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] arrayIn(int n) throws IOException { int arr[] = new int[n]; for(int i=0; i<n; i++) { arr[i] = nextInt(); } return arr; } } public static class Pairs implements Comparable<Pairs> { int value,index; Pairs(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pairs p) { return Integer.compare(this.value, p.value); } } static final Random random = new Random(); static void ruffleSort(int arr[]) { int n = arr.length; for(int i=0; i<n; i++) { int j = random.nextInt(n),temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static long nCk(int n, int k) { return (modMult(fact(n),fastexp(modMult(fact(n-k),fact(k)),M-2))); } static long fact (long n) { long fact =1; for(int i=1; i<=n; i++) { fact = modMult(fact,i); } return fact%M; } static long modMult(long a,long b) { return a*b%M; } static long fastexp(long x, int y){ if(y==1) return x; long ans = fastexp(x,y/2); if(y%2 == 0) return modMult(ans,ans); else return modMult(ans,modMult(ans,x)); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7782747b634d4bb1a82532da4d85b6ab
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine()); while (t-- > 0) { long n = Long.parseLong(in.readLine()); long ans = countFactAndPowOf2(n); out.println(ans); } in.close(); out.close(); } private static long countFactAndPowOf2(long n) { int finalAns = noOfSetBits(n); for (int mask=0; mask<=65536; mask++) { long sum = 0; for (int j=0; j<16 ; j++) { if (( (mask >> j) & 1) == 1) sum += factorial(j+1); if (sum > n) break; } if (sum > n) continue; int ans1 = noOfSetBits(n-sum) + noOfSetBits(mask); finalAns = Math.min(ans1, finalAns); } return finalAns; } private static long factorial(int i) { if (i <= 1) return 1; return (long)i*factorial(i-1); } private static int noOfSetBits(long x) { int count = 0; while (x > 0) { if ((x & 1) == 1) count++; x >>= 1; } return count; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
29c7be95c543bbb9cbf8a301e2546617
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long startTime = System.currentTimeMillis(); // for global initializations and methods starts here static long[] dp; static long inf = (long) 1e12 + 5; static void pre() { dp = new long[32]; dp[0] = 1L; for (int i = 1; i < 32; i++) { dp[i] = dp[i - 1] * i; if (dp[i] > inf) break; } } // global initialisations and methods end here static void run() { boolean tc = true; AdityaFastIO r = new AdityaFastIO(); //FastReader r = new FastReader(); try (OutputStream out = new BufferedOutputStream(System.out)) { //long startTime = System.currentTimeMillis(); int testcases = tc ? r.ni() : 1; int tcCounter = 1; // Hold Here Sparky------------------->>> // Solution Starts Here pre(); start: while (testcases-- > 0) { long n = r.nl(); long count = 500; long sum = 0; while (sum < (1L << 16)) { long res = 0L; long ans1 = 0; { int i = 0; while (i < 16) { if ((sum & (1L << i)) != 0) { res += dp[i]; ans1++; } i++; } } if (res <= n) { long ans2 = 0; long get = n - res; int i = 0; while (i < 60) { if ((get & (1L << i)) != 0) { ans2++; } i++; } count = Math.min(count, ans2 + ans1); } sum++; } out.write((count + " ").getBytes()); out.write(("\n").getBytes()); } // Solution Ends Here } catch (IOException e) { e.printStackTrace(); } } static class AdityaFastIO { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public BufferedReader br; public StringTokenizer st; public AdityaFastIO() { br = new BufferedReader(new InputStreamReader(System.in)); din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public AdityaFastIO(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public String readLine() throws IOException { byte[] buf = new byte[100000001]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nl() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nd() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws Exception { run(); } static int[] readIntArr(int n, AdityaFastIO r) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = r.ni(); return arr; } static long[] readLongArr(int n, AdityaFastIO r) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = r.nl(); return arr; } static List<Integer> readIntList(int n, AdityaFastIO r) throws IOException { List<Integer> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.ni()); return al; } static List<Long> readLongList(int n, AdityaFastIO r) throws IOException { List<Long> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(r.nl()); return al; } static long mod = 998244353; static long modInv(long base, long e) { long result = 1; base %= mod; while (e > 0) { if ((e & 1) > 0) result = result * base % mod; base = base * base % mod; e >>= 1; } return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String word() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String line() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int ni() { return Integer.parseInt(word()); } long nl() { return Long.parseLong(word()); } double nd() { return Double.parseDouble(word()); } } static int MOD = (int) (1e9 + 7); static long powerLL(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = result * x % MOD; n = n / 2; x = x * x % MOD; } return result; } static long powerStrings(int i1, int i2) { String sa = String.valueOf(i1); String sb = String.valueOf(i2); long a = 0, b = 0; for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD; for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1); return powerLL(a, b); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long lower_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } static int upper_bound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) { graph.get(edge1).add(edge2); graph.get(edge2).add(edge1); } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair o) { // TODO Auto-generated method stub if (this.first != o.first) return (int) (this.first - o.first); else return (int) (this.second - o.second); } } public static class PairC<X, Y> implements Comparable<PairC> { X first; Y second; public PairC(X first, Y second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(PairC o) { // TODO Auto-generated method stub return o.compareTo((PairC) first); } } static boolean isCollectionsSorted(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false; return true; } static boolean isCollectionsSortedReverseOrder(List<Long> list) { if (list.size() == 0 || list.size() == 1) return true; for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false; return true; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
f8c5f0086112f97e80ccf3fca2c4568b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args)throws Exception { Main ob=new Main(); ob.fun(); } long two[]; long fac[]; int min; HashSet<Long> hs; public void fun()throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); two=new long[43]; fac=new long[17]; long x=1l; for(int i=0;i<=42;i++) { two[i]=x; x=x*2l; } x=1l; fac[0]=1l; for(long i=1l;i<=16l;i++) { x*=i; fac[(int)i]=x; } int t=Integer.parseInt(br.readLine()); while(t-->0) { long n=Long.parseLong(br.readLine()); min=Integer.MAX_VALUE; hs=new HashSet<Long>(); recur(n,0,hs); pw.println(min); } pw.flush(); } public void recur(long n,int ct,HashSet<Long> hs) { if(n==0) { min=Math.min(min,ct); return; } if(ct>min) { return; } long x=floor(two,n); if(!hs.contains(x)) { hs.add(x); recur(n-x,ct+1,hs); hs.remove(x); } x=floor(fac,n); if(!hs.contains(x)) { hs.add(x); recur(n-x,ct+1,hs); hs.remove(x); } } public long floor(long ar[],long x) { int l=0; int r=ar.length-1; if(ar[0]>x) { return -1; } int m=(l+r)/2; while(l<=r) { m=(l+r)/2; if(ar[m]==x) { break; } else if(ar[m]<x) { l=m+1; } else { r=m-1; } } if(ar[m]==x) { return x; } else { return ar[r]; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 8
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
c85977e1e2593eb059ffa8d949397d32
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { prepare(); int T = sc.nextInt(); for(int i = 0; i < T; i++)solve(); //solve(); pw.flush(); } static ArrayList<Long> fact = new ArrayList<>(); public static void prepare(){ long now = 6; long max = (long)1e12; for(long i = 4; i <= 100000; i++){ fact.add(now); now *= i; if(now > (long)max){ break; } } } static HashMap<Long,Long> map; public static void solve() { long N = sc.nextLong(); map = new HashMap<>(); dfs(N,new HashSet<>()); //pw.println(map); pw.println(map.get(N)); } public static long dfs(long N, HashSet<Long> set){ //pw.println(N); if(map.containsKey(N)){ return map.get(N); } if(set.size() >= 30){ return Long.bitCount(N); } long ret = Long.bitCount(N); for(long v : fact){ if(N < v) break; if(set.contains(v)){ continue; } set.add(v); ret = Math.min(dfs(N - v, set)+1,ret); set.remove(v); } map.put(N,ret); return ret; } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * 処理は[s, t) の半開区間で指定すること * 例えばtree[0]の値を取得したい場合はfind(0,1)で取得できる。 * 初期値を設定したい場合は、for(int i = 0; i < N; i++) st.update(i,i+1,A[i]);のようにadd()メソッドを経由しないと正常に動かない */ class LazySegmentTree{ long[] tree,lazy; boolean[] bool; int N; long INF = (long)1e18; public LazySegmentTree(int n){ int now = 1; while(now < n){ now *= 2; } this.N = now; this.tree = new long[N*2]; Arrays.fill(this.tree,-INF); this.lazy = new long[N*2]; this.bool = new boolean[N*2]; } public void eval(int k, int l, int r){ if(bool[k]){ tree[k] = lazy[k]; if(r-l > 1){ lazy[k*2+1] = lazy[k*2+2] = lazy[k]; bool[k*2+1] = bool[k*2+2] = true; } bool[k] = false; } } public void update(int a, int b, long x){ update(a,b,x,0,0,N); } public void update(int a, int b, long x, int k, int l, int r){ if(r < 0) r = N; eval(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { lazy[k] = x; bool[k] = true; eval(k, l, r); } else { update(a, b, x, 2*k+1, l, (l+r)/2); update(a, b, x, 2*k+2, (l+r)/2, r); tree[k] = Math.max(tree[2*k+1], tree[2*k+2]); } return; } public long find(int a, int b){ return find(a,b,0,0,N); } public long find(int a, int b, int k, int l, int r) { if(r < 0) r = N; eval(k, l, r); if(b <= l || r <= a) return -INF; if(a <= l && r <= b) return tree[k]; long vl = find(a, b, 2*k+1, l, (l+r)/2); long vr = find(a, b, 2*k+2, (l+r)/2, r); return Math.max(vl, vr); } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
f3140a9167b312347798f0ce262c0b2b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static HashMap<Long,Integer> dp2; static long []pow; static int solve2(int bit,long n){ HashSet<Long> hs=new HashSet<>(); int ans=0; for(int i=0;i<=14;i++){ int mask=(1<<i); if((mask&bit)>0){ n-=fact[i]; if(hs.contains(fact[i])){ return Integer.MAX_VALUE; } hs.add(fact[i]); } } if(n<0)return Integer.MAX_VALUE; else{ if((hs.contains(1)&&n%2==1)||hs.contains(2)&&((n&2)>0)){ return Integer.MAX_VALUE; } for(long i=0;i<62;i++){ long mask=(long)(1l<<i); if((mask & n)>0)ans++; } } return ans+hs.size(); } static void solve() { long n=l(); long pow=(long)(Math.log(n)/Math.log(2)); long tw=p(2,15); int ans=Integer.MAX_VALUE; for(int i=0;i<=(int)tw;i++){ int x=solve2(i,n); ans=Math.min(ans,x); } if(ans==Integer.MAX_VALUE)ans=-1; sb.append(ans+"\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); dp2=new HashMap<>(); fact=new long[15]; fact[0]=fact[1]=1; pow=new long[41]; for(int i=2;i<fact.length;i++) { fact[i]=fact[i-1]*i;} while (test-- > 0) { solve(); } System.out.println(sb); } //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) ; y--; } x = (x * x) ; y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = -1; } int find(int a) { if (parent[a] < 0) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int x; long y; Pair(int x, long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (this.y - o.y); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static char[] sort(char[] a2) { int n = a2.length; ArrayList<Character> l = new ArrayList<>(); for (char i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
367fd9f072120056a60afa1a041a719b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class Eshan { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.0000000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); // int t = 1; int t = readInt(); preprocess(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { long n = readLong(); int count = Long.bitCount(n); for (long k : map.keySet()) { if (k <= n) count = Math.min(count, map.get(k) + Long.bitCount(n - k)); } out.println(count); } static Map<Long, Integer> map; private static void preprocess() { List<Long> fact = new ArrayList<>(); fact.add(1L); for (int i = 2;; i++) { long prev = fact.get(i - 2); if (prev * i > 1e12) break; fact.add(prev * i); } map = new HashMap<>(); fill(fact, 0, 0L, 0); } private static void fill(List<Long> fact, int i, long sum, int t) { if (i == fact.size()) { map.put(sum, t); return; } fill(fact, i + 1, sum, t); fill(fact, i + 1, sum + fact.get(i), t + 1); } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac Eshan.java // java Eshan // javac Eshan.java && java Eshan // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair o) { if (this.first != o.first) return this.second - o.second; return this.first - o.first; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b >> 1); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b, int mod) { if (b == 0) return 1; int temp = mod_power(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) { if (primes[j] == j) primes[j] = i; } } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== SEGMENT TREE (RANGE SUM) ===================== public static class SegmentTree { int n; int[] arr, tree, lazy; SegmentTree(int arr[]) { this.arr = arr; this.n = arr.length; this.tree = new int[(n << 2)]; this.lazy = new int[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, int val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } int query(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } public void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } public boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
de3929067a7dbaf032532b148a233ec4
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; // Created by @thesupremeone on 3/18/22 public class FactorialsAndPowersOf2 { long n; int bitCount; void solve() { int ts = getInt(); for (int t = 0; t < ts; t++) { n = getLong(); bitCount = Long.bitCount(n); long i = 1; ArrayList<Long> set = new ArrayList<>(); long fact = 1; while (fact<=n){ set.add(fact); fact *= i; i++; } int ans = Integer.MAX_VALUE; for (int j = 0; j < 1 << set.size(); j++) { long sum = n; for (int k = 0; k < set.size(); k++) { int mask = 1<<k; if((mask&j)!=0){ sum -= set.get(k); } } if(sum<0) continue; ans = Math.min(ans, Long.bitCount(sum)+Integer.bitCount(j)); } println(ans); } } public static void main(String[] args) throws Exception { if (isOnlineJudge()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new FactorialsAndPowersOf2().solve(); out.flush(); } else { localJudge = new Thread(); in = new BufferedReader(new FileReader("input.txt")); out = new BufferedWriter(new FileWriter("output.txt")); localJudge.start(); new FactorialsAndPowersOf2().solve(); out.flush(); localJudge.suspend(); } } static boolean isOnlineJudge() { try { return System.getProperty("ONLINE_JUDGE") != null || System.getProperty("LOCAL") == null; } catch (Exception e) { return true; } } // Fast Input & Output static Thread localJudge = null; static BufferedReader in; static StringTokenizer st; static BufferedWriter out; static String getLine() { try { return in.readLine(); } catch (Exception ignored) { return ""; } } static String getToken() { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(getLine()); return st.nextToken(); } static int getInt() { return Integer.parseInt(getToken()); } static long getLong() { return Long.parseLong(getToken()); } static void print(Object s) { try { out.write(String.valueOf(s)); } catch (Exception ignored) { } } static void println(Object s) { try { out.write(String.valueOf(s)); out.newLine(); } catch (Exception ignored) { } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
5d5f0740fbde344257991ce280a9768f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long t=1; t=sc.nextLong(); long[] a=new long[16]; a[0]=1; for(int i=1;i<16;i++) { a[i]=a[i-1]*i; //System.out.println(a[i]); } while(t-->0) { long n=sc.nextLong(); long q=n; long res=0; while(q>0) { res += q % 2; q/=2; } long m=(1<<16); for(int i=1;i<m;i++) { long sum=0,k=0; for(int j=0;j<16;j++) { int h=(1<<j); if((h&i)!=0) { k++; sum+=a[j]; } } if(n>sum) { q=n-sum; while(q>0) { k+=q%2; q/=2; } } if(n>=sum) { if(k<res) { res=k; } } } System.out.println(res); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
36a2383bf73e8f2223b7e044908768ac
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); long t=1; t=in.nextLong(); long[] a=new long[16]; a[0]=1; for(int i=1;i<16;i++) { a[i]=a[i-1]*i; //System.out.println(a[i]); } while(t-->0) { long n=in.nextLong(); long q=n; long res=0; while(q>0) { res += q % 2; q/=2; } long m=(1<<16); for(int i=1;i<m;i++) { long sum=0,k=0; for(int j=0;j<16;j++) { int h=(1<<j); if((h&i)!=0) { k++; sum+=a[j]; } } if(n>sum) { q=n-sum; while(q>0) { k+=q%2; q/=2; } } if(n>=sum) { if(k<res) { res=k; } } } out.println(res); } out.close(); } private static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } boolean hasNext() { return st.hasMoreTokens(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
8b308d5a11879f24df00161d7d640e65
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long t=1; t=sc.nextLong(); while(t-->0) { long n=sc.nextLong(); long q=n; long res=0; while(q>0) { res += q % 2; q/=2; } long[] a=new long[16]; a[0]=1; for(int i=1;i<16;i++) { a[i]=a[i-1]*i; //System.out.println(a[i]); } long m=(1<<16); for(int i=1;i<m;i++) { long sum=0,k=0; for(int j=0;j<16;j++) { int h=(1<<j); if((h&i)!=0) { k++; sum+=a[j]; } } if(n>sum) { q=n-sum; while(q>0) { k+=q%2; q/=2; } } if(n>=sum) { if(k<res) { res=k; } } } System.out.println(res); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
5e36794666c18d35e3a55b9a520a9365
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class codeforces_774_C { private static void solve(FastIOAdapter in, PrintWriter out) { long n = in.nextLong(); int ans = Long.bitCount(n); var l = new ArrayList<Long>(); long cur = 1; for (int i = 1; i * cur <= n; i++) { cur *= i; l.add(cur); } for (int i = 0; i < 1 << l.size(); i++) { long taken = 0; for (int j = 0; j < l.size(); j++) { if ((i & (1 << j)) != 0) { taken += l.get(j); } } if (taken <= n) { ans = Math.min(ans, Long.bitCount(n - taken) + Long.bitCount(i)); } } out.println(ans); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter, ioAdapter.out); } } } static void ruffleSort(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return null; } } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } @Override public void close() throws Exception { out.flush(); out.close(); br.close(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7a956bc6c8e5c0b5482af569837d86d5
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static long M = (long)(1e12); static int INF = 0x3f3f3f3f; static long min; static long n; static List<Long> list = new ArrayList<>(); static Set<Long> set = new HashSet<>(); public static void main(String[] args) { init(); FastScanner sc = new FastScanner(); int T = sc.nextInt(); while (T-- > 0) { n = sc.nextLong(); min = INF; solver(); if(min == INF) System.out.println(-1); else System.out.println(min); } } static void init(){ long res = 1; long st = 1; while (res <= M){ res = res * st; st++; list.add(res); } } static void solver() { for(int i = 0; i <= (1 << list.size()); i++){ long curS = 0; long cntP = 0; set.clear(); for(long j = 0; j < list.size(); j++){ if(((i >> j) & 1) == 1) { curS += list.get((int)j); set.add(list.get((int)j)); cntP++; } } // System.out.println(curS + " " + cntP); if(curS > n) continue; else{ long res = n - curS; for(long k = 0; k < 41; k++){ if(((res >> k) & 1) == 1){ cntP++; } } min = Math.min(min, cntP); } } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
575625a618b0b6691259441e98541ec7
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class C_Factorials_and_Powers_of_Two { private static int ans ; private static void solve(long n, int i,List<Long> arr, int k){ if(i==arr.size()){ ans = min(ans,k + Long.bitCount(n)); return; } if(arr.get(i)<=n){ solve(n-arr.get(i),i+1,arr,k+1); } solve(n,i+1,arr,k); } public static void main(String[] args) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); // int N = Integer.parseInt(st.nextToken()); // int M = Integer.parseInt(st.nextToken()); // /* int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); FastScanner fs = new FastScanner(); List<Long> set = new ArrayList<>(); long a = 1; for(int i = 1;i<=15;i++){ a = a*(long)i; set.add(a); } // System.out.println(set.size()); while(T-->0) { st = new StringTokenizer(infile.readLine()); // int N = Integer.parseInt(st.nextToken()); long n = Long.parseLong(st.nextToken()); ans = Integer.MAX_VALUE; long N = n; Set<Long> s = new HashSet<Long>(); solve(n,0,set,0); System.out.println(ans); } // */ // BufferedReader infile = new BufferedReader(new FileReader()); // System.setOut(new PrintStream(new File())); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+' '); out.println(); } public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if(!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k)+v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if(lol == v) map.remove(k); else map.put(k, lol-v); } public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for(int x: ls) if(!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for(int i=0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for(int a=0; a < N; a++) for(int b=0; b < M; b++) for(int c=0; c < left[0].length; c++) { res[a][b] += (left[a][c]*right[c][b])%MOD; if(res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for(int i=0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while(pow > 0) { if((pow&1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } } class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N+1]; size = new int[N+1]; for(int i=0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; //change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N+1]; exit = new int[N+1]; dp = new int[N+1][LOG]; this.edges = edges; int[] time = new int[1]; //change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for(int b=1; b < LOG; b++) for(int v=1; v <= N; v++) dp[v][b] = dp[dp[v][b-1]][b-1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for(int next: edges[curr]) if(next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if(isAnc(x, y)) return x; if(isAnc(y, x)) return y; int curr = x; for(int b=LOG-1; b >= 0; b--) { int temp = dp[curr][b]; if(!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
f6ec8a31a4163b826633e7b3a3363e70
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
/*----------- ---------------* Author : Ryan Ranaut __Hope is a big word, never lose it__ ------------- --------------*/ import java.io.*; import java.util.*; public class Codeforces1 { static PrintWriter out = new PrintWriter(System.out); static final int mod = 1_000_000_007; static int max = (int)(1e6); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /*--------------------------------------------------------------------------*/ //Try seeing general case //Minimization Maximization - BS..... Connections - Graphs..... //Greedy not worthy - Try DP public static void main(String[] args) { FastReader s = new FastReader(); List<Long> ls = new ArrayList<>(); long fact = 1; for(int i=1;i<=15;i++) { ls.add(fact); fact *= i; } int t = s.nextInt(); while(t-->0) { long ans = Long.MAX_VALUE; long sum = s.nextLong(); for(long i=0;i<(1L<<15);i++) { long s1 = 0; for(int j=0;j<15;j++) { if((i & (1L<<j)) != 0) s1 += ls.get(j); } if(s1>sum) break; long ops = Long.bitCount(i); ops += Long.bitCount(sum-s1); ans = Math.min(ans, ops); } out.println(ans); } out.close(); } /*----------------------------------End of the road--------------------------------------*/ static class DSU { int[] parent; int[] ranks; int[] groupSize; int size; public DSU(int n) { size = n; parent = new int[n];//0 based ranks = new int[n]; groupSize = new int[n];//Size of each component for (int i = 0; i < n; i++) { parent[i] = i; ranks[i] = 1; groupSize[i] = 1; } } public int find(int x)//Path Compression { if (parent[x] == x) return x; else return parent[x] = find(parent[x]); } public void union(int x, int y)//Union by rank { int x_rep = find(x); int y_rep = find(y); if (x_rep == y_rep) return; if (ranks[x_rep] < ranks[y_rep]) { parent[x_rep] = y_rep; groupSize[y_rep] += groupSize[x_rep]; } else if (ranks[x_rep] > ranks[y_rep]) { parent[y_rep] = x_rep; groupSize[x_rep] += groupSize[y_rep]; } else { parent[y_rep] = x_rep; ranks[x_rep]++; groupSize[x_rep] += groupSize[y_rep]; } size--;//Total connected components } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static long gcd(long x,long y) { return y==0L?x:gcd(y,x%y); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long pow(long a,long b) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; b>>=1; } return (tmp*a); } public static long modPow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1L) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } static long mul(long a, long b) { return a*b; } static long fact(int n) { long ans=1; for (int i=2; i<=n; i++) ans=mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static void debug(int ...a) { for(int x: a) out.print(x+" "); out.println(); } static void debug(long ...a) { for(long x: a) out.print(x+" "); out.println(); } static void debugMatrix(int[][] a) { for(int[] x:a) out.println(Arrays.toString(x)); } static void debugMatrix(long[][] a) { for(long[] x:a) out.println(Arrays.toString(x)); } static void reverseArray(int[] a) { int n = a.length; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void sort(int[] a) { ArrayList<Integer> ls = new ArrayList<>(); for(int x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static void sort(long[] a) { ArrayList<Long> ls = new ArrayList<>(); for(long x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static class Pair{ int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } @Override public int hashCode() { return Objects.hash(x, y); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
e072a522f546bd48627d08209eb4ea2f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
/*----------- ---------------* Author : Ryan Ranaut __Hope is a big word, never lose it__ ------------- --------------*/ import java.io.*; import java.util.*; public class Codeforces1 { static PrintWriter out = new PrintWriter(System.out); static final int mod = 1_000_000_007; static int max = (int)(1e6); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /*--------------------------------------------------------------------------*/ //Try seeing general case //Minimization Maximization - BS..... Connections - Graphs..... //Greedy not worthy - Try DP static List<Long> ls; public static void main(String[] args) { FastReader s = new FastReader(); ls = new ArrayList<>(); long fact = 1; for(int i=2;i<=15;i++) { ls.add(fact); fact *= i; } int t = s.nextInt(); while(t-->0) { long ans = Long.MAX_VALUE; long sum = s.nextLong(); hmp = new HashMap<>(); find(0, 0, 0); long ops = 0; for(Long key: hmp.keySet()) { ops = hmp.get(key); long rem = sum-key; ops += Long.bitCount(rem); ans = Math.min(ans, ops); } out.println(ans); } out.close(); } static Map<Long, Integer> hmp; public static void find(int i, long s, int count) { if(i == ls.size()) { hmp.put(s, count); return ; } find(i+1, s+ls.get(i), count+1); find(i+1, s, count); } /*----------------------------------End of the road--------------------------------------*/ static class DSU { int[] parent; int[] ranks; int[] groupSize; int size; public DSU(int n) { size = n; parent = new int[n];//0 based ranks = new int[n]; groupSize = new int[n];//Size of each component for (int i = 0; i < n; i++) { parent[i] = i; ranks[i] = 1; groupSize[i] = 1; } } public int find(int x)//Path Compression { if (parent[x] == x) return x; else return parent[x] = find(parent[x]); } public void union(int x, int y)//Union by rank { int x_rep = find(x); int y_rep = find(y); if (x_rep == y_rep) return; if (ranks[x_rep] < ranks[y_rep]) { parent[x_rep] = y_rep; groupSize[y_rep] += groupSize[x_rep]; } else if (ranks[x_rep] > ranks[y_rep]) { parent[y_rep] = x_rep; groupSize[x_rep] += groupSize[y_rep]; } else { parent[y_rep] = x_rep; ranks[x_rep]++; groupSize[x_rep] += groupSize[y_rep]; } size--;//Total connected components } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static long gcd(long x,long y) { return y==0L?x:gcd(y,x%y); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static long pow(long a,long b) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; b>>=1; } return (tmp*a); } public static long modPow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1L) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } static long mul(long a, long b) { return a*b; } static long fact(int n) { long ans=1; for (int i=2; i<=n; i++) ans=mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp==0) return 1; long half=fastPow(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static void debug(int ...a) { for(int x: a) out.print(x+" "); out.println(); } static void debug(long ...a) { for(long x: a) out.print(x+" "); out.println(); } static void debugMatrix(int[][] a) { for(int[] x:a) out.println(Arrays.toString(x)); } static void debugMatrix(long[][] a) { for(long[] x:a) out.println(Arrays.toString(x)); } static void reverseArray(int[] a) { int n = a.length; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void sort(int[] a) { ArrayList<Integer> ls = new ArrayList<>(); for(int x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static void sort(long[] a) { ArrayList<Long> ls = new ArrayList<>(); for(long x: a) ls.add(x); Collections.sort(ls); for(int i=0;i<a.length;i++) a[i] = ls.get(i); } static class Pair{ int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } @Override public int hashCode() { return Objects.hash(x, y); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
6db2182c20ef409d3c66c949c225d0d5
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; private HashSet<Long> set; private long p[]; private int size; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } set = new HashSet<Long>(); int t = sc.nextInt(); long limit = 1_000_000_000_000l; long temp = 2l; int count = 3; set.add(1l); set.add(2l); while (true) { temp *= (long)count; count++; if (temp > limit) break; set.add(temp); } int size = set.size(); p = new long[size]; count = 0; for (long i : set) { p[count++] = i; // System.out.print(p[count - 1] + " "); } // System.out.println(); Sorter.sort(p); // int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; private class Pair { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } } private long n; private int x; public void solve() { n = sc.nextLong(); x = calculate(n, false, false); int temp = helper(0, 0, 0, false, false); pw.println(temp); } private int calculate(long n, boolean contains1, boolean contains2) { long temp = n; int ans = 0; for (int i = 40; i >= 0; i--) { if (temp >= (1l << i)) { if (i == 1 && contains2) { return 100; } else if (i == 0 && contains1) { return 100; } temp -= (1l << i); ans++; } } return ans; } private int helper(int pos, long sum, int count, boolean contains1, boolean contains2) { if (sum > n) return (int)mod; if (sum == n) return count; if (pos == p.length) { return (count + calculate(n - sum, contains1, contains2)); } return (int)Math.min(helper(pos + 1, sum, count, contains1, contains2) , helper(pos + 1, sum + p[pos], count + 1, contains1 || p[pos] == 1l, contains2 || p[pos] == 2l)); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : ans; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
4741ba1de14befa75534472b538bf425
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; public class S { static long solve(ArrayList<Long> coins, long n, int ind, long sum, long itr) { if (ind < coins.size()) { return Math.min(solve(coins, n, ind + 1, sum, itr), solve(coins, n, ind + 1, sum + coins.get(ind), itr + 1)); } else { return Long.bitCount(n - sum) + itr; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); ArrayList<Long> coins = new ArrayList<Long>(); Long a = 1L; for (int i = 1; i < 15; i++) { a *= i; coins.add(a); } for (int tt = 0; tt < t; tt++) { long n = sc.nextLong(); System.out.println(Math.min(Long.bitCount(n), solve(coins, n, 0, 0L, 0L))); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7eda39171fc4d2a5728f1152ff233625
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static final int mod=100000007; static final int maxn=1000+10; static IO io; static long[] fac=new long[12]; static HashSet<val> hs=new HashSet<>(); public static void main(String[] args)throws IOException { io=new IO(); int t=io.nextInt(); fac[0]=6; for(int i=1;i<12;i++) { fac[i]=fac[i-1]*(i+3); } for(int i=0;i<(1<<12);i++) { long tmp=0; for(int j=0;j<12;j++) { if( (1&(i>>j)) ==1 ) { tmp+=fac[j]; } } hs.add(new val(tmp,Long.bitCount(i))); } while(t>0) { t--; solve(); } io.flush();io.close(); } private static void solve()throws IOException { long maxx=Long.MAX_VALUE; long n=io.nextLong(); //long maxx=Long.bitCount(n); for(val x : hs) { long left=n-x.n; if(left<0) continue; maxx=Math.min(maxx,Long.bitCount(left)+x.c); } io.println(maxx); } public static class val { long n;int c; public val(long n,int c) { this.n=n;this.c=c; } } static class IO extends PrintWriter { BufferedReader br; StringTokenizer st; // standard input public IO() { this(System.in, System.out); } public IO(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public IO(String problemName) throws IOException { super(problemName + ".out"); br = new BufferedReader(new FileReader(problemName + ".in")); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
3e1501f932b6e5cbe76efc6f0ba180cf
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main{ static final int mod=100000007; static final int maxn=1000+10; static IO io; static long[] fac=new long[12]; static HashSet<val> hs=new HashSet<>(); public static void main(String[] args)throws IOException { io=new IO(); int t=io.nextInt(); fac[0]=6; for(int i=1;i<12;i++) { fac[i]=fac[i-1]*(i+3); } for(int i=0;i<(1<<12);i++) { long tmp=0; for(int j=0;j<12;j++) { if( (i&(1<<j)) !=0) { tmp+=fac[j]; } } hs.add(new val(tmp,Long.bitCount(i))); } while(t>0) { t--; solve(); } io.flush();io.close(); } private static void solve()throws IOException { long maxx=Long.MAX_VALUE; long n=io.nextLong(); //long maxx=Long.bitCount(n); for(val x : hs) { long left=n-x.n; if(left<0) continue; maxx=Math.min(maxx,Long.bitCount(left)+x.c); } io.println(maxx); } public static class val { long n;int c; public val(long n,int c) { this.n=n;this.c=c; } } static class IO extends PrintWriter { BufferedReader br; StringTokenizer st; // standard input public IO() { this(System.in, System.out); } public IO(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public IO(String problemName) throws IOException { super(problemName + ".out"); br = new BufferedReader(new FileReader(problemName + ".in")); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
e7b1571394470019e01ee301f10263a9
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class Main { static long N = 1000000000000L; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); long[] a = new long[100]; HashSet<Long> set = new HashSet<>(); int m = 0; long now = 1L; while(true) { set.add(now); now <<= 1; if(now > N ) break; } now = 1L; long temp = 1; while(true) { if( !set.contains(now) ) { a[m++] = now; } now = now * temp; temp++; if(now > N) break; } //System.out.println(m); Arrays.sort(a, 0, m); while(t-- != 0) { long n = in.nextLong(); long ans = 60; for(int i = 0; i < (1 << m); i ++) { now = 0L; temp = n; long res = 0; for(int j = 0; j < m; j ++) { if( (i >> j & 1) == 1) { now += a[j]; res++; } } if(now <= n) { temp -= now; } while (temp != 0) { if (temp%2 == 1) res++; temp >>= 1; } ans = Math.min(res, ans); } System.out.println(ans); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
d7e19891712d7354cd02f5c4ca9f95db
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; /** * * @author eslam */ public class IncreaseSubarraySums { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static ArrayList<ArrayList<Long>> powerSet = new ArrayList<>(); public static void main(String[] args) throws IOException { Queue<Long> fa = new LinkedList<>(); for (int i = 1; i < 15; i++) { fa.add(factorial(i)); } getPowerSet(fa); ArrayList<pair> sum = new ArrayList<>(); for (int i = 0; i < powerSet.size(); i++) { long s = 0; for (int j = 0; j < powerSet.get(i).size(); j++) { s += powerSet.get(i).get(j); } sum.add(new pair(s, powerSet.get(i).size())); } FastReader input = new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t = input.nextInt(); while (t-- > 0) { long n = input.nextLong(); int ans = cnt1(n); for (int i = 0; i < sum.size(); i++) { if (sum.get(i).x > n) { continue; } ans = Math.min(ans, sum.get(i).y + cnt1(n - sum.get(i).x)); } log.write(ans + "\n"); } log.flush(); } public static int cnt1(long n) { String w = Long.toBinaryString(n); int an = 0; for (int i = 0; i < w.length(); i++) { an += (w.charAt(i) - '0'); } return an; } public static void getPowerSet(Queue<Long> a) { long n = a.poll(); if (!a.isEmpty()) { getPowerSet(a); } int s = powerSet.size(); for (int i = 0; i < s; i++) { ArrayList<Long> ne = new ArrayList<>(); ne.add(n); for (int j = 0; j < powerSet.get(i).size(); j++) { ne.add(powerSet.get(i).get(j)); } powerSet.add(ne); } ArrayList<Long> p = new ArrayList<>(); p.add(n); powerSet.add(p); } static class pair { long x; int y; public pair(long x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } } public static long factorial(long n) { long t = n - 1; while (t > 1) { n *= t; t--; } return n; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
409b4ff82ebfdb99ea75b53623688ece
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; public class Factorial_And_Powers_Of_Two { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); long fact[] = new long[15]; fact[0] = 1; for(int i=1;i<15;i++) { fact[i] = ((long)i)*fact[i-1]; if(fact[i-1]<=2) fact[i-1] = -1; }while(t-->0) { long n = sc.nextLong(); int ans = Integer.MAX_VALUE; for(int i=0;i<(1<<15);i++) { long sum = 0; boolean lower = true; for(int j=0;j<15;j++) { if((i>>j)%2==1&&fact[j]!=-1) sum += fact[j]; if(sum>n) { lower = false; break; } } if(lower) { int temp = count(i)+count(n-sum); ans = Math.min(ans, temp); } } System.out.println(ans); } } public static int count(long n) { int ans = 0; while(n>0) { long d = (n&1); ans += (int)d; n >>=1;; } return ans; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
258ea9aa79e1c50f32bf2ed7fbe12bce
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static int i() { return obj.nextInt(); } public static void main(String[] args) { int len = i(); while (len-- != 0) { long n=obj.nextLong(); Vector<Long> v=new Vector<>(); long fac=6; for(int i=4;true;i++) { if(fac>n)break; v.add(fac); fac=fac*i; } long ans=Integer.MAX_VALUE; for(int mask=0;mask<(1<<v.size());mask++) { long sum=n; for(int i=0;i<v.size();i++) if((mask&(1<<i))!=0)sum-=v.get(i); if(sum<0)continue; int tut=Integer.bitCount(mask)+Long.bitCount(sum); ans=Math.min(ans, tut); } out.println(ans); } out.flush(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7607b7d8c661640b473eddf796aa5b6d
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static PrintWriter out; static Kioken sc; public static void main(String[] args) throws FileNotFoundException { boolean t = true; boolean f = false; if (f) { out = new PrintWriter("output.txt"); sc = new Kioken("input.txt"); } else { out = new PrintWriter((System.out)); sc = new Kioken(); } int tt = 1; int n = 21; long[] fac = new long[n]; fac[0] = 1; fac[1] = 1; fac[2] = 2; fac[3] = 6; for(int i = 4; i < n; i++){ fac[i] = fac[i-1]*(i); // out.println(" " + fac[i]); } tt = sc.nextInt(); while (tt-- > 0) { solve(fac); } out.flush(); out.close(); } static long setBit(long v){ int cnt = 0; while(v > 0){ if((v&1) == 1){ cnt++; } v = v >> 1; } return (long)cnt; } public static void solve(long[] fac) { long n = sc.nextLong(); int min = Long.bitCount(n); for(int i = 0; i < (1 << 15); i++){ int temp = i; long ll = n; int j = 0; int cnt = 0; while(temp > 0){ if((temp&1) != 0){ ll -= fac[j]; cnt++; } j++; temp = temp >> 1; } if(ll >= 0){ min = Math.min(min, (cnt + Long.bitCount(ll))); } } out.println(min); } public static long gcd(long a, long b) { while (b != 0) { long rem = a % b; a = b; b = rem; } return a; } public static long leftShift(long a) { return (long) Math.pow(2, a); } public static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n + 1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (!isPrime[i]) continue; for (int j = i * 2; j <= n; j = j + i) { isPrime[j] = false; } } return isPrime; } public static void reverse(int[] arr) { Arrays.sort(arr); int n = arr.length; for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = temp; } return; } public static int lower_bound(ArrayList<Integer> ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList<Integer> ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { // FileInputStream br = new FileInputStream("input.txt"); BufferedReader br; StringTokenizer st; Kioken(String filename) { try { FileReader fr = new FileReader(filename); br = new BufferedReader(fr); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } Kioken() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null || next.length() == 0) { return false; } st = new StringTokenizer(next); return true; } } class DSU { int[] parent, size; DSU(int n) { parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int findParent(int i) { if (parent[i] == i) { return i; } return parent[i] = findParent(parent[i]); } void Union(int u, int v) { int parent_u = findParent(u); int parent_v = findParent(v); if (parent_u == parent_v) return; // small attached to big, since we want to reduce overall size if (size[parent_u] < size[parent_v]) { parent[parent_u] = parent_v; size[parent_v]++; } else { parent[parent_v] = parent_u; size[parent_u]++; } } } // SEGMENT-TREE static class SegmentTree { int[] arr = new int[4 * 100000]; int[] givenArr; // HINT: This can be updated with ques. int build(int index, int l, int r) { if (l == r) { return arr[index] = givenArr[l]; } int mid = (l + r) / 2; return arr[index] = build(2 * index + 1, l, mid) + build(2 * index + 2, mid + 1, r); } SegmentTree(int[] nums) { givenArr = nums; build(0, 0, nums.length - 1); } // HINT: This can be updated with ques. void update(int index, int l, int r, int diff, int i) { if (i >= arr.length) { return; } if (index >= l && index <= r) { arr[i] = arr[i] + diff; } if (index < l || index > r) { return; } int mid = (l + r) / 2; update(index, l, mid, diff, 2 * i + 1); update(index, mid + 1, r, diff, 2 * i + 2); return; } void update(int index, int val) { int diff = val - givenArr[index]; givenArr[index] = val; update(index, 0, givenArr.length - 1, diff, 0); } int query(int left, int right, int l, int r, int i) { // not overlapping if (r < left || l > right) { return 0; } // total - overlapping if (l >= left && r <= right) { return arr[i]; } // partial overlapping int mid = (l + r) / 2; int le = query(left, right, l, mid, 2 * i + 1); int ri = query(left, right, mid + 1, r, 2 * i + 2); return le + ri; } // HINT: for max sum, can be changed according to ques. int query(int l, int r) { return query(l, r, 0, givenArr.length - 1, 0); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
1ec1a5de8c122e27f94395cc18e7c88f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import javax.lang.model.type.IntersectionType; import javax.swing.*; import java.lang.reflect.Array; import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static FastReader sc = new FastReader(); static long [] factorials = new long[15]; public static void main (String[] args) throws java.lang.Exception { int t = sc.nextInt(); factorials[0] = factorials[1] = 1; for(int i = 2;i<15;++i){ factorials[i] = i*factorials[i-1]; } while(t-->0){ solve(); } } public static void solve() { long n = l(); if(n==1 || n==2 || n==4){ out.println(1);out.flush();return; } if(n == 3){ out.println(2);out.flush();return; } long ans = Long.MAX_VALUE; int nob = 14; while(nob>=0&&factorials[nob]>n){ nob--; } // 91009901168 long mask = (1L<<nob) - 1; for(int i = 0;i<=mask;++i){ long temp = n; long cdt = 0; for(int j = 0;j<nob;++j){ if(((1<<j)&i) != 0){ cdt++; if(temp - factorials[nob-j]<0)break; temp-=factorials[nob-j]; } } cdt+=csb(temp); ans = Math.min(ans, cdt); } out.println(ans); out.flush(); } /* int [] arr = new int[n]; for(int i = 0;i<n;++i){ arr[i] = sc.nextInt(); } */ static int csb(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static class Pair implements Comparable<Pair>{ int ind;int val; Pair(int ind,int val){ this.ind=ind; this.val=val; } public int compareTo(Pair o){ return this.val-o.val; } } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
349a7e0f1577ec8182a4f2f45793495d
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long ans; public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); long t = scan.nextInt(); for (int test = 0; test < t; test++) { long n = scan.nextLong(); ans = Integer.MAX_VALUE; long fact = 1; long next = 2; getSolve(fact, next, n, 0); System.out.println(ans); } } public static void getSolve(long fact, long next, long n, long step) { if(n<0){ return; } if (next == 16){ // System.out.println(n+" "+step); getTwoPower(n, step); return; } getSolve(fact * next, next + 1, n - fact, step + 1); getSolve(fact * next, next + 1, n, step); } public static void getTwoPower(long n, long step) { long pow = 0; while (n > 0) { if ((n & 1) == 1) { pow++; } n = n >> 1; } ans = Math.min(ans, step + pow); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7cb555dc303e7a0bc84c09b41285c9e9
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class C { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { ArrayList<Long> fac = new ArrayList<>(); for (int i = 3; i <= 14; i++) { long p = 1; for (int j = 1; j <= i; j++) { p *= j; } fac.add(p); } int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { long n = fs.nextLong(); int ans = 100; for (int i = 0; i < (1 << 12); i++) { long n2 = n; for (int j = 0; j < 12; j++) { if (((1 << j) & i) == (1 << j)) { n2 -= fac.get(j); } } if (n2 >= 0) ans = Math.min(ans, Integer.bitCount(i) + Long.bitCount(n2)); } sb.append(ans + "\n"); } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
de507452cc7480ee230178b6bf399caa
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class C { // static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { ArrayList<Long> fac = new ArrayList<>(); for (int i = 3; i <= 14; i++) { long p = 1; for (int j = 1; j <= i; j++) { p *= j; } fac.add(p); } int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { long n = fs.nextLong(); int ans = 100; for (int i = 0; i < (1 << 12); i++) { long n2 = n; for (int j = 0; j < 12; j++) { if (((i >> j) & 1) == 1) { n2 -= fac.get(j); } } if (n2 >= 0) ans = Math.min(ans, Integer.bitCount(i) + Long.bitCount(n2)); } sb.append(ans + "\n"); } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
544ffb969deadfbac17cefc80ae6083f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class C { static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(""); public static void main(String[] args) { ArrayList<Long> fac = new ArrayList<>(); for (int i = 3; i <= 14; i++) { long p = 1; for (int j = 1; j <= i; j++) { p *= j; } fac.add(p); } int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { long n = fs.nextLong(); int ans = 100; for (int i = 0; i < (1 << 12); i++) { long n2 = n; for (int j = 0; j < 12; j++) { if (((i >> j) & 1) == 1) { n2 -= fac.get(j); } } if (n2 >= 0) ans = Math.min(ans, Integer.bitCount(i) + Long.bitCount(n2)); } sb.append(ans + "\n"); } pw.print(sb.toString()); pw.close(); } static void sort(int[] a) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) al.add(i); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
a391740a9ffd8f10fe8c8d3b1562333d
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.lang.Math; import java.nio.BufferOverflowException; import java.util.*; import java.io.*; import java.lang.Math; public final class code { boolean isSub; code() { this.isSub = false; } static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> obj, Pair<Integer, Integer> obj1) { if (obj.getValue() < obj1.getValue()) { return -1; } else { return 1; } } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } f getKey() { return this.a; } s getValue() { return this.b; } } interface modOperations { long mod(long a, long b, long mod); } static long findBinaryExponentian(long a, long pow, long mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { long retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod(modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod); } } static long findPow(long a, int b) { if (b == 1) { return a; } else if (b == 0) { return 1L; } else { long res = findPow(a, (int) b / 2); return res * res * (b % 2 == 1 ? a : 1); } } static int bleft(int ele, ArrayList<Integer> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(long no) { int i = 0; while ((1 << i) <= no) { i += 1; } return i - 1; } static modOperations modAdd = (long a, long b, long mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (long a, long b, long mod) -> { return (a % mod - b % mod + mod) % mod; }; static modOperations modMul = (long a, long b, long mod) -> { return (a % mod * b % mod) % mod; }; static modOperations modDiv = (long a, long b, long mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static ArrayList<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; ArrayList<Integer> obj = new ArrayList<Integer>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int cal_log(long no) { int i = 0; while (i <= no) { i++; } return i - 1; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Long> fact = new ArrayList<Long>(); ArrayList<Long> pow = new ArrayList<Long>(); int i = 0, cnt = 1, cases, inc, j; long res = 1, n, tot, rem,min_op; for (i = 1; i <= 14; i++) { res *= cnt; cnt++; fact.add(res); } res = 1; for (i = 0; i <= 48; i++) { pow.add(res); res *= 2; } cases = Integer.parseInt(br.readLine()); while (cases-- != 0) { n = Long.parseLong(br.readLine()); min_op = findPow(10, 12)+1; long te=pow.get(14); for (i = 0; i < te; i++) { tot = 0L; inc = 0; for (j = 0; j < 14; j++) { if ((pow.get(j) & i) != 0) { tot += fact.get(j); inc++; } } if (tot <= n) { rem = n - tot; for (j = 0; j <= 48; j++) { if ((pow.get(j) & rem) != 0) { inc++; } } min_op = Math.min(min_op, inc); } } System.out.println(min_op); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
71e2beae827e2cc1542f53cfc808d4c0
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(int a[]){ // int -> long ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long for(int i=0;i<a.length;i++) arr.add(a[i]); Collections.sort(arr); for(int i=0;i<a.length;i++) a[i]=arr.get(i); } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } private static long pow(long x,long y){ if(y==0)return 1; long temp = pow(x, y/2); if(y%2==1){ return x*temp*temp; } else{ return temp*temp; } } static int log(long n){ if(n<=1)return 0; long temp = n; int res = 0; while(n>1){ res++; n/=(long)2; } return (1<<res)==temp?res:res; } static int mod = (int)1e9+7; static int INF = Integer.MAX_VALUE; static PrintWriter out; static FastReader sc ; public static void main(String[] args) throws IOException { sc = new FastReader(); out = new PrintWriter(System.out); // primes(); // ================================ // int test = sc.nextInt(); list = new ArrayList<>(); long cur = 6; int idx = 4; while(cur<=1e12){ list.add(cur); cur*=(long)idx; idx++; } while (test-- > 0) { long n= sc.nextLong(); out.println( solver(n)); } // ================================ // // int n = sc.nextInt(); // solver(); // ================================ // out.flush(); } static ArrayList<Long> list; public static int solver(long n) { if(n<=0)return 0; if(n<=2)return 1; int m = list.size(); int res = INF; for(int i=0;i<(1<<m);i++){ long ho = n; int k=0; for(int j=0;j<m;j++){ if(((1<<j)&i)!=0){ k++; ho-=list.get(j); } } if(ho>=0) res = Math.min(res, k+csb(ho)); } return res; } static int csb(long n){ int res = 0; while(n>0){ if(n%2==1)res++; n/=2; } return res; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
317966f742dd9da1eac5c6d9deb1153c
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static ArrayList<Long> al; static ArrayList<Long> all; static Map<Long, Integer> map; public static void main(String args[]) { if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } } else { // Input is System.in } FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); al = new ArrayList<>(); all= new ArrayList<>(); map = new HashMap<>(); long start = 2; al.add(start-1); while(true){ if(start > 14)break; al.add(start*al.get(al.size()-1)); start++; } allValuesPos(al, all, 0, 0, 0); //System.out.println(all); Collections.sort(all); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t>0) { long n = sc.nextLong(); int ans = countSetBits(n); for(int i = 0; i<all.size(); i++){ if(all.get(i)>n)break; else { long newN = n-all.get(i); int newAns = countSetBits(newN) + map.get(all.get(i)); ans = Math.min(ans, newAns); } } sb.append(ans+"\n"); t--; } System.out.println(sb); } public static void allValuesPos(ArrayList<Long> al, ArrayList<Long> all, int ind, long asf, int eleUsed){ if(ind == al.size()){ if(asf == 0)return; all.add(asf); map.put(asf, eleUsed); return; } allValuesPos(al, all, ind+1, asf, eleUsed); allValuesPos(al, all, ind+1, asf+al.get(ind), eleUsed+1); return; } //////////nCr//////////////////////////////////////// public static int countSetBits(long n) { int count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } ///////// SUM OF EACH DIGIT OF A NUMBER /////////////// public static long digSum(long a) { long sum = 0; while(a>0) { sum += a%10; a /= 10; } return sum; } ///////// TO CHECK NUMBER IS PRIME OR NOT /////////////// public static boolean isPrime(int n) { if(n<=1)return false; if(n <= 3)return true; if(n%2==0 || n%3==0)return false; for(int i = 5; i*i<=n; i+=6) { if(n%i == 0 || n%(i+2) == 0)return false; } return true; } ///////// NEXT PRIME NUMBER BIGGER THAN GIVEN NUMBER /////////////// public static int nextPrime(int n) { while(true) { n++; if(isPrime(n)) break; } return n; } ///////// GCD /////////////// public static int gcd(int a, int b) { if(b == 0)return a; return gcd(b, a%b); } ///////// LCM /////////////// public static int lcm(int a, int b) { return (a*b)/gcd(a,b); } ///////// IS POWER OF 2 /////////////// public static boolean isPowerOfTwo (int x){ /* First x in the below expression is for the case when x is 0 */ return x!=0 && ((x&(x-1)) == 0); } } class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } @Override public boolean equals(Object o) { if(this == o)return true; if(o == null || this.getClass() != o.getClass())return false; Pair p = (Pair)o; return x == p.x && y == p.y; } @Override public int hashCode(){ return Objects.hash(x , y); } // @Override // public int compareTo(Pair o) { // } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
95fcf8ff207a973fd71d53240974346b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Factorials { public static void main(String[] args) throws IOException { BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(f.readLine()); while(t-->0) { long n=Long.parseLong(f.readLine()); ArrayList<Long> fact=new ArrayList<Long>(); long prod=1; int s=2; while(prod<=n) { fact.add(prod); prod*=s; s++; } int ans=Integer.MAX_VALUE; for(int i=0;i<(1<<fact.size());i++) { long sum=0; int count=0; boolean seen1=false; boolean seen2=false; for(int j=0;j<fact.size();j++) { if((i&(1<<j))>0) { sum+=fact.get(j); if(fact.get(j)==1) seen1=true; if(fact.get(j)==2) seen2=true; count++; } } long need=n-sum; if(need<0) continue; if(seen1&&need%2==1) continue; if(seen2&&(need/2)%2==1) continue; while(need!=0) { count+=need%2==1?1:0; need/=2; } if(count<ans) { ans=count; } } System.out.println(ans); } } } //2395410832
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
5496d28f0eda9f2e803c18162ab525e5
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { // when can't think of anything -->> // 1. In sorting questions try to think about all possibilities like sorting from start, end, middle. // 2. Two pointers, brute force. // 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse. // 4. If order does not matter then just sort the data if constraints allow. It'll never harm. // 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with. // 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve. public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); Set<List<Integer>> set = new HashSet<>(); List<Integer> midList = new ArrayList<>(); long factorial[] = {1,1,2,6,24,120,720, 5040,40320,362880,3628800,39916800,479001600,6227020800l,87178291200l}; int t = sc.nextInt(); generate(set, midList,1); while(t-->0) { long n = sc.nextLong(); int ans = Integer.MAX_VALUE; for (List<Integer> list : set) { long sum = 0; for (int i : list) { sum+=factorial[i]; } if(sum <= n) { int temp = list.size() + Long.bitCount(n-sum); ans = Math.min(ans, temp); } } if(ans == Integer.MAX_VALUE) ans = -1; writer.println(ans); } writer.flush(); writer.close(); } public static void generate(Set<List<Integer>> set, List<Integer> midList, int i) { if(i == 16)return; List<Integer> copy = new ArrayList<>(midList); set.add(copy); midList.add(i); generate(set, midList, i+1); // not adding midList.remove(midList.get(midList.size() - 1)); generate(set, midList, i+1); } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static int gcd(int a, int b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Integer.compare(this.b, o.b); }else { return Integer.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
31cbc8dc07465840dfd0b279ee2b583e
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.lang.Math; import java.nio.BufferOverflowException; import java.util.*; import java.io.*; import java.lang.Math; public final class code { boolean isSub; code() { this.isSub = false; } static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> obj, Pair<Integer, Integer> obj1) { if (obj.getValue() < obj1.getValue()) { return -1; } else { return 1; } } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } f getKey() { return this.a; } s getValue() { return this.b; } } interface modOperations { long mod(long a, long b, long mod); } static long findBinaryExponentian(long a, long pow, long mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { long retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod(modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod); } } static long findPow(long a, int b) { if (b == 1) { return a; } else if (b == 0) { return 1L; } else { long res = findPow(a, (int) b / 2); return res * res * (b % 2 == 1 ? a : 1); } } static int bleft(int ele, ArrayList<Integer> sortedArr) { int l = 0; int h = sortedArr.size() - 1; int ans = -1; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr.get(mid) < ele) { l = mid + 1; } else if (sortedArr.get(mid) >= ele) { ans = mid; h = mid - 1; } } return ans; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(long no) { int i = 0; while ((1 << i) <= no) { i += 1; } return i - 1; } static modOperations modAdd = (long a, long b, long mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (long a, long b, long mod) -> { return (a % mod - b % mod + mod) % mod; }; static modOperations modMul = (long a, long b, long mod) -> { return (a % mod * b % mod) % mod; }; static modOperations modDiv = (long a, long b, long mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static ArrayList<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; ArrayList<Integer> obj = new ArrayList<Integer>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } static int cal_log(long no) { int i = 0; while (i <= no) { i++; } return i - 1; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Long> fact = new ArrayList<Long>(); ArrayList<Long> pow = new ArrayList<Long>(); int i = 0, cnt = 1, cases, inc, j; long res = 1, n, tot, rem,min_op; for (i = 1; i <= 14; i++) { res *= cnt; cnt++; fact.add(res); } res = 1; for (i = 0; i <= 48; i++) { pow.add(res); res *= 2; } cases = Integer.parseInt(br.readLine()); while (cases-- != 0) { n = Long.parseLong(br.readLine()); min_op = findPow(10, 12)+1; long te=pow.get(14); for (i = 0; i < te; i++) { tot = 0L; inc = 0; for (j = 0; j < 14; j++) { if ((pow.get(j) & i) != 0) { tot += fact.get(j); inc++; } } if (tot <= n) { rem = n - tot; for (j = 0; j <= 48; j++) { if ((pow.get(j) & rem) != 0) { inc++; } } min_op = Math.min(min_op, inc); } } System.out.println(min_op); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
46d4e2358ea88e575ea0c6730fcc41b4
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.SocketTimeoutException; import java.sql.Array; import java.sql.SQLOutput; import java.util.*; public class Solution { static class in { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { //System.out.println(" I WAS CALLED"); return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static ArrayList<String> func(int n) { ArrayList<String> ret= new ArrayList<>(); if(n==1) { ret.add("0"); ret.add("1"); return ret; } ArrayList<String> rret=func(n-1); for(String i:rret) { ret.add("0"+i); } for(int i=rret.size()-1;i>=0;i--) { ret.add("1"+rret.get(i)); } return ret; } static ArrayList<String> arr= new ArrayList<>(); private static void move(int n, int start,int ind, int dest) { if(n==1) { arr.add(start+" "+dest); return; } move(n-1,start,dest,ind); arr.add(start+" "+dest); move(n-1,ind,start,dest); } private static HashSet <String>perm(String s) { HashSet<String> set= new HashSet<>(); if(s.length()==2) { set.add(s); set.add((s.charAt(1)+""+s.charAt(0))); return set; } for(int i=0;i<s.length();i++) { char a=s.charAt(i); HashSet<String> prv= perm(s.substring(0,i)+s.substring(i+1,s.length())); for(String curr:prv)set.add(a+curr); } return set; } static int ways=0; private static void merge(int l, int mid , int h, long arr[] ) { int n2= h-mid; int n1= mid-l+1; long left[]= new long [n1]; long right[]= new long [n2]; for(int i=0;i<n1;i++)left[i]=arr[l+i]; for(int i=0;i<n2;i++)right[i]=arr[mid+1+i]; int i=0,j=0,k=l; while(i<n1&& j<n2) { if(left[i]<right[j]) { arr[k]=left[i]; i++; k++; } else { arr[k]=right[j]; j++;k++; } } while(i<n1) { arr[k]=left[i] ; i++; k++; } while(j<n2) { arr[k]=right[j]; j++; k++; } } private static void mergesort(int l, int h, long []arr) { if(l<h) { int mid=l+(h-l)/2; mergesort(l,mid,arr); mergesort(mid+1,h,arr); merge(l,mid,h,arr); return; } } private static int upper(int l, int h,int target,ArrayList <Integer>arr,boolean[] visited) { while(l<h) { int mid= l+(h-l)/2; if(arr.get(mid)>=target) { h=mid; } else { l=mid+1; } } return l; } /*private static int bsearch(int l, int h, int [] arr) { while(l<h) { int mid=l+(h-l)/2; if( (mid)) } }*/ private static int bsearch(ArrayList<Integer>arr, int target) { int l=0; int h=arr.size()-1; while(l<h) { int mid= l+(h-l)/2; if(arr.get(mid)>target) { h=mid; } else { l=mid+1; } } return l; } static class Node { int a; Node next; Node(int a) { this.a=a; } } // static void rec(SortedSet<Integer> set,int currpos,int k) // { // if(set.size()==1){ System.out.println(set.last());return;} // // // // }} static class Point { int x,y=0; Point(int a, int b) { this.x=a; this.y=b; } } static int bfs(ArrayList<String []>maze,boolean[][]visited,Point start, int r, int c) { Queue<Point> q= new LinkedList<>(); q.add(start); int level=0; while(!q.isEmpty()) { int m=q.size(); while(m-->0) { Point curr=q.poll(); if(curr.x>=r||curr.x<0||curr.y>=c||curr.y<0||visited[curr.x][curr.y]||maze.get(curr.x)[curr.y].equals("X"))continue; if(maze.get(curr.x)[curr.y].equals("M"))return level; q.add(new Point(curr.x +1,curr.y)); q.add(new Point(curr.x -1,curr.y)); q.add(new Point(curr.x ,curr.y+1)); q.add(new Point(curr.x ,curr.y-1)); visited[curr.x][curr.y]=true; } level++; } return -1; } private static boolean res(long time, long prod, long[] arr) { long tot=0; for(long i : arr)tot+=time/i; if(prod<=tot)return true; else return false; } static class Pair { int start; int end ; Pair(int a , int b){ this.start=a; this.end=b; } } static int count=0; static int minRange(int [][]dp, int arr[], int a, int b) { int len= b-a+1; int k=0; while((1<<k)<=len)k++; k--; return Math.min(arr[dp[a][k]],arr[dp[b-(1<<k)+1][k]]); } static int [][] sparseTable(int [] arr, int n) { int dp[][]= new int[n][50]; for(int i=0;i<n;i++)dp[i][0]=i; for(int j=1;(1<<j)<=n;j++) { for(int i=0;(i+(1<<j)-1)<n;i++) { if(arr[dp[i][j-1]]<arr[dp[i+(1<<(j-1))][j-1]]) { dp[i][j]=dp[i][j-1]; } else { dp[i][j]=dp[i+(1<<(j-1))][j-1]; } } } return dp; } static void RobinCarp(char[] pat, char[]str, int prime) { long p=31l; long pow=p; long mod=1000000007l; int n=str.length; long []dp= new long[n]; long dhv2=pat[0]-'a'+1; for(int i=1;i<pat.length;i++) { dhv2=(dhv2+ (pat[i]-'a'+1)*pow)%mod; pow=(pow*p)%mod; } long pa[]=new long[str.length]; dp[0]=str[0]-'a'+1; pa[0]=1; pow=p; for(int i=1;i<str.length;i++) { dp[i]=((dp[i-1]+(str[i]-'a'+1)*pow)%mod); pa[i]=pow; pow=(pow*p)%mod; } int sp=0; int end=pat.length-1; ArrayList<Integer> ans= new ArrayList<>(); while(end<str.length) { long rsum=dp[end]; if(sp>0) rsum=(rsum-dp[sp-1]+mod)%mod; if(rsum==(dhv2*pa[sp])%mod)ans.add(sp+1); sp++; end++; } System.out.println(ans.size()); } // private static bsearch(int i, int j , int a, int b, char[] arr, long dp[], long pow) // { // int l= // } // int minimumDeviation(int[] nums) // { // // PriorityQueue<Integer>maxH= new PriorityQueue<Integer>((o1,o2)->o2-o1); // int min= Integer.MAX_VALUE; // for( int i: nums) // { // if(i%2==1)i=2*i; // min=Math.min(min,i); // maxH.add(i); // // } // // while(maxH.peek()%2==0) // { // int a =maxH.poll(); // min=Math.min(min,a/2); // maxH.add(a/2); // } // return maxH.peek()-min; // // } private static ArrayList<ArrayList<Long>> make_subset(long[] fact, int n) { ArrayList<ArrayList<Long>> ret= new ArrayList<ArrayList<Long>>(); for(int i=0;i<(1<<n);i++) { ArrayList<Long> temp= new ArrayList<>(); long sum=0; for(int j=0;j<15;j++) { if((i&(1<<j))>0){temp.add(fact[j]);sum+=fact[j];} } temp.add(sum); ret.add(temp); } return ret; } private static int count_set_bit(long n) { int count =0; while(n>0) { count+= n&1; n=n>>1; } return count; } public static void main(String[] args) throws IOException { StringBuilder str= new StringBuilder(); long fact[]= new long[15]; fact[0]=1l; fact[1]=1l; for(int i=2;i<15;i++)fact[i]=fact[i-1]*i; // long arr[]= new long [3]; // arr[0]=0; // arr[1]=1; // arr[2]=2; // ArrayList<ArrayList<Long>>tt=(make_subset(arr,3)); // for(ArrayList<Long> temp: tt) // { // System.out.println(temp.toString()); // } // System.out.println(count_set_bit(6l)); // return; ArrayList<ArrayList<Long>> ret= make_subset(fact, 15); long n= in.nextLong(); while(n-->0) { int k=Integer.MAX_VALUE; long num= in.nextLong(); for(int i=0;i< ret.size();i++) { int el_fact=ret.get(i).size()-1; long sum_fact=ret.get(i).get(el_fact); long remain=num-sum_fact; if(remain>=0){ int el_power= count_set_bit(remain); // if(el_power+el_fact==1){ // System.out.println(el_fact); // System.out.println(sum_fact); // System.out.println(el_power);} k=Math.min(k,el_power+el_fact);} } if(k==Integer.MAX_VALUE)str.append(-1); else str.append(k); str.append('\n'); } System.out.println(str); } } class Nodee { boolean isEnd; Nodee[] arr=new Nodee [26]; } class Trie { final Nodee root; Trie() { root=new Nodee(); } void insert(String s) { Nodee curr=root; for(int i=0;i<s.length();i++) { if(curr.arr[s.charAt(i)-'a']==null)curr.arr[s.charAt(i)-'a']=new Nodee(); curr=curr.arr[s.charAt(i)-'a']; } curr.isEnd=true; } boolean search(String word) { Nodee curr=root; for(int i=0;i<word.length();i++) { char ch= word.charAt(i); if(curr.arr[ch-'a']==null)return false; curr=curr.arr[ch-'a']; } return curr.isEnd; } private boolean start(String word) { Nodee curr=root; for(int i=0;i<word.length();i++) { char ch= word.charAt(i); if(curr.arr[ch-'a']==null)return false; curr=curr.arr[ch-'a']; } return true; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
42c3765bd9da570c21ed9e8af3c9933e
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.TreeMap; public class C { private static HashMap<Long, Integer> repository; public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); preprocess(); while (t-- > 0) { long N = Long.parseLong(br.readLine().trim()); sb.append(solve(N)).append("\n"); } br.close(); System.out.println(sb); } private static void preprocess() { final long MAX = (long) 1e12; long start = 6; repository = new HashMap<>(); for (int i = 4; start < MAX; i++) { HashMap<Long, Integer> temp = new HashMap<>(); for (long x : repository.keySet()) { long key = x + start; temp.put(key, Math.min(repository.getOrDefault(key, 100), repository.get(x) + 1)); } for (long x : temp.keySet()) repository.put(x, Math.min(temp.get(x), repository.getOrDefault(x, 100))); repository.put(start, 1); start *= i; } // TreeMap<Long, Integer> map = new TreeMap<>(repository); // System.out.println(map); } private static int solve(Long N) { int ret = Long.bitCount(N); for (long i : repository.keySet()) { if (i <= N) ret = Math.min(ret, repository.get(i) + Long.bitCount(N - i)); } return ret; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
8313b7591b183a73e3f969006aefe2dc
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class C_Factorials_and_Powers_of_Two{ static final int MOD = (int) 1e9 + 7; public static void main (String[] args){ FastReader s = new FastReader(); int t=1;t=s.ni(); for(int test=1;test<=t;test++){ long n=s.nl(); List<Long> factorials = new ArrayList<>(); long prod = 2; for (int i = 3; true; i++) { prod *= i; factorials.add(prod); if (prod > n) break; } int ans = Integer.MAX_VALUE; for (int mask = 0; mask < (1 << factorials.size()); mask++) { long sum = n; for (int j = 0; j < factorials.size(); j++) { if ((mask & (1 << j)) != 0) { sum -= factorials.get(j); } } ans = Math.min(ans, Integer.bitCount(mask) + Long.bitCount(sum)); } System.out.println(ans); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in));} int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } String next(){ while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e){e.printStackTrace();}}return st.nextToken();} String nextLine(){String str = "";try {str = br.readLine();}catch (IOException e) {e.printStackTrace();}return str;} } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
44105333ceb141792789b601a45458df
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class JoinSet { int[] fa; JoinSet(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int mod = (int)998244353; static int[][] dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; static boolean[] prime = new boolean[1000]; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i +""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static int max(int a, int b) { return Math.max(a,b); } static int min(int a, int b) { return Math.min(a,b); } static long max(long a, long b) { return Math.max(a,b); } static long min(long a, long b) { return Math.min(a,b); } static int max(int[] a) { int max = a[0]; for(int i : a) max = max(max,i); return max; } static int min(int[] a) { int min = a[0]; for(int i : a) min = min(min,i); return min; } static long max(long[] a) { long max = a[0]; for(long i : a) max = max(max,i); return max; } static long min(long[] a) { long min = a[0]; for(long i : a) min = min(min,i); return min; } public static void main(String[] args) throws Exception { int t = get(); while (t-- > 0){ long n = getx(); List<Long> list = new ArrayList<>(); long x = 6, k = 4; while(x <= n){ list.add(x); x *= k; k++; } int ans = Long.bitCount(n); for (int i = 0; i < (1<<12); i++){ long n2 = n; int len = list.size(); for(int j = 0;j < len;j++){ if(((i>>j)&1) == 1){ n2 -= list.get(j); } } if(n2 >= 0){ ans = min(ans,Integer.bitCount(i)+Long.bitCount(n2)); } } print(ans); } bw.flush(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7d89725287163f643a1a07808f45311b
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class new1{ static long mod = 1000000007; public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void main(String[] args) throws IOException{ BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader s = new FastReader(); int t = s.nextInt(); for(int z = 0; z < t; z++) { long n = s.nextLong(); long[] fact = new long[16]; fact[0] = 1; for(int i = 1; i < 16; i++) { fact[i] = fact[i - 1] * i; } int min = Integer.MAX_VALUE; for(int i = 0; i < (int) Math.pow(2, 14); i++) { long aa = 0; int count = 0; long a1 = i; int j = 1; while(a1 > 0) { if(a1 % 2 == 1) { aa = aa + fact[j]; count++; } a1 = a1 / 2; j++; } long exp = n - aa; //if(i == 4) System.out.println(exp + " " + aa + " " + count); if(exp > 0) { while(exp > 0) { if(exp % 2 == 1) { count++; } exp = exp / 2; } // if(i == 0) System.out.println(count); } else if(exp < 0) count = Integer.MAX_VALUE; min = Math.min(min, count); //if(count == 1) System.out.println(count + " " + i); } output.write(min + "\n"); } output.flush(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }}
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
68a6aa75970eba185f7614b1098fa472
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Integer.numberOfLeadingZeros; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; public class Main { public static long[] fact = new long[20]; public static long[] took = new long[20]; public static long ans = 100; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int t = nextInt(); fact[0] = 1; for (int i = 1; i <= 19; i++) { fact[i] = fact[i-1] * i; } while (t-- > 0) { ans = 100; long n = nextLong(); for (int i = 0; i <= 15; i++) { perm(n, 0, i, 0, 0); } out.println(ans); } in.close(); out.close(); } public static int numOfOnes(long a) { int ans = 0; while (a > 0) { ans += (a % 2); a /= 2; } return ans; } public static void perm(long n, int start, int k, int sum, long sumFact) { if (sum == k) { if (sumFact <= n) { ans = Math.min(ans, k + numOfOnes(n - sumFact)); } return; } for (int i = start; i <= 15; i++) { if (took[i] == 0 && fact[i] + sumFact <= n) { took[i] = 1; perm(n, i + 1, k, sum + 1, sumFact + fact[i]); took[i] = 0; } } } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } } class Interval { int xL, xR, yL, yR, num; public Interval(int xL, int xR, int yL, int yR, int num) { this.xL = xL; this.xR = xR; this.yL = yL; this.yR = yR; this.num = num; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
281e553f5409998c5bbb2f6b0e6356ca
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class Q1646C { static int mod = (int) (1e9 + 7); static void solve() { long fact = 1; long n = l(); ArrayList<Long> factorials = new ArrayList<>(); factorials.add((long) 1); for (int i = 1; i <= 14; i++) { fact *= i; factorials.add(fact); } int ans = Integer.MAX_VALUE; for (int i = 0; i < (1 << factorials.size()); i++) { long sum = 0; int count = 0; for (int j = 0; j < factorials.size(); j++) { if ((i & (1 << j)) != 0) { sum += factorials.get(j); count++; } } if (sum > n) { continue; } long rem = n - sum; if (((i & 1) != 0 && (rem & 1) != 0) || ((i & 2) != 0 && (rem & 2) != 0)) { continue; } // System.out.println(rem); ans = Math.min(ans, count + helper(rem)); } System.out.println(ans); return; } public static int helper(long val) { int ans = 0; while (val > 0) { ans++; val = val & (val - 1); } return ans; } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } } // ----->segmentTree--> segTree as class // ----->lazy_Seg_tree --> lazy_Seg_tree as class // -----> Trie --->Trie as class // ----->fenwick_Tree ---> fenwick_Tree // -----> POWER ---> long power(long x, long y) <---- // -----> LCM ---> long lcm(long x, long y) <---- // -----> GCD ---> long gcd(long x, long y) <---- // -----> SIEVE --> ArrayList<Integer> sieve(int N) <----- // -----> NCR ---> long ncr(int n, int r) <---- // -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <---- // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int // parent)<--- // ---> NODETOROOT --> ArrayList<Integer> // node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind) // <-- // ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int // child, int parent,int[]level,int currLevel) <-- // ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <--- // ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <--- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
91087f87fde4a6b54e787284ddee7b33
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static int bits(long n) { int c=0; while(n>0) { if(n%2!=0)c++; n=n/2; } return c; } public static void main(String[] args) { int len = obj.nextInt(); while (len-- != 0) { long n = obj.nextLong(); long ans = bits(n); Vector<Long> fact = new Vector<>(); long val = 1; for (int i = 2; val<=n; i++) { fact.add(val); val *= i; } for(int i=0;i<(1<<fact.size());i++) { long gg=0,cc=0; for(int j=0;j<=fact.size();j++) { int check=(1<<j)&i; if(check!=0) { gg+=fact.get(j); cc++; } } if(gg<=n)ans=Math.min(ans,cc+bits(n-gg)); } out.println(ans); } out.flush(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
82bd8beedf240fd67a74ee50c039fa31
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[]args){ long s = System.currentTimeMillis(); new Solver().run(); System.err.println(System.currentTimeMillis()-s+"ms"); } } class Solver{ final long mod = (long)1e9+7l; final boolean DEBUG = true, MULTIPLE_TC = true; final int MASK_LT = 32_768, oo = (int)1e8; FastReader sc; PrintWriter out; long N, factorial[]; void init(){ N = nl(); } int calcCost(int mask){ long n = N; int cost = 0; int pow2 = 1; for(int i = 0; i < 15; i++){ if((mask & 1) == 1){ cost += 1; n -= factorial[i]; if(n < 0){ return oo; } } mask /= 2; } cost += Long.bitCount(n); return cost; } void calcFactorials(int upTill){ factorial = new long[upTill + 1]; factorial[0] = 1l; for(int i = 1; i <= upTill; i++){ factorial[i] = factorial[i - 1] * (i * 1l); } } void process(int testNumber){ init(); int res = oo; for(int mask = 0; mask < MASK_LT; mask++){ int val = calcCost(mask); res = Math.min(res, val); } pn(res); } void run(){ calcFactorials(14); sc = new FastReader(); out = new PrintWriter(System.out); int t = MULTIPLE_TC ? ni() : 1; for(int test = 1; test <= t; test++){ process(test); } out.flush(); } void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); }; void pn(Object o){ out.println(o); } void p(Object o){ out.print(o); } int ni(){ return Integer.parseInt(sc.next()); } long nl(){ return Long.parseLong(sc.next()); } double nd(){ return Double.parseDouble(sc.next()); } String nln(){ return sc.nextLine(); } long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); } class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class pair implements Comparable<pair> { int first, second; public pair(int first, int second){ this.first = first; this.second = second; } @Override public int compareTo(pair ob){ if(this.first != ob.first) return this.first - ob.first; return this.second - ob.second; } @Override public String toString(){ return this.first + " " + this.second; } static public pair from(int f, int s){ return new pair(f, s); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
fc98c2ec66c270791423c73a966c5e24
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
// package practice; import java.io.*; import java.util.*; public class cp { public static int logbase2(double n) { int count = 0; while(n >= 2) { n /= 2; count++; } return count; } public static void clear(int arr[]) { for(int i = 0;i < arr.length;i++) { arr[i] = 0; } } public static void main(String[] args) throws IOException { //Your Solve // Reader s = new Reader(); FastReader s = new FastReader(); // Scanner s = new Scanner(System.in); int t = s.nextInt(); ArrayList<Long> list = new ArrayList<>(); long num = 6; for(long i = 4;num <= 1e12;i++) { list.add(num); num *= i; } // System.out.println(list); ArrayList<Pair> sumOfPerm = new ArrayList<>(); sumAllPerm(0, 0,sumOfPerm,list,0); Collections.sort(sumOfPerm, new Comparator<Pair>() { @Override public int compare(final Pair o1, final Pair o2) { // TODO: implement your logic here return o1.sum > o2.sum ? 1 : -1; } }); // for(int i = 0;i < 100;i++) { // System.out.println(sumOfPerm.get(i).sum); // } for(int p = 0;p < t;p++) { long n = s.nextLong(); long ans = Integer.MAX_VALUE; for(int i = 0;i < sumOfPerm.size();i++) { if(sumOfPerm.get(i).sum>n) { break; } String binary = Long.toBinaryString(n-sumOfPerm.get(i).sum); int count1 = 0; for(int j = 0;j < binary.length();j++) { if(binary.charAt(j) == '1') { count1++; } } long smallAns = sumOfPerm.get(i).count + count1; ans = Math.min(ans, smallAns); } System.out.println(ans); } } public static void sumAllPerm(long sumTillNow,int start,ArrayList<Pair> sumOfPerm,ArrayList<Long> list,int count){ if(start == list.size()) { sumOfPerm.add(new Pair(count,sumTillNow)); return; } sumAllPerm(sumTillNow,start+1,sumOfPerm,list,count); sumAllPerm(sumTillNow+list.get(start), start+1, sumOfPerm, list,count+1); } public static int binarySearch(ArrayList<Integer> arr, int target) { int s = 0,e = arr.size()-1; int mid = (s+e)/2; int ans = -1; while(s<=e) { mid = (s+e)/2; if(arr.get(mid)<=target) { ans = mid; s = mid+1; }else{ e = mid-1; } } return ans; } public static Vector<Integer> sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } Vector<Integer> v = new Vector<>(); // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) v.add(i); } return v; } static long binomialCoeff(long n, long k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = new Random(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } /* Iterative Function to calculate (x^y) in O(log y) */ static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long getPairsCount(int n, long sum,long arr[]) { HashMap<Long, Integer> hm = new HashMap<>(); // Store counts of all elements in map hm for (int i = 0; i < n; i++) { // initializing value to 0, if key not found if (!hm.containsKey(arr[i])) hm.put(arr[i], 0); hm.put(arr[i], hm.get(arr[i]) + 1); } long twice_count = 0; // iterate through each element and increment the // count (Notice that every pair is counted twice) for (int i = 0; i < n; i++) { if (hm.get(sum - arr[i]) != null) twice_count += hm.get(sum - arr[i]); // if (arr[i], arr[i]) pair satisfies the // condition, then we need to ensure that the // count is decreased by one such that the // (arr[i], arr[i]) pair is not considered if (sum - arr[i] == arr[i]) twice_count--; } // return the half of twice_count return twice_count / 2; } public static<T,V> HashMap<T,V> sortByValue(HashMap<T,V> hm) { // Create a list from elements of HashMap List<Map.Entry<T,V> > list = new LinkedList<Map.Entry<T,V> >( hm.entrySet()); // Sort the list using lambda expression Collections.sort( list, (i1, i2) -> ((String) i1.getValue()).compareTo((String) i2.getValue())); // put data from sorted list to hashmap HashMap<T,V> temp = new LinkedHashMap<T,V>(); for (Map.Entry<T,V> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static<T> HashMap<T,Integer> sortByValueDescending(HashMap<T,Integer> unSortedMap) { LinkedHashMap<T,Integer> reverseSortedMap = new LinkedHashMap<>(); unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue())); return reverseSortedMap; } static int lower_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array[(int)mid]) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] < key) { low++; } // Returning the lower_bound index return (int)low; } static int upper_bound(long array[], long key,long start,long end) { // Initialize starting index and // ending index long low = start, high = end; long mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key >= array[(int)mid]) { low = mid+1; } // If key is greater than array[mid], // then find in right subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < end && array[(int)low] <= key) { low++; } // Returning the lower_bound index return (int)low; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } } class Pair{ int count; long sum; Pair(int count,long sum) { this.count = count; this.sum = sum; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
6c246e7fb9340e0e5f81c882037910b2
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main{ static List<Long> list=new ArrayList<>(); static void init(){ long res=1; long cnt=1; while (res<=1e12){ res*=cnt; list.add(res); cnt++; } } static int count(long num){ return Long.bitCount(num); } static void dfs(int u,long x,long num){ if(u>=list.size()||x<=0) return; if(x-list.get(u)>=0){ ans=Math.min(ans,num+1+count(x-list.get(u))); dfs(u+1,x-list.get(u),num+1); } ans=Math.min(ans,num+count(x)); dfs(u+1,x,num); } static long ans=1<<30; static void solve() { ans=1<<30; dfs(0,in.nextLong(),0); out.println(ans); } public static void main(String[] args) throws Exception { int cnt=in.nextInt(); init(); while (cnt-->0){ solve(); } out.flush(); } static class FastReader{ StringTokenizer st; BufferedReader br; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st==null||!st.hasMoreElements()){ try { st=new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
b9ab989b3c073763260656d1f538dce4
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author 邶风 * @data 2022/3/4 23:55 */ public class Main { static class FastReader{ StringTokenizer st; BufferedReader br; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st==null||!st.hasMoreElements()){ try { st=new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Set<Long> set=new TreeSet<>(); static void init(){ long res=1; int n=2; while (res<=1e12){ set.add(res); res=res*n; n++; } // res=4; // while (res<=1e12){ // set.add(res); // res*=2; // } for(long v:set){ list.add(v); } } static List<Long> list=new ArrayList<>(); static int ans; static void dfs(int u,long x,int num){ if(x==0||u>=list.size()){ return; } if(x-list.get(u)>=0){ ans=Math.min(num +count(x-list.get(u)),ans); dfs(u+1,x-list.get(u),num+1); } ans=Math.min(num-1+count(x),ans); dfs(u+1,x,num); } static int count(long x){ int res=0; while (x!=0){ if((x&1)==1) res++; x/=2; } return res; } static void solve(){ long n=in.nextLong(); ans=1<<30; dfs(0,n,1); out.println(ans); } public static void main(String[] args) { int t=in.nextInt(); init(); while (t-->0){ solve(); out.flush(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
8663b9dd32f3307efb645c278b8ee483
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class CodeForces { public static void main(String[] args) throws Exception { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(s.readLine()); int t = Integer.parseInt(st.nextToken()); long[] factorials = new long[12]; factorials[0] = 6; for (int i = 1; i < 12; i++) { factorials[i] = (i + 3) * factorials[i - 1]; } HashSet<Value> choices = new HashSet<Value>(); for (int i = 0; i < (1 << 12); i++) { long temp = 0; for (int x = 0; x < 12; x++) { if (((1 << x) & i) != 0) { temp += factorials[x]; } } choices.add(new Value(temp, Long.bitCount(i))); } for (int i = 0; i < t; i++) { st = new StringTokenizer(s.readLine()); long n = Long.parseLong(st.nextToken()); long ans = Long.MAX_VALUE; for (Value x: choices) { long rem = n - x.n; if (rem < 0) { continue; } ans = Math.min(ans, x.count + Long.bitCount(rem)); } w.println(ans); } w.close(); } public static class Value { long n; int count; public Value(long n, int count) { this.n = n; this.count = count; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
d70a066a5a090ede9f708704057b9939
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF_774 { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100); private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100); private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; private static final long[] fact_arr = new long[15]; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); fact_arr[0] = 1; for (int i = 1; i <= 14; i++) fact_arr[i] = (i * fact_arr[i - 1]); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static long n; private static Set<Long> set; private static void solve() { n = fs.nextLong(); set = new HashSet<>(); int result = util(1, 0); fw.out.println(result); } private static int util(int i, long sum) { if (sum > n) return iMax; if (i >= 15) { long left = n - sum; int cnt = 0; for (int bit = 0; bit < 64; bit++) { if (((left >> bit) & 1) == 1) { cnt++; if (set.contains((1L << bit))) return iMax; } } return (set.size() + cnt); } set.add(fact_arr[i]); int ans1 = util(i + 1, sum + fact_arr[i]); set.remove(fact_arr[i]); int ans2 = util(i + 1, sum); return Math.min(ans1, ans2); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static <T> void randomizeArr(T[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swap(arr, i, j); } } private static Integer[] readIntArray(int n) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static <T> void swap(T[] arr, int i, int j) { T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static <T> void displayArr(T[] arr) { for (T x : arr) fw.out.print(x + " "); fw.out.println(); } private static <T> void displayList(List<T> list) { for (T x : list) fw.out.print(x + " "); fw.out.println(); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
ea0c6065a6739df60232009d8c91526c
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF_774 { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100); private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100); private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; private static final long[] fact_arr = new long[15]; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); fact_arr[0] = 1; for (int i = 1; i <= 14; i++) fact_arr[i] = (i * fact_arr[i - 1]); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static long n; private static Set<Long> set; private static void solve() { n = fs.nextLong(); set = new HashSet<>(); long result = util(1, 0); fw.out.println(result); } private static long util(int i, long sum) { if (sum > n) return iMax; if (i >= 15) { long left = n - sum; List<Long> setBit_nums = new ArrayList<>(); for (int bit = 0; bit < 64; bit++) { if (((left >> bit) & 1) == 1) setBit_nums.add((1L << bit)); } for (long x : setBit_nums) { if (set.contains(x)) return iMax; } return (set.size() + setBit_nums.size()); } set.add(fact_arr[i]); long ans1 = util(i + 1, sum + fact_arr[i]); set.remove(fact_arr[i]); long ans2 = util(i + 1, sum); return Math.min(ans1, ans2); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static <T> void randomizeArr(T[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swap(arr, i, j); } } private static Integer[] readIntArray(int n) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static <T> void swap(T[] arr, int i, int j) { T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static <T> void displayArr(T[] arr) { for (T x : arr) fw.out.print(x + " "); fw.out.println(); } private static <T> void displayList(List<T> list) { for (T x : list) fw.out.print(x + " "); fw.out.println(); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
573330c1b95b8fb97df60d3bfff08094
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF_774 { static FastScanner fs; static FastWriter fw; static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null; private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100); private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100); private static final int mod1 = (int) (1e9 + 7); private static final int mod2 = 998244353; private static final long[] fact_arr = new long[15]; public static void main(String[] args) throws IOException { fs = new FastScanner(); fw = new FastWriter(); fact_arr[0] = 1; for (int i = 1; i <= 14; i++) fact_arr[i] = (i * fact_arr[i - 1]); int t = 1; t = fs.nextInt(); while (t-- > 0) { solve(); } fw.out.close(); } private static long n; private static void solve() { n = fs.nextLong(); long result = util(1, 0, new HashSet<>()); fw.out.println(result); } private static long util(int i, long sum, Set<Long> set) { if (sum > n) return iMax; if (i >= 15) { long left = n - sum; List<Long> setBit_nums = new ArrayList<>(); for (int bit = 0; bit < 64; bit++) { if (((left >> bit) & 1) == 1) setBit_nums.add((1L << bit)); } for (long x : setBit_nums) { if (set.contains(x)) return iMax; } return (set.size() + setBit_nums.size()); } set.add(fact_arr[i]); long ans1 = util(i + 1, sum + fact_arr[i], set); set.remove(fact_arr[i]); long ans2 = util(i + 1, sum, set); return Math.min(ans1, ans2); } private static class UnionFind { private final int[] parent; private final int[] rank; UnionFind(int n) { parent = new int[n + 5]; rank = new int[n + 5]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 0; } } private int find(int i) { if (parent[i] == i) return i; return parent[i] = find(parent[i]); } private void union(int a, int b) { a = find(a); b = find(b); if (a != b) { if (rank[a] < rank[b]) { int temp = a; a = b; b = temp; } parent[b] = a; if (rank[a] == rank[b]) rank[a]++; } } } private static long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } private static long lcm(long a, long b) { return ((a * b) / gcd(a, b)); } private static long pow(long a, long b, int mod) { long result = 1; while (b > 0) { if ((b & 1L) == 1) { result = (result * a) % mod; } a = (a * a) % mod; b >>= 1; } return result; } private static long ceilDiv(long a, long b) { return ((a + b - 1) / b); } private static long getMin(long... args) { long min = lMax; for (long arg : args) min = Math.min(min, arg); return min; } private static long getMax(long... args) { long max = lMin; for (long arg : args) max = Math.max(max, arg); return max; } private static boolean isPalindrome(String s, int l, int r) { int i = l, j = r; while (j - i >= 1) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } private static List<Integer> primes(int n) { boolean[] primeArr = new boolean[n + 5]; Arrays.fill(primeArr, true); for (int i = 2; (i * i) <= n; i++) { if (primeArr[i]) { for (int j = i * i; j <= n; j += i) { primeArr[j] = false; } } } List<Integer> primeList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (primeArr[i]) primeList.add(i); } return primeList; } private static class Pair<U, V> { private final U first; private final V second; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { return Objects.hash(first, second); } @Override public String toString() { return "(" + first + ", " + second + ")"; } private Pair(U ff, V ss) { this.first = ff; this.second = ss; } } private static <T> void randomizeArr(T[] arr, int n) { Random r = new Random(); for (int i = (n - 1); i > 0; i--) { int j = r.nextInt(i + 1); swap(arr, i, j); } } private static Integer[] readIntArray(int n) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) arr[i] = fs.nextInt(); return arr; } private static List<Integer> readIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(fs.nextInt()); return list; } private static <T> void swap(T[] arr, int i, int j) { T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static <T> void displayArr(T[] arr) { for (T x : arr) fw.out.print(x + " "); fw.out.println(); } private static <T> void displayList(List<T> list) { for (T x : list) fw.out.print(x + " "); fw.out.println(); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws IOException { if (checkOnlineJudge) this.br = new BufferedReader(new FileReader("src/input.txt")); else this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } private static class FastWriter { PrintWriter out; FastWriter() throws IOException { if (checkOnlineJudge) out = new PrintWriter(new FileWriter("src/output.txt")); else out = new PrintWriter(System.out); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
7101b068f811cc1994e9c8bf70ac1cc4
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static int i() { return obj.nextInt(); } public static void main(String[] args) { int len = i(); while (len-- != 0) { long n=obj.nextLong(); Vector<Long> v=new Vector<>(); long fac=6; for(int i=4;true;i++) { if(fac>n)break; v.add(fac); fac=fac*i; } long ans=Integer.MAX_VALUE; for(int mask=0;mask<(1<<v.size());mask++) { long sum=n; for(int i=0;i<v.size();i++) if((mask&(1<<i))!=0)sum-=v.get(i); if(sum<0)continue; int tut=Integer.bitCount(mask)+Long.bitCount(sum); ans=Math.min(ans, tut); } out.println(ans); } out.flush(); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
db022c1721cc20ab3ffffe0b0b3b1eb6
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { long n = sc.nextLong(); if(n%2==1)out.println(1 + find(0,n-1)); else out.println(find(0,n)); } private static int find(int i,long n){ if(i==fact.length){ int cnt = 0; while (n>0){ if((n & 1) == 1)cnt++; n>>=1; } return cnt; } int c1 = 10000; if(n >=fact[i])c1 = 1 + find(i+1,n-fact[i]); int c2 = find(i+1,n); return Math.min(c1,c2); } static long[] fact; private static void pre(){ fact = new long[15]; fact[0] = 1; for(int i=1;i<15;i++)fact[i] = fact[i-1] * i; } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc. nextInt(); pre(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
93b911490d46ff1a86598d9e010f96a3
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { long n = sc.nextLong(); if(n%2==1)out.println(1 + find(0,n-1)); else out.println(find(0,n)); } private static int find(int i,long n){ if(i==fact.length){ int cnt = 0; while (n>0){ if(n % 2 ==1)cnt++; n>>=1; } return cnt; } int c1 = 10000; if(n >=fact[i])c1 = 1 + find(i+1,n-fact[i]); int c2 = find(i+1,n); return Math.min(c1,c2); } static long[] fact; private static void pre(){ fact = new long[15]; fact[0] = 1; for(int i=1;i<15;i++)fact[i] = fact[i-1] * i; } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc. nextInt(); pre(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
afafe1646c8e19cffc8186fab5691cc1
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { long[] fact = new long[15]; fact[0] = 1; for(int i=1;i<15;i++)fact[i] = fact[i-1] * i; long n = sc.nextLong(); if(n%2==1)out.println(1 + find(0,n-1,fact)); else out.println(find(0,n,fact)); } private static int find(int i,long n,long[] fact){ if(i==fact.length){ int cnt = 0; while (n>0){ if(n % 2 ==1)cnt++; n>>=1; } return cnt; } int c1 = 10000; if(n >=fact[i])c1 = 1 + find(i+1,n-fact[i],fact); int c2 = find(i+1,n,fact); return Math.min(c1,c2); } public static void main(String[] args) throws IOException { openIO(); int testCase = 1; testCase = sc. nextInt(); for (int i = 1; i <= testCase; i++) solve(i); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
fbb02f793029b811961c62d5467d15b7
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class B{ static int find(long num) { return Long.bitCount(num); } public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); long[] fact = new long[15]; fact[0] = 1; for(int i=1;i<=14;i++) { fact[i] = i * fact[i-1]; } int t = fs.nextInt(); for(int tt=0;tt<t;tt++) { long n = fs.nextLong(); int ans = find(n); for(int i=0;i<(1 << 15);i++) { int count = 0; long sum = 0; for(int pos=0;pos<=14;pos++) { if(((1<<pos) & i) != 0) { count += 1; sum += fact[pos]; } } if(sum <= n) { ans = Math.min(ans, count + find(n - sum)); } } out.println(ans); } out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } public static long[] sort(long[] arr) { List<Long> temp = new ArrayList(); for(long i:arr)temp.add(i); Collections.sort(temp); int start = 0; for(long i:temp)arr[start++]=i; return arr; } public static String rev(String str) { char[] arr = str.toCharArray(); char[] ret = new char[arr.length]; int start = arr.length-1; for(char i:arr)ret[start--] = i; String ret1 = ""; for(char ch:ret)ret1+=ch; return ret1; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
30645f409071a73a844679b856dcd70a
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//package factorialsandpowersoftwo; import java.util.*; import java.io.*; public class factorialsandpowersoftwo { public static void main(String[] args) throws IOException { BufferedReader fin = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(fin.readLine()); StringBuilder fout = new StringBuilder(); long[] f = new long[15]; long[] fSums = new long[(int) Math.pow(2, 15)]; int[] cnt = new int[(int) Math.pow(2, 15)]; f[0] = 1; for(int i = 1; i < 15; i++) { f[i] = f[i - 1] * (i + 1); } for(int i = 0; i < fSums.length; i++) { char[] c = Integer.toBinaryString(i).toCharArray(); for(int j = 1; j < 15; j++) { //j = 1 since we can't have 1 in our factorial if(c.length > j && c[j] == '1') { cnt[i] ++; fSums[i] += f[j]; } } } while(t-- > 0) { long n = Long.parseLong(fin.readLine()); int ans = Integer.MAX_VALUE; for(int i = 0; i < fSums.length; i++) { long next = n - fSums[i]; int curAns = cnt[i]; char[] c = Long.toBinaryString(next).toCharArray(); for(char ch : c) { if(ch == '1') { curAns ++; } } // if(ans > curAns) { // System.out.println(i + " " + curAns + " " + fSums[i] + " " + next); // System.out.println(String.valueOf(c)); // } ans = Math.min(ans, curAns); } fout.append(ans).append("\n"); } System.out.print(fout); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
454a339b7a0e0015182e8039b83f1f19
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.*; import java.util.*; public class codeforces { static class in { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { //System.out.println(" I WAS CALLED"); return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // static class AVLTreePBDS { private Node root; private boolean multi; // // AVLTreePBDS(boolean multi) { // this.root = null; // this.multi = multi; // } // // int size() { // return size(root); // } // // boolean isEmpty() { // return size(root) == 0; // } // // boolean contains(int key) { // return contains(root, key); // } // // void add(int key) { // root = add(root, key); // } // // void remove(int key) { // root = remove(root, key); // } // // Integer first() { // Node min = findMin(root); // return min != null ? min.key : null; // } // // Integer last() { // Node max = findMax(root); // return max != null ? max.key : null; // } // // Integer poolFirst() { // Node min = findMin(root); // if (min != null) { // remove(min.key); // return min.key; // } // return null; // } // // Integer poolLast() { // Node max = findMax(root); // if (max != null) { // remove(max.key); // return max.key; // } // return null; // } // // // min >= key // Integer ceiling(int key) { // return contains(key) ? key : higher(key); // } // // // max <= key // Integer floor(int key) { // return contains(key) ? key : lower(key); // } // // // min > key // Integer higher(int key) { // Node min = higher(root, key); // return min == null ? null : min.key; // } // // private Node higher(Node cur, int key) { // if (cur == null) // return null; // // if (cur.key <= key) // return higher(cur.right, key); // // // cur.key > key // Node left = higher(cur.left, key); // return left == null ? cur : left; // } // // // max < key // Integer lower(int key) { // Node max = lower(root, key); // return max == null ? null : max.key; // } // // private Node lower(Node cur, int key) { // if (cur == null) // return null; // // if (cur.key >= key) // return lower(cur.left, key); // // // cur.key < key // Node right = lower(cur.right, key); // return right == null ? cur : right; // } // // private class Node { // int key, height, size; // Node left, right; // // Node(int key) { // this.key = key; // height = size = 1; // left = right = null; // } // private void preOrder(Node root , StringBuilder ans) { // if(root == null) return; // if(root.left != null) preOrder(root.left,ans ); // ans.append(root.key+","); // if(root.right!=null) preOrder(root.right, ans); // } // public String toString() { // StringBuilder res = new StringBuilder(); // preOrder(root, res); // return "[" + String.valueOf(res.substring(0 , res.length()-1)) +"]" ; // } // } // // private int height(Node cur) { // return cur == null ? 0 : cur.height; // } // // private int balanceFactor(Node cur) { // return height(cur.right) - height(cur.left); // } // // private int size(Node cur) { // return cur == null ? 0 : cur.size; // } // // // fixVertex // private void fixHeightAndSize(Node cur) { // cur.height = Math.max(height(cur.left), height(cur.right)) + 1; // cur.size = size(cur.left) + size(cur.right) + 1; // } // // private Node rotateRight(Node cur) { // Node prevLeft = cur.left; // cur.left = prevLeft.right; // prevLeft.right = cur; // fixHeightAndSize(cur); // fixHeightAndSize(prevLeft); // return prevLeft; // } // // private Node rotateLeft(Node cur) { // Node prevRight = cur.right; // cur.right = prevRight.left; // prevRight.left = cur; // fixHeightAndSize(cur); // fixHeightAndSize(prevRight); // return prevRight; // } // // private Node balance(Node cur) { // fixHeightAndSize(cur); // if (balanceFactor(cur) == 2) { // if (balanceFactor(cur.right) < 0) // cur.right = rotateRight(cur.right); // return rotateLeft(cur); // } // if (balanceFactor(cur) == -2) { // if (balanceFactor(cur.left) > 0) // cur.left = rotateLeft(cur.left); // return rotateRight(cur); // } // return cur; // } // // private boolean contains(Node cur, int key) { // if (cur == null) // return false; // else if (key < cur.key) // return contains(cur.left, key); // else if (key > cur.key) // return contains(cur.right, key); // else // return true; // } // // private Node add(Node cur, int key) { // if (cur == null) // return new Node(key); // // if (key < cur.key) // cur.left = add(cur.left, key); // else if (key > cur.key || multi) // cur.right = add(cur.right, key); // // return balance(cur); // } // // private Node findMin(Node cur) { // return cur.left != null ? findMin(cur.left) : cur; // } // // private Node findMax(Node cur) { // return cur.right != null ? findMax(cur.right) : cur; // } // // private Node removeMin(Node cur) { // if (cur.left == null) // return cur.right; // // cur.left = removeMin(cur.left); // return balance(cur); // } // // private Node removeMax(Node cur) { // if (cur.right == null) // return cur.left; // // cur.right = removeMax(cur.right); // return balance(cur); // } // // private Node remove(Node cur, int key) { // if (cur == null) // return null; // // if (key < cur.key) // cur.left = remove(cur.left, key); // else if (key > cur.key) // cur.right = remove(cur.right, key); // else { // k == cur.key // Node prevLeft = cur.left; // Node prevRight = cur.right; // // if (prevRight == null) // return prevLeft; // // Node min = findMin(prevRight); // min.right = removeMin(prevRight); // min.left = prevLeft; // // return balance(min); // } // // return balance(cur); // } // // int orderOfKey(int key) { // return orderOfKey(root, key); // } // // // count < key // private int orderOfKey(Node cur, int key) { // if (cur == null) // return 0; // // if (cur.key < key) // return size(cur.left) + 1 + orderOfKey(cur.right, key); // // if(cur.key > key || (multi && cur.left!=null && cur.left.key == key)) // return orderOfKey(cur.left, key); //// cur.key == key // return size(cur.left); // // } // // Integer findByOrder(int pos) { // return size(root) > pos ? findByOrder(root, pos) : null; // } // // // get i-th // private int findByOrder(Node cur, int pos) { // if (size(cur.left) > pos) // return findByOrder(cur.left, pos); // // if (size(cur.left) == pos) // return cur.key; // // // size(cur.left) < pos // return findByOrder(cur.right, pos - 1 - size(cur.left)); // } // public String toString() { // return String.valueOf(this.root) ; // } // } static void merge(int[] arr, int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int[] L = new int[n1]; int[] R = new int[n2]; /*Copy data to temp arrays*/ System.arraycopy(arr, l, L, 0, n1); for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; // Initial index of merged subarray array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int[] arr, int l, int r) { if (l < r) { // Find the middle point int m =l+ (r-l)/2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void main(String[] args) throws IOException { int t =in.nextInt(); StringBuilder st = new StringBuilder(); long[] fact = new long[14]; fact[0]=1L; for(int i=1;i<14;i++)fact[i]=fact[i-1]*(i+1); //System.out.println(Arrays.toString(fact)); while (t>0){ long n= in.nextLong(); long ans= Integer.MAX_VALUE; long N = (long)Math.pow(2, fact.length); for(long mask=0;mask<N;mask++){ long temp=n; int count=0; for(int i=0;i<14;i++){ if((mask&(1L<<i))>0){ temp=temp-fact[13-i]; count++; } if(temp<=0)break; } if(temp>=0){ for(int i=0;i<63;i++){ if((temp&(1L<<i))>0)count++; } ans=Math.min(count,ans); } } if(ans==Integer.MAX_VALUE)st.append(-1); else st.append(ans); st.append('\n'); t--; } System.out.println(st); } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
44169cc22b26f35678c8bb5ded25d35f
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class C_Factorials_and_Powers_of_Two { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); static long m = 998244353; static ArrayList<Long> arr; static long ans ; public static void main (String[] args) throws java.lang.Exception { // ArrayList<Long> arr = sieveOfEratosthenes(10000); long t = sc.nextLong(); while(t-- >0) { long n = sc.nextLong(); arr = new ArrayList<>(); for(int i=3 ; i<=15 ; i++){ long xx = factorial(i); arr.add(xx); } // out.println(arr); ans=Long.MAX_VALUE; int z =0; for(int i=0 ; i<arr.size() ; i++) { if(arr.get(i)<=n) { z=i; } else break; } solve(n,0,z); out.println(ans); // 378404098055 } out.flush(); } static void solve(long n , long f , int j) { if(n<0) return; ans = Math.min(ans,(countSetBits(n)+f)); if(j<0) return ; solve(n,f,j-1); solve(n-arr.get((int) j),f+1,j-1); } static long countSetBits(long n) { long count=0; while(n>0) { if((n&1)>0) count++; n = n>>1; } return count; } static long factorial(long n) { if(n==0 || n==1) return 1; return factorial(n-1)*n; } static int[] reverseArray(int arr[]) { int arr1[] = new int[arr.length]; int j=0; for(int i=arr.length-1 ;i>=0 ; i--) { arr1[j] = arr[i]; j++; } return arr1; } static int maxSubArraySum(ArrayList<Integer> a) { int size = a.size(); int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a.get(i); if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } static void printArray(int[] arr1) { for(int i=0 ; i<arr1.length ; i++) { out.print(arr1[i]+" "); } out.println(); } public static class Pair { long a; long b; long c ; Pair(long a , long b , long c) { this.a=a; this.b=b; this.c=c; } } static class Sort implements Comparator<Pair> { // Method // Sorting in ascending order of roll number public int compare(Pair a, Pair b) { return (int) (a.a - b.a); } } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static ArrayList<Long> sieveOfEratosthenes(long n) { boolean[] prime = new boolean[(int) (n + 1)]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; if(1<=n) prime[1]=false; ArrayList<Long> arr = new ArrayList<>(); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { if(p!=2 && p!=3 ) arr.add((long) p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } return arr; } static int count(String s) { int count=0; for(int i=0 ; i<s.length() ; i++) { if(s.charAt(i)=='1' ) count++; } return count; } static long gcd(long a , long b) { if(b==0) return a; return gcd(b,a%b); } static long lcm(long a , long b) { return (a*b)/gcd(a,b); } static long fastPower(long a , long b , long n ) { long res=1; while(b>0) { if((b&1)!=0) { res = (res%n * a%n)%n; } a = (a%n * a%n)%n; b=b>>1; } return res; } static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } static long getFractionModulo(long a, long b) { long c = gcd(a, b); a = a / c; b = b / c; long d = modexp(b, m - 2); long ans = ((a % m) * (d % m)) % m; return ans; } public static long power(long x, long y) { long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return modMult(temp,temp); else { if (y > 0) return modMult(x,modMult(temp,temp)); else return (modMult(temp,temp)) / x; } } static long modMult(long a,long b) { return a*b%m; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
6cabeed954fa5dedc7c614f766047183
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef {static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int mod = 998244353 ; static int N = 200005; static long factorial_num_inv[] = new long[N+1]; static long natual_num_inv[] = new long[N+1]; static long fact[] = new long[N+1]; static void InverseofNumber() { natual_num_inv[0] = 1; natual_num_inv[1] = 1; for (int i = 2; i <= N; i++) natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod; } static void InverseofFactorial() { factorial_num_inv[0] = factorial_num_inv[1] = 1; for (int i = 2; i <= N; i++) factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod; } static long nCrModP(long N, long R) { long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod; return ans%mod; } static boolean prime[]; static void sieveOfEratosthenes(int n) { prime = new boolean[n+1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } } //static int distance[]; static long min = 100; static int size = 0; public static void main (String[] args) throws java.lang.Exception { // InverseofNumber(); // InverseofFactorial(); // fact[0] = 1; // for (long i = 1; i <= 2*100000; i++) // { // fact[(int)i] = (fact[(int)i - 1] * i) % mod; // } FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); List<Long> a = new ArrayList<Long>(); long x = 1; for(long i=1;i<=15;i++){ x = x*i; a.add(x); } size = a.size(); int t = scan.nextInt(); while(t-->0){ long n = scan.nextLong(); min = 100; fun(n,a,0,0,0); pw.println(min); pw.flush(); } } static void fun(long n,List<Long> a,int index,int count,long sum){ if(index==size){ long val = n-sum; if(val>=0){ long bits = bit_count(val); // System.out.println(val+" "+bits); if(bits+count<min) min = bits+count;} return; } fun(n,a,index+1,count,sum); fun(n,a,index+1,count+1,sum+a.get(index)); } static long bit_count(long val){ long x =1 ; long bits = 0; while(x<=val){ if((x&val)!=0) bits++; x = x<<1; } return bits; } //static long bin_exp_mod(long a,long n){ // long res = 1; // while(n!=0){ // if(n%2==1){ // res = ((res)*(a)); // } // n = n/2; // a = ((a)*(a)); // } // return res; //} //static long bin_exp_mod(long a,long n){ // long mod = 998244353; // long res = 1; // while(n!=0){ // if(n%2==1){ // res = ((res%mod)*(a%mod))%mod; // } // n = n/2; // a = ((a%mod)*(a%mod))%mod; // } // res = res%mod; // return res; //} // static long gcd(long a,long b){ // if(a==0) // return b; // return gcd(b%a,a); // } // static long lcm(long a,long b){ // return (a/gcd(a,b))*b; // } } class Pair{ int x,y; Pair(int x,int y){ this.x = x; this.y = y; } //public boolean equals(Object obj) { // // TODO Auto-generated method stub // if(obj instanceof Pair) // { // Pair temp = (Pair) obj; // if(this.x.equals(temp.x) && this.y.equals(temp.y)) // return true; // } // return false; //} //@Override //public int hashCode() { // // TODO Auto-generated method stub // return (this.x.hashCode() + this.y.hashCode()); //} } //class Compar implements Comparator<Pair>{ // public int compare(Pair p1,Pair p2){ // if(p1.y==p2.y) // return 0; // else if(p1.y<p2.y) // return -1; // else // return 1; // } //} //class DisjointSet{ // Map<Long,Node> map = new HashMap<>(); // class Node{ // long data; // Node parent; // int rank; // } // public void makeSet(long data){ // Node node = new Node(); // node.data = data; // node.parent = node; // node.rank = 0; // map.put(data,node); // } // //here we just need the rank of parent to be exact // public void union(long data1,long data2){ // Node node1 = map.get(data1); // Node node2 = map.get(data2); // Node parent1 = findSet(node1); // Node parent2 = findSet(node2); // if(parent1.data==parent2.data){ // return; // } // if(parent1.rank>=parent2.rank){ // parent1.rank = (parent1.rank==parent2.rank)?parent1.rank+1:parent1.rank; // parent2.parent = parent1; // } // else{ // parent1.parent = parent2; // } // } // public long findSet(long data){ // return findSet(map.get(data)).data; // } // private Node findSet(Node node){ // Node parent = node.parent; // if(parent==node){ // return parent; // } // node.parent = findSet(node.parent); // return node.parent; // } // }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
12552b7e3bd8636fe741719347840cd3
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static int min(int x, int y){return Math.min(x, y);} static int TEN(int x){if(x==0) return 1; return 10*TEN(x-1);} static int INF = TEN(9)+9; static int max(int x, int y){return Math.max(x, y);} static int abs(int x){return Math.abs(x);} static boolean Check(int N, int pos){ int x = (N&(1<<pos)); if(x>0) return true; return false;} static int Set(int N, int pos){return N|(1<<pos);} static int Toggle(int N, int pos){return N^(1<<pos);} static int Off(int N, int pos){if(Check(N,pos)==false) return N; return Toggle(N,pos);} static int in(){int a = sc.nextInt(); return a;} static void println(int n){System.out.println(n);} static void print(int n){System.out.print(n);} static void print(char c){System.out.print(c);} static void print(int arr[]){for(int a: arr) System.out.print(a + " "); System.out.println();} static ArrayList<Long>facts = new ArrayList<Long>(); static ArrayList<Long>sums = new ArrayList<Long>(); static long n; static int Cnt(Long x) { int ret = 0; while(x>0) { if(x%2==1) ret++; x/=2; } return ret; } static void Solve() { long i, j, k, l, m, x, y, w, p, q, t; n = sc.nextLong(); long ans = 1000000; int sz = facts.size(); for(int mask=0; mask<(1<<sz); mask++) { long sum = 0, cnt = 0; for(i=0; i<sz; i++) { if((mask&(1<<i))>0){ sum = sum+facts.get((int)i); cnt++; } } if(sum<=n)ans = Math.min(ans, cnt+Cnt(n-sum)); } System.out.println(ans); } public static void main(String []args) { int tc = sc.nextInt();int i, j; facts.add((long)6); for(i=4, j=1; i<=15 ; i++, j++) { facts.add(facts.get(j-1)*i); } //System.out.println(facts.get(i-1)); while(tc-->0) { Solve(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
68ffed2e949213da2e234907ed1e6202
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; // static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { long n = sc.nextLong(); long ans = Long.MAX_VALUE; ArrayList<Long> fact = new ArrayList<>(); long fact_pro = 6; for(int i=4;true;i++) { if(fact_pro > n) { break; } fact.add(fact_pro); fact_pro *= i; } for(int i=0;i<(1 << fact.size());i++) { long sum = 0; for(int j=0;j<fact.size();j++) { if((i & (1 << j)) != 0) { sum += fact.get(j); } } if(sum > n) { break; } long temp = Integer.bitCount(i); temp += Long.bitCount(n - sum); ans = Math.min(ans , temp); } System.out.println(ans); } } static long countSetBits(long n) { long count = 0; while (n > 0) { n &= (n - 1); count++; } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
849ab15a927a26f100e893e43cca1862
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner sc = new Scanner(System.in); public static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int t = sc.nextInt(); while( t > 0 ) { solve(); t--; } pw.flush(); } static void solve() { long N = sc.nextLong(); long[] f = new long[15]; int ans = 1<<10; f[0] = 1; for( int i = 1; i <= 14; i++ ) { f[i] = f[i-1]*(long)i; if( f[i-1] <= 2 ) f[i-1] = -1; } for( int S = 0; S < 1<<15; S++ ) { long sum = 0; boolean lower = true; for( int i = 0; i < 15; i++ ) { if( (S>>i)%2 == 1 && f[i] != -1 ) sum += f[i]; if( sum > N ) { lower = false; break; } } if( lower ) { int val = popcount(S)+popcount(N-sum); ans = Math.min(ans,val); } } pw.println(ans); } static int popcount(long N) { int res = 0; while( N > 0 ) { long d = N&1; res += (int)d; N >>= 1; } return res; } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
df3a281ed988b36d5756d86d57364735
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
// package practise; import java.io.*; import java.util.*; public class C { static HashMap<List<Long>,Integer> dp; // static int dp[]; static Reader sc; static PrintWriter w; public static void main(String[] args) throws IOException { sc = new Reader(); w = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { solve(); } w.close(); } static void solve() throws IOException { long n=sc.nextLong(); List<Long> factorials = new ArrayList<>(); long prod=6; for(int i=4;true;i++) { if(prod>n)break; factorials.add(prod); prod*=i; } int ans=Integer.MAX_VALUE; for(int mask=0;mask< (1<<factorials.size());mask++) { // 101 means chhose factorial.get(10+ fact[2]; long sum=0; for(int j=0;j<factorials.size();j++) { if( (mask & (1<<j))!=0) { sum+=factorials.get(j); } } if(sum<=n) { ans=Math.min(ans, Long.bitCount(mask)+Long.bitCount(n-sum)); } } w.println(ans); } static int countSetBits(long n) { int count = 0; while (n > 0L) { n &= (n - 1L); count++; } return count; } static class FenwickTree{ int tree[]; void contructFenwickTree(int ar[],int n) { tree= new int[n+1]; for(int i=0;i<ar.length;i++) { update(i,ar[i],n); } } void update(int i,int delta,int n) { //delta means newValue - preValue. //i is index of array. i++; while(i<=n) { tree[i]+=delta; i=i+Integer.lowestOneBit(i); } } int getSum(int i ){ int sum=0; i++; while(i>0) { sum+=tree[i]; i-=Integer.lowestOneBit(i); } return sum; } int rangeOfSum(int i,int j) { return getSum(j)-getSum(i-1); } } static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static void sort(long[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<>(); for(long x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } static class Reader // here reader class is defined. { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
82bda64000bc9234a9b5a5c3b69beeaf
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.List; import java.util.Collections; import java.io.DataInputStream; import java.io.FileInputStream; import java.util.Comparator; import java.util.Set; import java.util.ArrayDeque; import java.util.TreeMap; import java.util.Iterator; import java.util.HashSet; import java.util.Map; import java.util.HashMap; public class Master { private static FastScanner fs = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); // private static Kattio io = new Kattio(System.in, System.out); private static Reader io = new Reader(); public static void main(String[] args) throws IOException { int t = fs.nextInt(); outer:while(t-->0) { long n = fs.nextLong(); List<Long> list = new ArrayList<>(); long ans1 =1; for(int ad =1;true;ad++) { if(ans1>n) { break; } ans1 *=ad; list.add(ans1); } int ans =Long.bitCount(n); for(int mask=0;mask<(1<<list.size());mask++) { long sum =n; for(int i=0;i<32;i++) { if(((mask>>i)&1)==1) { sum -=(list.get(i)); } } if(sum<0) { continue; } ans = Math.min(ans,Long.bitCount(mask)+Long.bitCount(sum)); } System.out.println(ans); } } // 5 4 3 2 1 static final Random random=new Random(); static void ruffleSort(int [] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } int [] sort(int [] arr) { List<Integer> list = new ArrayList<>(); for(int i : arr) list.add(i); Collections.sort(list); int res[] = new int[arr.length]; for(int i=0;i<arr.length;i++) res[i] = list.get(i); return res; } void debug(int a) { System.out.println("This is var "+a); } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output
PASSED
34d6b9b258e40b512a205092472b0cf9
train_110.jsonl
1646408100
A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\cdot 2\cdot \ldots \cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.
256 megabytes
//import java.lang.reflect.Array; import java.util.*; import java.lang.reflect.Array; import javax.print.DocFlavor.STRING; import javax.swing.Popup; import javax.swing.plaf.synth.SynthStyleFactory; public class Simple{ public static class Pair implements Comparable<Pair>{ int val; int freq = 0; Pair prev ; Pair next; boolean bool = false; public Pair(int val,int freq){ this.val = val; this.freq= freq; } public int compareTo(Pair p){ // if(p.freq == this.freq){ // return this.val - p.freq; // }; // if(this.freq > p.freq)return -1; // return 1; return (p.freq-p.val) - (this.freq - this.val); } } public static long factorial(long n) { // single line to find factorial return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } static long m = 998244353; // Function to return the GCD of given numbers static long gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // Recursive function to return (x ^ n) % m static long modexp(long x, long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % m, n / 2); } else { return (x * modexp((x * x) % m, (n - 1) / 2) % m); } } // Function to return the fraction modulo mod // static long getFractionModulo(long a, long b) // { // long c = gcd(a, b); // a = a / c; // b = b / c; // // (b ^ m-2) % m // long d = modexp(b, m - 2); // // Final answer // long ans = ((a % m) * (d % m)) % m; // return ans; // } // public static long bs(long lo,long hi,long fact,long num){ // long help = num/fact; // long ans = hi; // while(lo<=hi){ // long mid = (lo+hi)/2; // if(mid/) // } // } // public static boolean isPrime(int n) // { // // Check if number is less than // // equal to 1 // if (n <= 1) // return false; // // Check if number is 2 // else if (n == 2) // return true; // // Check if n is a multiple of 2 // else if (n % 2 == 0) // return false; // // If not, then just check the odds // for (int i = 3; i <= sqrt(n); i += 2) // { // if (n % i == 0) // return false; // } // return true; // } // public static int countDigit(long n) // { // int count = 0; // while (n != 0) { // n = n / 10; // ++count; // } // return count; // } // static ArrayList<Long> al ; // static boolean checkperfectsquare(long n) // { // // If ceil and floor are equal // // the number is a perfect // // square // if (processesh.ceil((double)processesh.sqrt(n)) == // processesh.floor((double)processesh.sqrt(n))) // { // return true; // } // else // { // return false; // } // } public static void decToBinary(int n,int arr[],int j) { // Size of an integer is assumed to be 32 bits for (int i = 31; i >= 0; i--) { int k = n >> i; if ((k & 1) > 0){ arr[j]++; } j++; } } public static class Node{ //int u; long v; long w; public Node(long v,long w){ //this.u = u; this.v=v; this.w=w; } } public static boolean[] sieve(int n){ boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0] = false; isPrime[1] = false; for(int i=2;i*i<=n;i++){ if(!isPrime[i])continue; for(int j= i*2;j<=n;j=j+i){ isPrime[j] = false; } } return isPrime; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more // than or equal to p while (y > 0) { // If y is odd, multiply // x with result if ((y & 1) > 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // static int modInverse(int a, int p) // { // return power(a, p - 2, p); // } public static void bfs(ArrayList<ArrayList<Integer>> adj,boolean vis[],int dis[]){ vis[0] = true; Queue<Integer> q = new LinkedList<>(); q.add(0); int count = 0; while(!q.isEmpty()){ int size = q.size(); for(int j = 0;j<size;j++){ int poll = q.poll(); dis[poll] = count; vis[poll] = true; for(Integer x : adj.get(poll)){ if(!vis[x]){ q.add(x); } } } count++; } } // static void swap(int[] arr, int i, int j) // { // int temp = arr[i]; // arr[i] = arr[j]; // arr[j] = temp; // } public static boolean isSorted(int arr[],int[] copy,int n){ for(int i=0;i<n;i++){ if(arr[i]!=copy[i])return false; } return true; } public static class DSU{ int par[]; int rank[];//rank denotes the height of graph public DSU(int n){ par = new int[n]; for(int i=0;i<n;i++){ par[i] = i; } rank = new int[n]; } public int findPar(int node){ if(par[node]==node)return node; par[node] = findPar(par[node]); return par[node]; } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[v]>rank[u]){ par[u] = v; } else if(rank[v]<rank[u]){ par[v] = u; } else{ rank[u]++; par[v] = u; } } } public static long modFact(int n,int k) { long M = 998244353; long f = 1; for (int i = 1; i <= n; i++){ if(i==k+1)continue; f = (f%M*i%m) % M; // Now f never can } // exceed 10^9+7 return f; } public static class Item{ int val; int index; public Item(int val,int index){ this.val = val; this.index = index; } } public static void mergeSortCount(Item[] itemArr,int count[],int n,int i,int j){ if(i>=j)return; int mid = (i+j)/2; mergeSortCount(itemArr, count, n, i, mid); mergeSortCount(itemArr, count, n, mid+1, j); mergeCount(itemArr,count,i,mid+1,mid,j); } public static void mergeCount(Item[] itemArr,int count[],int i1,int i2,int j1,int j2){ int size = j2 - i1+1; Item[] sortedItems = new Item[size]; int index = 0; int i11 = i1; //int j11 = j2; int i22 = i2; //int j22 = j2; while(i11<=j1 && i22 <=j2 ){ if(itemArr[i11].val < itemArr[i22].val){ count[itemArr[i11].index]+= (j2-i22+1); sortedItems[index++] = itemArr[i11++]; } else{ sortedItems[index++] = itemArr[i22++]; } } while(i11<=j1){ sortedItems[index++] = itemArr[i11++]; } while(i22<=j2){ sortedItems[index++] = itemArr[i22++]; } index=0; for(int i=i1;i<=j2;i++){ itemArr[i] = sortedItems[index++]; } } static int countSetBits(long n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } public static void backtrck(int i,ArrayList<Set<Long>>al,int count,long something[],long curr){ if(i>=14){ return; } al.get(count).add(curr); //System.out.println(curr+" "+count); backtrck(i+1, al, count+1, something, curr+ something[i]); backtrck(i+1, al, count, something, curr); } public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ long n = s.nextLong(); int count = countSetBits(n); //long last = (long)Math.pow(10,12); //long x =3; long something[] = new long[16]; long sum = 6; long x = 4; ArrayList<Set<Long>> al = new ArrayList<>(); for(int i=0;i<=14;i++)al.add(new HashSet<>()); for(int i=0;i<14;i++){ something[i] = sum; sum*=x; x++; //System.out.println(sum); } backtrck(0, al, 0, something, (long)0); for(int u =1;u<=14;u++){ for(Long x1 : al.get(u)){ if(x1>n)continue; count = Math.min(count,u+countSetBits(n-x1)); //System.out.print(x1+" " ); } //System.out.println(); } System.out.println(count); } } } /* */
Java
["4\n\n7\n\n11\n\n240\n\n17179869184"]
3 seconds
["2\n3\n4\n1"]
NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$.
Java 11
standard input
[ "bitmasks", "brute force", "constructive algorithms", "dp", "math" ]
ff0b041d54755984df3706aae78d8ff2
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\le n\le 10^{12}$$$).
1,500
For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.
standard output