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 | ff8fb3ce9a9b2869232e44c361e22969 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | // ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
// ♚⭃ Author: 18jad ⥷♚
// ☬ Time: 30-03-2022 19:36:54 ☬
// ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while (t-- > 0) {
long n = cin.nextLong(), s = cin.nextLong();
PRINT(s / (n * n));
}
}
public static void PRINT(Object arg) {
System.out.println(arg);
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 0357b580f3c03001efed9ed32d685405 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
public class Square_Count{
public static void main(String args[]){
Scanner obj = new Scanner(System.in);
int t = obj.nextInt() ;
long s, n;
for( int i = 0; i < t; i ++){
n = obj.nextLong();
s = obj.nextLong();
System.out.println(s / (n*n));
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 2486c655623258db17139858435c5ff1 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.*;
import java.util.*;
public class SquareCounting {
public static PrintWriter out;
public static void main(String[] args)throws IOException{
Scanner sc=new Scanner();
out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
long n=sc.nextLong();
long s=sc.nextLong();
n*=n;
out.println(s/n);
}
out.close();
}
public static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
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 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 4feea87fc7be6141b733f4d402adcd17 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes |
import java.io.*;
import java.util.*;
public class GPL implements Runnable {
public static void main(String[] args) {
new Thread(new GPL()).run();
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public int lengthOfLIS(int[] nums) {
int[] tails = new int[nums.length];
int size = 0;
for (int x : nums) {
int i = 0, j = size;
while (i != j) {
int m = (i + j) / 2;
if (tails[m] < x)
i = m + 1;
else
j = m;
}
tails[i] = x;
if (i == size) ++size;
}
return size;
}
public int dfs(int i,ArrayList<ArrayList<Integer>>adj,boolean visited[])
{
if(adj.get(i).size()==1)
return 0;
int max=0;
for(int it:adj.get(i))
{
if(!visited[it])
{
visited[it]=true;
max=Math.max(max, 1+dfs(it,adj,visited));
}
}
return max;
}
public static int gcd(int a,int b) {if(a==0)return b; return gcd(b%a,a);};
public void solve() throws IOException {
long n=nextLong();
long sum=nextLong();
long ans=sum/(n*n);
out.println(ans);
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int test=nextInt();
while(test-->0)
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 3b8307a7fbba988a565478f9b23d1221 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 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();
OUTER:
while (T-->0) {
long n = in.nextLong(), s = in.nextLong();
out.append(s/(n*n)+"\n");
}
System.out.print(out);
}
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 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 1780d7b583b6fb9cca8dc434bb1a71a1 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class pre1 {
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;
}
}
public static void main(String args[]){
FastReader obj = new FastReader();
int tc = obj.nextInt();
while(tc--!=0){
long n = obj.nextLong(),k = obj.nextLong();
int count = 0;
System.out.println(k/(n*n));
}
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 7e2ff10b71485ad9f31c50bc4b96146b | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
import java.io.*;
public class Test {
static HashSet <String>[] likes;
static HashSet <String>[] dislikes;
static HashSet <String> Ingredients;
static int[] numLikes;
static int [] numDislikes;
static int n;
public static int lengthOfLongestSubstring(String s) {
HashMap <Character,Integer> pos = new HashMap<Character,Integer>();
int ans = 0;
int curr = 0;
int start = 0;
for(int i = 0;i<s.length();i++) {
if(!pos.containsKey(s.charAt(i)) || pos.get(s.charAt(i))<start) {
pos.put(s.charAt(i), i+1);
curr ++;
}
else {
ans = Math.max(ans, curr);
curr = (i+1)-pos.get(s.charAt(i));
start = pos.get(s.charAt(i))+1;
pos.replace(s.charAt(i), i+1);
}
}
ans = Math.max(ans, curr);
return ans;
}
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();
while(t-->0) {
long n = sc.nextLong();
long s = sc.nextLong();
System.out.println(s/(n*n*1l));
}}
static int score (String removed) {
int score = 0;
for (int i = 0;i<n;i++) {
int numlikes = 0;
boolean hasDislikes = false;
for (String ing : likes[i]) {
if (Ingredients.contains(ing) && !ing.contentEquals(removed)) {
numlikes ++;
}
}
for (String ing : dislikes[i]) {
if (Ingredients.contains(ing) && !ing.contentEquals(removed)) {
hasDislikes = true;
break;
}
}
if (numlikes == numLikes[i] && !hasDislikes) {
score ++;
}
}
return score;
}
static long lcm (long a, long b) {
return a*b /gcd(a, b);
}
static Pair extendedEuclid (long a , long b) {
if (b==0) {
return new Pair (1,0);
}
Pair temp = extendedEuclid(b, a%b);
return new Pair (temp.y,temp.x-(a/b)*temp.y);
}
static ArrayList<Integer> primes;
static ArrayList <Pair> primeFactors;
static void primeFactorization(long n) {
for(int i : primes) {
int c = 0;
while(n%i ==0) {
c++;
n/=i;
}
if(c!=0) {
primeFactors.add(new Pair(i, c));
}
}
if(n!=1) {
primeFactors.add(new Pair(n, 1));
}
}
static void 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)
{
primes.add(p);
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}}
static void paint (int i,int j,int k) {
painted[i][j] = true;
for(int c = 1;c<=k;c++) {
painted[i-c][j+c]=true;
painted[i-c][j-c] = true;
}
}
static long gcd1(long a, long b) {
if(b==0) {
return a;
}
return gcd1 (b, a%b);
}
static boolean canMoveDiagonalRight(int i, int j) {
if (i-1>=0&&j+1<m&&map[i-1][j+1]=='*') {
return true;
}
return false;
}
static int longestIncreasingSubsequence (int [] arr) {
ArrayList<Integer> lcs = new ArrayList<Integer>();
for(int i = 0;i<arr.length;i++) {
int ans = -1;
int l = 0;
int r = lcs.size()-1;
while(l<=r) {
int mid = (l+r)/2;
if(arr[lcs.get(mid)]>=arr[i]) {
ans = mid;
r = mid -1;
}
else {
l = mid +1;
}
}
if(ans ==-1) {
lcs.add(i);
}
else {
lcs.set(ans, i);
}
for(Integer e: lcs) {
// System.out.print(e+" ");
}
// System.out.println();
}
return lcs.size();
}
static boolean canMoveDiagonalLeft(int i, int j) {
if (i-1>=0&&j-1>=0&&map[i-1][j-1]=='*') {
return true;
}
return false;
}
static int countDiagonal(int start, int end) {
int countRight = 0;
int countLeft = 0;
int i = start;
int j = end ;
while(canMoveDiagonalRight(i, j)) {
countRight++;
i = i-1;
j = j+1;
}
i = start;
j = end ;
while(canMoveDiagonalLeft(i, j)) {
countLeft++;
i = i-1;
j = j-1;
}
return Math.min(countLeft, countRight);
}
static boolean [][] painted;
static char [][]map ;
//static int k;
static long cost (int k ) {
Long [] costs = new Long[n];
for(int i = 0;i<n;i++) {
costs [i] = arr[i]*1l + (i+1)*k*1l;
}
long ans = 0;
Arrays.sort(costs);
for(int i = 0;i<k;i++) {
ans += costs[i];
}
return ans;
}
static long [] a;
static long [] b;
static int k;
//static HashMap< String, Boolean> map;
static long [] arr;
static LinkedList<Integer> first;
static LinkedList<Integer> second;
static Pair [] islands;
static HashSet<Integer> special ;
static ArrayList <StringBuilder> ans ;
static int mod=1000000007;
static int m;
static int [][][]dp;
static int bitCount (int msk) {
int comp = 1;
int count = 0;
while(comp<=msk) {
if((msk&comp)!=0) {
count++;
}
comp = comp<<1;
}
return count;
}
static class triple {
int u;
int v;
int z;
public triple(int u, int v, int z) {
this.u = u;
this.v = v;
this.z = z;
}
public String toString() {
return "triple{" +
"u=" + u +
", v=" + v +
", z=" + z +
'}';
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
triple triple = (triple) o;
return u == triple.u &&
v == triple.v &&
z == triple.z;
}
public int hashCode() {
return Objects.hash(u, v, z);
}}
static class Pair {
public long x;
public long y;
public Pair (long x, long y) {
this.x = x;
this.y = y;
}
public double distance( Pair p) {
return Math.sqrt((y-p.y)*(y-p.y)+(x-p.x)*(x-p.x));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair triple = (Pair) o;
return x == triple.x &&
y == triple.y ;
}
}static long modPow(long a, long x, long p) {
//calculates a^x mod p in logarithmic time.
long res = 1;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x /= 2;
}
return res;
}
static long modInverse(long a, long p) {
//calculates the modular multiplicative of a mod m.
//(assuming p is prime).
return modPow(a, p-2, p);
}
static long [] fact;
static long nCr1(int n, int r,long mod)
{ //System.out.println(r);
long numerator = fact[n]%mod;
// System.out.println(r+" "+(n-r));
long deno = ((fact[r]%mod)*(fact[n-r]%mod))%mod;
return (numerator*modInverse(deno,mod))%mod;
}
static ArrayList <Integer> prime;
static void simpleSieve(int limit)
{
// Create a boolean array "mark[0..n-1]" and initialize
// all entries of it as true. A value in mark[p] will
// finally be false if 'p' is Not a prime, else true.
boolean mark[] = new boolean[limit+1];
for (int i = 0; i < mark.length; i++)
mark[i] = true;
for (int p=2; p*p<limit; p++)
{
// If p is not changed, then it is a prime
if (mark[p] == true)
{
// Update all multiples of p
for (int i=p*p; i<limit; i+=p)
mark[i] = false;
}
}
// Print all prime numbers and store them in prime
for (int p=2; p<limit; p++)
{
if (mark[p] == true)
{
prime.add(p);
// System.out.print(p + " ");
}
}
}
public static long gcd (long a, long b ) {
if(b==0) {
return a;
}
else {
return gcd1 (b, a%b);
}
}
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 boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
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 boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 98d2dbc1ea2463a77fde54d80ffd4a4d | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.Scanner;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.io.*;
/*
* Author : Sruthi
*/
public class Solution {
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
PrintStream ps = new PrintStream(new File("output.txt"));
InputStream is = new FileInputStream("input.txt");
System.setIn(is);
System.setOut(ps);
}
solve();
}
public static void solve() {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
long n,s;
while (--t >= 0) {
n = sc.nextLong();
s = sc.nextLong();
long ans = s -((n+1)*(n-1));
if(ans<=0)
System.out.println(0);
else
System.out.println((long)Math.ceil(s/(n*n)));
}
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 0647acdbe0d38868db5909a83915e3e5 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.*;
import java.util.*;
public class Sqr_Counting {
public static void main(String args[]){
FScanner in = new FScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while(t-->0) {
int n=in.nextInt();
long s=in.nextLong();
out.println(s/(long)(n*(long)n));
}
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 boolean checkprime(int n1)
{
if(n1%2==0||n1%3==0)
return false;
else
{
for(int i=5;i*i<=n1;i+=6)
{
if(n1%i==0||n1%(i+2)==0)
return false;
}
return true;
}
}
static class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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;
}
float nextFloat(){
return Float.parseFloat(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | b43e8860e83343071bdd055c16a10798 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | //package ritz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C{
public static int binarySearch(int a[], int high, int low, int x)
{
int mid = (high+low)/2;
if(low<=high) {
if(a[mid]==x)
return mid;
if(a[mid]>x)
return binarySearch(a, mid-1, low, x);
if(a[mid]<x)
return binarySearch(a, high, mid+1, x);
}
return -1;
}
public static int find(int a[], int low, int high, int x)
{
if(low>high)
return -1;
int mid = (low+high)/2;
if(x>a[mid])
return find(a, mid+1, high, x);
else if(x<a[mid])
return find(a, low, mid-1, x);
else
{
if(a[mid-1]!=a[mid] || mid==0)
return mid;
else
return find(a, low, mid-1, x);
}
}
public static int[] insertionSort(int a[], int n)
{
for(int i=0;i<n;i++)
{
int key = a[i];
int j = i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
return a;
}
public static void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
return;
}
public static int[] selectionSort(int a[], int n)
{
for(int i=0;i<n;i++)
{
int minIndex = i;
for(int j=i+1;j<n;j++)
if(a[j]<a[minIndex])
minIndex = j;
int temp = a[i];
a[i]=a[minIndex];
a[minIndex] = temp;
}
return a;
}
public static int[] merge(int a[], int b[])
{
int la = a.length;
int lb = b.length;
int res[]=new int[la+lb];
int i = 0, j = 0;
int index = 0;
while(i<la && j<lb)
{
if(a[i]<=b[j])
{
res[index++] = a[i++];
}
else
res[index++] = b[j++];
}
while(i<la)
res[index++] = a[i++];
while(j<lb)
res[index++] = b[j++];
return res;
}
public static int MaxRepeatingElement(int [] arrA){
int maxCounter = 0;
int element=0;
for (int i = 0; i <arrA.length ; i++) {
int counter =1;
for (int j = i+1; j <arrA.length ; j++) {
if(arrA[i]==arrA[j]){
counter++;
}
}
if(maxCounter<counter){
maxCounter=counter;
element = arrA[i];
}
}
return maxCounter;
}
static int highestPowerof2(int n)
{
// Invalid input
if (n < 1)
return 0;
int res = 1;
// Try all powers
// starting from 2^1
for (int i = 0; i < 8 * Integer.BYTES; i++)
{
int curr = 1 << i;
// If current power is
// more than n, break
if (curr > n)
break;
res = curr;
}
return res;
}
public static boolean cubeRoot(int n) {
int cube_root = (int)Math.round((Math.cbrt(n)));
if(cube_root*cube_root*cube_root==n)
return true;
else
return false;
}
public static boolean squareRoot(int n) {
int square_root = (int)Math.round(Math.sqrt(n));
if(square_root*square_root==n) {
return true;
}
return false;
}
public static boolean f(String s, int l, int r) {
if(l>=r) {
return true;
}
if(s.charAt(l)!=s.charAt(r))
return false;
f(s,l+1, r-1);
return true;
}
public static void printF(int ind, int n, int arr[], ArrayList<Integer> res, int k) {
if(ind>=n) {
int sum = 0;
for(int i : res) {
sum+=i;
}
if(sum==k) {
System.out.println(res);
}
return;
}
//pick
res.add(arr[ind]);
printF(ind+1, n, arr, res, k);
res.remove(res.size()-1); // have to remove what picked
// not pick
printF(ind+1, n, arr, res, k);
}
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;
}
public static int getMax(int[] a) {
int max = 0;
for(int i=0;i<a.length;i++) {
max = Math.max(a[i], max);
}
return max;
}
public static int findMex(int a[]) {
int mex = 0;
for (int e : a) {
if (e == mex) {
mex++;
}
}
return mex;
}
public static void main (String[] args) throws java.lang.Exception {
try {
FastScanner fs = new FastScanner();
int t = 1;
t = fs.nextInt();
while(t-->0) {
int n = fs.nextInt();
long s = fs.nextLong();
long count = s / (long)(Math.pow(n, 2));
if(count>n+1) {
System.out.println(n+1);
}
else
System.out.println(count);
}
}
catch (Exception e)
{
return;
}
}
static class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
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 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 911f42d0524d30006d9935172d0bab94 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | /* package whatever; // 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- != 0){
long n = sc.nextLong();
long s = sc.nextLong();
System.out.println(s/(n*n));
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | c434a4a754e5fc04218a3c55f07ec744 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class solution {
static final Random random = new Random();
static void sort(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);
}
// Use this to input code since it is faster than a Scanner
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());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
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());
}
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));//
static FastScanner sc = new FastScanner();//
public static void main(String[] args) throws Exception {
int t=sc.nextInt();
while(t--!=0) {
long n=sc.nextLong();long s=sc.nextLong();
if(n*n>s) {
System.out.println(0);
}else {
System.out.println(s/(n*n));
}
}
out.flush();
out.close();
}//END OF MAIN METHOD
}//END OF MAIN CLASS | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 223c531ce6af673df407f73b26f4a4c2 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.*;
import java.util.*;
public class solve{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (Exception e) {
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 FastReader sc = new FastReader();//FastReader Object
static Scanner in=new Scanner(System.in);//Scanner Object
public static void main(String[] args) {
int t=sc.nextInt();
while(t--!=0) {
long n=sc.nextLong();long s=sc.nextLong();
if(n*n>s) {
System.out.println(0);
}else {
System.out.println(s/(n*n));
}
}
} //END OF MAIN MEHTOD
} //END OF MAIN CLASS
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | d7f4af2733c3c12c1454f806014847ff | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
import javax.swing.text.StyledEditorKit;
import java.io.*;
public class Main{
public static void solve(Scanner sc)
{
long n,s;
n=sc.nextLong();
s=sc.nextLong();
System.out.println(s/(n*n));
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t;
t=sc.nextInt();
while(t>0)
{solve(sc);t--;}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 6348849a692cbd76b73b0b467b78a375 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.Scanner;
public class Main {
public static Scanner sc;
public static void work() {
long n = sc.nextLong();
long s = sc.nextLong();
long ans = s/(n*n);
System.out.println(ans);
}
public static void main(String args[]) {
sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
work();
}
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | ce0b2be41f5fa16df7f8c4e49dc931c7 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.Scanner;
public class Square_Counting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
long n = sc.nextLong();
long k = sc.nextLong();
long sq = n*n;
long ans = k/sq;
System.out.println(ans);
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 4b21d4fbc69d3e051c9940b6562043d4 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | // package NK96;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
// code here
Task solver = new D();
solver.solve(1, in, out);
out.close();
}
}
class D implements Task {
@Override
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.readInt();
while (t-- > 0) {
long n = in.readLong(), s = in.readLong();
long l = 0, r = n + 1;
while (l < r) {
long mid = (r - l) / 2 + l;
if (s > mid * n * n) {
l = mid + 1;
} else {
r = mid;
}
}
System.out.println(l * n * n <= s ? l : l - 1);
}
}
}
class A implements Task {
@Override
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int x = in.readInt(), y = in.readInt();
boolean ans = true;
for (int i = 1; i < n; i++) {
int xx = in.readInt(), yy = in.readInt();
if (xx < x || yy < y) {
ans = false;
}
x = xx;
y = yy;
}
System.out.println(ans ? "YES" : "NO");
}
}
interface Task {
public void solve(int testNumber, InputReader in, OutputWriter out);
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public int readInt() {
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 readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
public InputReader.SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(InputReader.SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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(char[] array) {
writer.print(array);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(long[] array) {
print(array);
writer.println();
}
public void printLine() {
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void print(char i) {
writer.print(i);
}
public void printLine(char i) {
writer.println(i);
}
public void printLine(char[] array) {
writer.println(array);
}
public void printFormat(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
public void print(long i) {
writer.print(i);
}
public void printLine(long i) {
writer.println(i);
}
public void print(int i) {
writer.print(i);
}
public void printLine(int i) {
writer.println(i);
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 72aa74c5b22cf5e983d250a4a0b2d448 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.awt.image.RescaleOp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.swing.text.StyleConstants.CharacterConstants;
public class binarysearch {
public static void main(String[] args) throws IOException, InterruptedException {
PrintWriter pw = new PrintWriter(System.out);
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
long n=sc.nextInt(); //why long
long s=sc.nextLong();
pw.println(s / (n * n));
}
pw.flush();
}
}
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 p) {
if (this.x == p.x)
return y - p.y;
return x - p.x;
}
public String toString() {
return "a : "+x+", b : "+y;
}
}
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 boolean ready() throws IOException {
return br.ready();
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | cea026f4e4c9087b9b7c18c4ccd1b4c4 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class A_Square_Counting
{
static int M = 1_000_000_007;
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader fs = new FastReader();
static boolean prime[];
public static void main (String[] args) throws java.lang.Exception
{
int t= fs.nextInt();
for(int i=0;i<t;i++)
{
long n=fs.nextLong();
long s=fs.nextLong();
long x=(long)((double)(s-n*n+1)/(double)(n*n-n+1));
if(x<0){
out.println(0);
continue;
}
if(s-n*n*x<=(n+1-x)*(n-1)){
out.println(x);
}else{
out.println(x+1);
}
}
out.flush();
}
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 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 8 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | daa5d6a24ff0c53ab477e14f965bfbc5 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
public class file
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
long n=sc.nextLong();
long s=sc.nextLong();
n=n*n;
System.out.println(s/n);
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 889ea24a06c277c1e84efd8ec56f5d71 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util. Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
for(int e=0; e<t; e++)
{ long n=sc.nextLong(); long s=sc.nextLong();
// 8 integeri s=0
if(n==0) { if(s==0){ System.out.println(1);}
else{System.out.println(0); }
}
else{ System.out.println(s/(n*n)); }
}
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | bb6a3b3ca44e3199675e2dd15cdd7cd8 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.Scanner;
import java.util.*;
public class T {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t= in.nextInt();
while (t-- > 0){
long n = in.nextInt();
long sum = in.nextLong();
n *= n;
System.out.println(sum/n);
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 918114d12317edf5aa60ae0de4038876 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.stream.IntStream.iterate;
public class Template {
private static final FastScanner scanner = new FastScanner();
private static void solve() {
long n = l(), s = l();
out.println(s/(n*n));
}
public static void main(String[] args) {
int t = i();
while (t-->0) {
solve();
}
}
private static long fac(int n) {
long res = 1;
for (int i = 2; i<=n; i++) {
res*=i;
}
return res;
}
private static BigInteger factorial(int n) {
BigInteger res = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++){
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
private static long l() {
return scanner.nextLong();
}
private static int i() {
return scanner.nextInt();
}
private static String s() {
return scanner.next();
}
private static void yes() {
out.println("YES");
}
private static void no() {
out.println("NO");
}
private static int toInt(char c) {
return Integer.parseInt(c+"");
}
private static void printArray(long[] a) {
StringBuilder builder = new StringBuilder();
for (long i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static void printArray(int[] a) {
StringBuilder builder = new StringBuilder();
for (int i : a)
builder.append(i).append(' ');
out.println(builder);
}
private static int binPow(int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binPow(a, n-1) * a;
else {
int b = binPow(a, n/2);
return b * b;
}
}
private static boolean isPrime(long n) {
return iterate(2, i -> (long) i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0);
}
private static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private static void stableSort(int[] a) {
List<Integer> list = stream(a).boxed().sorted().toList();
setAll(a, list::get);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(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[] readLong(int n) {
long[] a = new long[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());
}
}
static class SegmentTreeRMXQ {
static int getMid(int s, int e) {
return s + (e - s) / 2;
}
static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) {
if (l <= ss && r >= se)
return st[node];
if (se < l || ss > r)
return -1;
int mid = getMid(ss, se);
return max(
MaxUtil(st, ss, mid, l, r,
2 * node + 1),
MaxUtil(st, mid + 1, se, l, r,
2 * node + 2));
}
static void updateValue(int[] arr, int[] st, int ss, int se, int index, int value, int node) {
if (index < ss || index > se) {
System.out.println("Invalid Input");
return;
}
if (ss == se) {
arr[index] = value;
st[node] = value;
} else {
int mid = getMid(ss, se);
if (index <= mid)
updateValue(arr, st, ss, mid,
index, value,
2 * node + 1);
else
updateValue(arr, st, mid + 1, se, index,
value, 2 * node + 2);
st[node] = max(st[2 * node + 1],
st[2 * node + 2]);
}
}
static int getMax(int[] st, int n, int l, int r) {
if (l < 0 || r > n - 1 || l > r) {
System.out.print("Invalid Input\n");
return -1;
}
return MaxUtil(st, 0, n - 1, l, r, 0);
}
static int constructSTUtil(int[] arr, int ss, int se, int[] st, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = max(
constructSTUtil(arr, ss, mid,
st, si * 2 + 1),
constructSTUtil(arr, mid + 1,
se, st,
si * 2 + 2));
return st[si];
}
static int[] constructST(int[] arr, int n) {
int x = (int)Math.ceil(Math.log(n) / Math.log(2));
int max_size = 2 * (int)Math.pow(2, x) - 1;
int[] st = new int[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return st;
}
}
static class SegmentTreeRMNQ {
int[] st;
int minVal(int x, int y) {
return min(x, y);
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int RMQUtil(int ss, int se, int qs, int qe, int index) {
if (qs <= ss && qe >= se)
return st[index];
if (se < qs || ss > qe)
return Integer.MAX_VALUE;
int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
int RMQ(int n, int qs, int qe) {
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return RMQUtil(0, n - 1, qs, qe, 0);
}
int constructSTUtil(int[] arr, int ss, int se, int si) {
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
void constructST(int[] arr, int n) {
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
constructSTUtil(arr, 0, n - 1, 0);
}
}
static class SegmentTreeRSQ
{
int[] st; // The array that stores segment tree nodes
/* Constructor to construct segment tree from given array. This
constructor allocates memory for segment tree and calls
constructSTUtil() to fill the allocated memory */
SegmentTreeRSQ(int[] arr, int n)
{
// Allocate memory for segment tree
//Height of segment tree
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // Memory allocation
constructSTUtil(arr, 0, n - 1, 0);
}
// A utility function to get the middle index from corner indexes.
int getMid(int s, int e) {
return s + (e - s) / 2;
}
/* A recursive function to get the sum of values in given range
of the array. The following are parameters for this function.
st --> Pointer to segment tree
si --> Index of current node in the segment tree. Initially
0 is passed as root is always at index 0
ss & se --> Starting and ending indexes of the segment represented
by current node, i.e., st[si]
qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int ss, int se, int qs, int qe, int si)
{
// If segment of this node is a part of given range, then return
// the sum of the segment
if (qs <= ss && qe >= se)
return st[si];
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return 0;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
}
/* A recursive function to update the nodes which have the given
index in their range. The following are parameters
st, si, ss and se are same as getSumUtil()
i --> index of the element to be updated. This index is in
input array.
diff --> Value to be added to all nodes which have I in range */
void updateValueUtil(int ss, int se, int i, int diff, int si)
{
// Base Case: If the input index lies outside the range of
// this segment
if (i < ss || i > se)
return;
// If the input index is in range of this node, then update the
// value of the node and its children
st[si] = st[si] + diff;
if (se != ss) {
int mid = getMid(ss, se);
updateValueUtil(ss, mid, i, diff, 2 * si + 1);
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
}
}
// The function to update a value in input array and segment tree.
// It uses updateValueUtil() to update the value in segment tree
void updateValue(int arr[], int n, int i, int new_val)
{
// Check for erroneous input index
if (i < 0 || i > n - 1) {
System.out.println("Invalid Input");
return;
}
// Get the difference between new value and old value
int diff = new_val - arr[i];
// Update the value in array
arr[i] = new_val;
// Update the values of nodes in segment tree
updateValueUtil(0, n - 1, i, diff, 0);
}
// Return sum of elements in range from index qs (query start) to
// qe (query end). It mainly uses getSumUtil()
int getSum(int n, int qs, int qe)
{
// Check for erroneous input values
if (qs < 0 || qe > n - 1 || qs > qe) {
System.out.println("Invalid Input");
return -1;
}
return getSumUtil(0, n - 1, qs, qe, 0);
}
// A recursive function that constructs Segment Tree for array[ss..se].
// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current node of
// segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then recur for left and
// right subtrees and store the sum of values in this node
int mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) +
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
return st[si];
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 848803529e1c1b1358825c61a1b63946 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes |
//https://codeforces.com/problemset/problem/1646/A
import java.util.Scanner;
public class Square_Counting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
while (testcase-- != 0) {
long n = sc.nextLong(), s = sc.nextLong();
System.out.print(s / (n * n));
if (testcase > 0)
System.out.println();
}
sc.close();
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 1d0b794e512cde62bbaae692eac4cba3 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
public class cp {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
long n=sc.nextLong();
long s=sc.nextLong();
long x=n;
if(s==0){
System.out.println("0");
}
else{
n=n*n;
{
System.out.println(s/n);
}
}
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 19a39ada9b653e0be8893167e742cb4e | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
int tc = sc.nextInt();
while(tc-- > 0){
long n = sc.nextLong();
long s = sc.nextLong();
long sq = n*n;
long no = s/sq;
System.out.println(no);
}
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 225e06dcd568f5005d2742e7e68ad292 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
long n,s;
while (t-- > 0) {
n=sc.nextLong();
s=sc.nextLong();
if(n>s) System.out.println(0);
else if(n==s) System.out.println(n==1?1:0);
else System.out.println(s/(n*n));
}
sc.close();
}
} | Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 90abc10e8aa73ed446c2b36ab62bb5f4 | train_110.jsonl | 1646408100 | Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \ldots, a_{n+1}$$$. For each $$$i = 1, 2, \ldots, n+1$$$ it is guaranteed that $$$0\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round774A {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Round774A sol = new Round774A();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
int n = in.nextInt();
long s = in.nextLong();
if(isDebug){
out.printf("Test %d\n", i);
}
int ans = solve(n, s);
out.printlnAns(ans);
if(isDebug)
out.flush();
}
in.close();
out.close();
}
int solve(int n, long s){
// (n-1)*(n+1) < n^2
return (int)(s/n/n);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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[][] nextTreeEdges(int n, int offset){
int[][] e = new int[n-1][2];
for(int i=0; i<n-1; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextPairs(int n){
return nextPairs(n, 0);
}
int[][] nextPairs(int n, int offset) {
int[][] xy = new int[2][n];
for(int i=0; i<n; i++) {
xy[0][i] = nextInt() + offset;
xy[1][i] = nextInt() + offset;
}
return xy;
}
int[][] nextGraphEdges(){
return nextGraphEdges(0);
}
int[][] nextGraphEdges(int offset) {
int m = nextInt();
int[][] e = new int[m][2];
for(int i=0; i<m; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
int[] inIdx = new int[n];
int[] outIdx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][outIdx[u]++] = v;
inNeighbors[v][inIdx[v]++] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
int[] idx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][idx[u]++] = v;
neighbors[v][idx[v]++] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"] | 1 second | ["0\n1\n3\n1"] | NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence. | Java 17 | standard input | [
"math"
] | 7226a7d2700ee52f52d417f404c42ab7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\le n< 10^6$$$, $$$0\le s \le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints. | 800 | For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$. | standard output | |
PASSED | 5652d015c5575434d8f988d60feb0dec | 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 class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
if (this.d == o.d)
return this.i - o.i;
if (this.d > o.d)
return 1;
else
return -1;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
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();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Reader sc = new Reader();
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
long[] fact = new long[17];
fact[0] = 1L;
for (int i = 1; i <= 16; i++) {
fact[i] = fact[i - 1] * i * 1L;
}
HashMap<Long, Long> hm = new HashMap<>();
Util(hm, 1, 0, 0, fact);
while (tc-- > 0) {
long n = sc.nextLong();
long nc = n;
long count = 2000;
if (hm.containsKey(n)) {
count = hm.get(n);
}
long c = 0;
long num = 0;
int k = 0;
long ans = 20000;
hm.put(0L, 0L);
for (long key : hm.keySet()) {
if (n >= key)
ans = Math.min(ans, hm.get(key) + countBit(n - key));
}
sb.append(ans);
sb.append("\n");
}
System.out.println(sb);
}
public static int countBit(long n) {
int count = 0;
while (n > 0) {
count += (n & 1);
n /= 2;
}
return count;
}
public static void Util(HashMap<Long, Long> hm, int idx, long sum, long count, long[] arr) {
if (idx >= arr.length) {
return;
}
Util(hm, idx + 1, sum, count, arr);
hm.put(sum + arr[idx], count + 1);
Util(hm, idx + 1, sum + arr[idx], count + 1, arr);
}
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1])
return false;
}
return true;
}
public static int get(int[] dsu, int x) {
if (dsu[x] == x)
return x;
int k = get(dsu, dsu[x]);
dsu[x] = k;
return k;
}
static int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
public static int Xor(int n) {
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
public static double digit(long num) {
return Math.floor(Math.log10(num) + 1);
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int 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.get(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 < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
| 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 | 7afdd7180ce3d1842ef00176bfc4e620 | 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 | /*
Rating: 1461
Date: 29-03-2022
Time: 19-18-50
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C_Factorials_and_Powers_of_Two {
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
public static long cnt(long n) {
long cnt = 0;
while(n != 0) {
cnt += (n&1);
n>>=1;
}
return cnt;
}
public static long count(long n, long curr) {
if(n < Functions.factorial(curr) || curr >= 15) return cnt(n);
long a = 1 + count(n-Functions.factorial(curr), curr+1);
long c = count(n, curr+1);
return Functions.min(a, c);
}
public static void s() {
long n = sc.nextLong();
p.writeln(count(n, 3));
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
static final Long MOD = Long.MAX_VALUE;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
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 void sort(long... a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static int max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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());
}
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;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
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 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 | 7bd2a602c01241cc5c326a525916fdd9 | 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 Codechef {
/*
* Aim 1: Do 200 DP codeforces problems
* Aim 2: Beat Sparsh in ratings before 2 june.
*
*/
static long fans[] = new long[200001];
static long inv[] = new long[200001];
static long mod = 1000000007;
static void init() {
fans[0] = 1;
inv[0] = 1;
fans[1] = 1;
inv[1] = 1;
for (int i = 2; i < 200001; i++) {
fans[i] = ((long) i * fans[i - 1]) % mod;
inv[i] = power(fans[i], mod - 2);
}
}
static long ncr(int n, int r) {
return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod;
}
static List<Long> list = new ArrayList<>();
static void fill() {
long zero = 1;
for(int i = 1;zero<1000000000000l;i++)
{
list.add(zero);
zero*=i;
}
}
static int min;
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
int t = 1;
t = in.nextInt();
fill();
//System.out.println(list.size());
while (t > 0) {
--t;
long n = in.nextLong();
min = countSetBits(n);
long bit = 0;
int ind = list.size()-1;
findans(n,0,bit,ind);
sb.append(min+"\n");
}
System.out.print(sb);
}
static void findans(long n,int cur,long bit,int ind)
{
if(n == 0) {
min = Math.min(cur, min);
return;
}
if(n<=0 || cur>min || ind<0)
return;
min = Math.min(cur+countSetBits(n), min);
if(n>=list.get(ind))
{
findans(n-list.get(ind),cur+1,bit,ind-1);
}
findans(n,cur,bit,ind-1);
}
static int countSetBits(long n)
{
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
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 % mod) * (x % mod)) % mod;
// y must be even now
y = y >> 1; // y = y/2
x = ((x % mod) * (x % mod)) % mod; // Change x to x^2
}
return res;
}
static long[] generateArray(FastReader in, int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextLong();
return arr;
}
static long[][] generatematrix(FastReader in, int n, int m) throws IOException {
long arr[][] = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextLong();
}
}
return arr;
}
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 / gcd(a, b)) * b;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
} | 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 | dccedfe3357ada9e94c26d6021c0c773 | 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 Codechef {
/*
* Aim 1: Do 200 DP codeforces problems
* Aim 2: Beat Sparsh in ratings before 2 june.
*
*/
static int min;
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
long fac[] = new long[16];
fac[0] = 1;
for(int i = 1;i<16;i++)
fac[i] = fac[i-1]*(long)i;
int t = 1;
t = in.nextInt();
while (t > 0) {
--t;
long n = in.nextLong();
min = Integer.MAX_VALUE;
for(int i = 0;i<(1<<16);i++)
{
long sum = 0;
for(int j = 0;j<16;j++)
{
if((i&(1<<j))>0)
{
sum+=fac[j];
}
}
if(sum<=n)
{
long temp = n - sum;
//if(((i&1) == 1 && (temp&1) == 1) && ((i&2) == 1 && (temp&2) == 1))
// continue;
min = Math.min(min, countSetBits(i) + countSetBits(temp));
}
}
sb.append(min+"\n");
}
System.out.print(sb);
}
static int countSetBits(long n)
{
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
static long[] generateArray(FastReader in, int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextLong();
return arr;
}
static long[][] generatematrix(FastReader in, int n, int m) throws IOException {
long arr[][] = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextLong();
}
}
return arr;
}
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 / gcd(a, b)) * b;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
} | 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 | b578b6e96077baf4f1b134f66806a6dd | 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.math.BigInteger;
import java.util.*;
public class Main {
///
// Two theorams are used to find ncrp
// Lucas theoram and the fermit theoram
// a^p-1=1(modp) example 2 and 3
// Lucas theoram
// ncr=ncr(n/mod,n/mod)*ncr1(n%p,r%mod);
static long max = Integer.MAX_VALUE;
//C0C2+C1C1+C2C0
static int catalanNumber(int n, int dp[]) {
if (n <= 2) {
return 1;
}
if (dp[n] != -1) {
return dp[n];
}
//C0C1+C1C2+C20
int answer = 0;
for (int i = 0; i < n; ++i) {
//01 //10
//This is the dynamic programming
answer += catalanNumber(i, dp) * catalanNumber(n - i - 1, dp);
// System.out.println(answer);
}
return dp[n] = answer;
}
static int printNthcatalanNumber(int n) {
int dp[] = new int[n + 1];
Arrays.fill(dp, -1);
return catalanNumber(n, dp);
}
static int power(int a, int b, int mod) {
int res = 1;
while (b > 0) {
if (b % 2 == 1) {
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
static int fermittheoram(int a, int mod) {
return power(a, mod - 2, mod);
}
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 PrintWriter out=new PrintWriter(System.out);
static FastReader scan=new FastReader();
static boolean check=false;
static class Pair{
int x;
int y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
}
static int min=Integer.MAX_VALUE;
static void find(List<Long>lst,int index,long sum,int count){
if(index>=lst.size()){
return ;
}
if(sum==0){
min=Math.min(count,min);
return ;
}
else
if(sum<0){
return;
}
find(lst,index+1,sum-lst.get(index),count+1);
find(lst,index+1,sum,count);
}
static void solve() {
int t=scan.nextInt();
List<Long>lst=new LinkedList<>();
for(int i=1;i<=14;++i){
long sum=1;
for(int j=1;j<=i;++j){
sum*=j;
}
lst.add(sum);
}
while(t-->0){
long s=scan.nextLong();
if(lst.contains(s)||((s&(s-1))==0)){
System.out.println("1");
continue;
}
int flag=0;
int answer=Integer.MAX_VALUE;
int min=Integer.MAX_VALUE;
for(int i=0;i<=(1<<14)-1;++i){
long sum=0;
int count=0;
for(int j=0;j<14;++j){
if((i&(1<<j))!=0){
++count;
sum+=lst.get(j);
}
if(sum==s){
answer=Math.min(answer,count);
flag=1;
}
}
if(flag==1){
break;
}
if(s>sum){
min=Math.min(min,count+Long.bitCount(s-sum));
}
}
System.out.println(Math.min(answer,min));
}
}
public static void main(String[] args) {
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 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 | c28c72c9526681b17e30f51669e07b07 | 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 | /*
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
If you don't use your brain 100%, it deteriorates gradually
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
static StringBuffer str=new StringBuffer();
static BufferedReader bf;
static PrintWriter pw;
static int n;
static long mask[];
static void cal(){
mask=new long[14];
long fact=1;
int j=2;
while(fact<=(long)1e12){
mask[j-2]=fact;
fact*=j;
j++;
}
}
static void solve(int te) throws Exception{
long x=Long.parseLong(bf.readLine().trim());
long ans=0;
for(int i=0;i<40;i++){
if((x&(1l<<i))>0) ans++;
}
for(int i=0;i<(1<<14);i++){
long cur=x;
int cnt2=0;
for(int j=0;j<14;j++){
if((i&(1<<j))>0){cur-=mask[j]; cnt2++;}
}
if(cur<0) continue;
int cnt=0;
for(int j=0;j<40;j++){
if((cur&(1l<<j))>0) cnt++;
}
ans=Math.min(ans, cnt+cnt2);
}
str.append(ans).append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
boolean lenv=false;
int te=1;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
// int q1 = Integer.parseInt(bf.readLine().trim());
// for(te=1;te<=q1;te++) {
cal();
n=Integer.parseInt(bf.readLine().trim());
while(n-->0) solve(te);
// }
pw.print(str);
pw.flush();
// System.out.println(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 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 | b653e72f4b00a28a69288d4e53423b28 | 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.BufferedOutputStream;
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.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Practice2 {
/* package codechef; // don't place package name! */
static class Pair{
long n;
long cost;
Pair(long n,long cost){
this.n=n;
this.cost=cost;
}
}
public static void find(HashMap<Long,ArrayList<Pair>> map,boolean[] vis,long node) {
if(vis[(int)node]==true) return;
vis[(int)node]=true;
if(map.containsKey(node)) {
for(Pair p: map.get(node)) {
find(map,vis,p.n);
}
}
}
public static long findmax(boolean[] vis) {
long max=0;
for(int i=1;i<vis.length;i++) {
if(vis[i]==true) {
max=Math.max(max, i);
}
}
return max;
}
/** Code for Dijkstra's algorithm **/
public static class ListNode {
int vertex, weight;
ListNode(int v,int w) {
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
}
public static int[] dijkstra(
int V, HashMap<Integer,ArrayList<ListNode> > graph,
int source) {
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[0] = 0;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
(v1, v2) -> v1.getWeight() - v2.getWeight());
pq.add(new ListNode(source, 0));
while (pq.size() > 0) {
ListNode current = pq.poll();
for (ListNode n :
graph.get(current.getVertex())) {
if (distance[current.getVertex()]
+ n.getWeight()
< distance[n.getVertex()]) {
distance[n.getVertex()]
= n.getWeight()
+ distance[current.getVertex()];
pq.add(new ListNode(
n.getVertex(),
distance[n.getVertex()]));
}
}
}
// If you want to calculate distance from source to
// a particular target, you can return
// distance[target]
return distance;
}
//Methos to return all divisor of a number
static ArrayList<Long> allDivisors(long n) {
ArrayList<Long> al=new ArrayList<>();
long i=2;
while(i*i<=n) {
if(n%i==0) al.add(i);
if(n%i==0&&i*i!=n) al.add(n/i);
i++;
}
return al;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int find(boolean[] vis,int x,int y,int n,int m) {
if(x<0||y<0||x>=n||y>=m) return 0;
// if(vis[i][j]==true) return
return 0;
}
static long power(long N,long R)
{
long x=998244353;
if(R==0) return 1;
if(R==1) return N;
long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p
temp=(temp*temp)%x;
if(R%2==0){
return temp%x;
}else{
return (N*temp)%x;
}
}
static int[] sort(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static long[] sort(long[] arr) {
long n=arr.length;
ArrayList<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
public static long find(long n) {
long count=0;
while(n>0) {
long temp=(n&1);
if(temp>0) count++;
n=n>>1;
}
return count;
}
public static long fact(long n) {
long ans=1;
while(n>1) {
ans *=n;
n--;
}
return ans;
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
long n=sc.nextLong();
long min=Long.MAX_VALUE;
for(long i=0;i<=Math.pow(2,14);i++) {
long sum=0,count=0;
for(long j=0;j<14;j++) {
if(((1l<<j)&i)>0) {
sum +=fact(j+1);
count++;
}
}
if(sum<=n) min=Math.min(min,count+find(n-sum));
}
out.println(min);
}
out.close();
}
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 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 | 01c21adb28ebd91e03110243369cea9c | 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 | 5e1e23436d33b6682b017a1188a1366b | 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 {
public static void main(String[] args) {
FastReader in = new FastReader();
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++;
}
int T = in.nextInt();
for(int ttt = 1; ttt <= T; ttt++) {
long n = in.nextLong();
long ans = Long.MAX_VALUE;
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;
}
ans = Math.min(Integer.bitCount(mask)+Long.bitCount(sum), ans);
}
System.out.println(ans);
}
out.close();
}
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[] readInt(int size) {
int[] arr = new int[size];
for(int i = 0; i < size; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
long[] readLong(int size) {
long[] arr = new long[size];
for(int i = 0; i < size; i++)
arr[i] = Long.parseLong(next());
return arr;
}
int[][] read2dArray(int rows, int cols) {
int[][] arr = new int[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
arr[i][j] = Integer.parseInt(next());
}
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 | 6624c655ecba7b3d6e02d916a30d2223 | 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 javax.sql.rowset.spi.SyncResolver;
import java.io.*;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormatSymbols;
public class Solution {
static int a[];
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
outer:
while (t-- > 0) {
long n = fs.nextLong();
long [] fact = new long[15];
fact[0]=0l;
fact[1]=1l;
fact[2]=2l;
long sum = 2;
long v=3;
for(int i=3;i<fact.length;i++) {
fact[i] = sum*v;
sum = sum*v;
v++;
}
long res = Integer.MAX_VALUE;
for(int i=0;i<(1l<<fact.length);i++) {
long tmp = n;
long count=0l;
for(int j=0;j<15;j++) {
if((i&(1l<<j))!=0) {
tmp = tmp-fact[j];
count++;
}
}
if(tmp>=0) {
res = Math.min(res, Long.bitCount(i) + Long.bitCount(tmp));
}
}
if(res!=Integer.MAX_VALUE) {
out.println(res);
}else {
out.println(-1);
}
}
out.close();
}
// static int count=0;
public static void solve(int l,int u, int ht[],int count) {
int maxid=-1;
int max = Integer.MIN_VALUE;
for(int i=l;i<=u;i++) {
if(a[i]>max) {
max = a[i];
maxid=i;
}
}
ht[maxid]=count;
solve(l, maxid-1, ht,count+1);
solve(maxid+1, u, ht,count+1);
}
static long bit[]; // '1' index based array
public static void update(int n,int i,int x){
for(;i<n;i+=(i&(-i))){
bit[i] += x;
}
}
public static long sum(int i){
long sum=0;
for(;i>0 ;i -= (i&(-i))){
sum += bit[i];
}
return sum;
}
static class Pair implements Comparable<Pair> {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return this.x - o.x;
}
}
static long power(long x, long y, long p) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long 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[] 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());
}
}
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;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i)
res *= i;
for (int i = 2; i <= k; ++i)
res /= i;
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 | 2d342ba9db119b232ddc60512a9e3528 | 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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class c731 {
public static void main(String[] args) throws IOException {
// try {
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int o = 0; o<t;o++){
long n = sc.nextLong();
long[] fac = new long[16];
fac[0] = 1;
for(int i = 1 ; i<=15;i++) {
fac[i] = fac[i-1]*i;
}
int ans = 100;
for(int i = 0 ;i<(1<<14);i++) {
int c1 = 0;
long s = 0;
int v = 1;
for(int j = 0 ;j<=14;j++ ) {
if((v&i)!=0) {
c1++;
s += fac[j+1];
}
v = v<<1;
}
long rem = n - s;
//System.out.println(c1 + " " + s + " " + rem);
if(rem<0) {
continue;
}
if((i&1)!=0 && (rem&1)!=0) {
continue;
}
if((i&2)!=0 && (rem&2)!=0) {
continue;
}
int c2 = cnt(rem);
ans = Math.min(ans, c1+c2);
}
System.out.println(ans);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static int cnt(long x) {
long v = 1l;
int c =0;
int f = 0;
while(v<=x) {
if((v&x)!=0) {
c++;
}
v = v<<1;
}
return c;
}
public static void dfs(int s , HashMap<Integer, ArrayList<Integer>> map, boolean[] vis) {
vis[s] = true;
for(int x : map.get(s)) {
if(!vis[x]) {
dfs(x, map, vis);
}
}
}
//public static int lis(int[] arr) {
// int n = arr.length;
// ArrayList<Integer> al = new ArrayList<Integer>();
// al.add(arr[0]);
// for(int i = 1 ; i<n;i++) {
// int x = al.get(al.size()-1);
// if(arr[i]>=x) {
// al.add(arr[i]);
// }else {
// int v = upper_bound(al, 0, al.size(), arr[i]);
// al.set(v, arr[i]);
// }
// }
//return al.size();
//}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long power(long x, long y, long p){
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n , int r , long m) {
if(r>n) {
return 0;
}
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static int lower_bound(ArrayList<Long> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Long> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// private static int[] parent;
// private static int[] size;
public static int find(int[] parent, int u) {
while(u != parent[u]) {
parent[u] = parent[parent[u]];
u = parent[u];
}
return u;
}
private static void union(int[] parent,int[] size,int u, int v) {
int rootU = find(parent,u);
int rootV = find(parent,v);
if(rootU == rootV) {
return;
}
if(size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
public static void build(boolean [] seg,boolean []arr,int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// seg[idx] = seg[idx*2+1]+ seg[idx*2+2];
// seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]);
seg[idx] = seg[idx*2+1] && seg[idx*2+2];
}
//for finding minimum in range
public static boolean query(boolean[]seg,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return true;
}
int mid = (lo + hi)/2;
boolean left = query(seg,idx*2 +1, lo, mid, l, r);
boolean right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//return gcd(left, right);
// return Math.min(left, right);
return left && right;
}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
//
static void shuffleArray(long[] ar)
{
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
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 coup{
int a;
int b;
public coup(int a , int b) {
this.a = a;
this.b = b;
}
}
class trip{
int a , b, c;
public trip(int a , int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
} | 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 | 13377488b610ea9aac698c4fbda51658 | 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.*;
// cd C:\Users\Lenovo\Desktop\New
//ArrayList<Integer> a=new ArrayList<>();
//List<Integer> lis=new ArrayList<>();
//StringBuilder ans = new StringBuilder();
//HashMap<Integer,Integer> map=new HashMap<>();
public class cf {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
//static long dp[]=new long[200002];
public static void main(String args[]) throws IOException {
FastReader sc=new FastReader();
//Scanner s=new Scanner(System.in);
int tt=sc.nextInt();
//int tt=1;
long fact[]=new long[14];
//fact[0]=1;
fact[0]=1;
fact[1]=2;
factorial(fact);
while(tt-->0){
long n=sc.nextLong();
int ans=Integer.MAX_VALUE;
for(int i=0;i<(1<<14);i++){
long sum=0l;
for(int j=0;j<14;j++){
if((i&(1<<j))!=0){
sum+=fact[j];
}
}
if(sum>n){
break;
}
int c1=count(i);
int c2=count(n-sum);
//System.out.println(c2+" "+c1+" "+sum);
ans=Math.min(ans,c1+c2);
}
System.out.println(ans);
}
}
static int count(long sum){
int f=0;
while(sum>0){
if(sum%2==1){
f++;
}
sum=sum/2;
}
return f;
}
static long find(ArrayList<Long> arr,long n){
int l=0;
int r=arr.size();
while(l+1<r){
int mid=(l+r)/2;
if(arr.get(mid)<n){
l=mid;
}
else{
r=mid;
}
}
return arr.get(l);
}
static void factorial(long []fact){
for(int i=2;i<14;i++){
fact[i]=1l*(i+1)*fact[i-1];
}
}
static boolean compare(String a4,String a7){
int len=a4.length();
for(int i=0;i<len;i++){
char c1=a4.charAt(i);
char c2=a7.charAt(i);
if(c1<c2){
return true;
}
else if(c2<c1){
return false;
}
}
return true;
}
static void rotate(int ans[]){
int last=ans[0];
for(int i=0;i<ans.length-1;i++){
ans[i]=ans[i+1];
}
ans[ans.length-1]=last;
}
static int reduce(int n){
while(n>1){
if(n%2==1){
n--;
n=n/2;
}
else{
if(n%4==0){
n=n/4;
}
else{
break;
}
}
}
return n;
}
static long count(long n,int p,HashSet<Long> set){
if(n<Math.pow(2,p)){
set.add(n);
return count(2*n+1,p,set)+count(4*n,p,set)+1;
}
return 0;
}
static int countprimefactors(int n){
int ans=0;
int z=(int)Math.sqrt(n);
for(int i=2;i<=z;i++){
while(n%i==0){
ans++;
n=n/i;
}
}
if(n>1){
ans++;
}
return ans;
}
/*static int count(int arr[],int idx,long sum,int []dp){
if(idx>=arr.length){
return 0;
}
if(dp[idx]!=-1){
return dp[idx];
}
if(arr[idx]<0){
return dp[idx]=Math.max(1+count(arr,idx+1,sum+arr[idx],dp),count(arr,idx+1,sum,dp));
}
else{
return dp[idx]=1+count(arr,idx+1,sum+arr[idx],dp);
}
}*/
static String reverse(String s){
String ans="";
for(int i=s.length()-1;i>=0;i--){
ans+=s.charAt(i);
}
return ans;
}
static int find_max(int []check,int y){
int max=0;
for(int i=y;i>=0;i--){
if(check[i]!=0){
max=i;
break;
}
}
return max;
}
static void dfs(int [][]arr,int row,int col,boolean [][]seen,int n){
if(row<0 || col<0 || row==n || col==n){
return;
}
if(arr[row][col]==1){
return;
}
if(seen[row][col]==true){
return;
}
seen[row][col]=true;
dfs(arr,row+1,col,seen,n);
dfs(arr,row,col+1,seen,n);
dfs(arr,row-1,col,seen,n);
dfs(arr,row,col-1,seen,n);
}
static int msb(int x){
int ans=0;
while(x!=0){
x=x/2;
ans++;
}
return ans;
}
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 int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
/* static boolean checkprime(int n1)
{
if(n1%2==0||n1%3==0)
return false;
else
{
for(int i=5;i*i<=n1;i+=6)
{
if(n1%i==0||n1%(i+2)==0)
return false;
}
return true;
}
}
*/
/* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> entry = iterator.next();
int value = entry.getValue();
if(value==1){
iterator.remove();
}
else{
entry.setValue(value-1);
}
}
*/
static class Pair implements Comparable
{
int a,b;
public String toString()
{
return a+" " + b;
}
public Pair(int x , int y)
{
a=x;b=y;
}
@Override
public int compareTo(Object o) {
Pair p = (Pair)o;
if(p.a!=a){
return a-p.a;//in
}
else{
return b-p.b;//
}
}
}
/* public static boolean checkAP(List<Integer> lis){
for(int i=1;i<lis.size()-1;i++){
if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){
return false;
}
}
return true;
}
public static int minBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]>=val){
r=mid;
}
else{
l=mid;
}
}
return r;
}
public static int maxBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]<=val){
l=mid;
}
else{
r=mid;
}
}
return l;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}*/
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 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 | 6234b9eb1f424bba08d83a42d228240b | 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 {
private static final Long[] fac = {6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L, 479001600L, 6227020800L, 87178291200L};
private static Long[] facSum = new Long[1 << fac.length];
private static void solve(long x){
int res = Long.bitCount(x);
if(res == 1){
System.out.println(res);
return;
}
for(int mask = 0; mask < facSum.length; mask++){
if(x < facSum[mask]){continue;}
res = Math.min(res, Integer.bitCount(mask) + Long.bitCount(x - facSum[mask]));
}
System.out.println(res);
}
private static void getAllFacSum(){
for(int mask = 0; mask < facSum.length; mask++){
long sum = 0L;
for(int i = 0; i < fac.length; i++){
if((mask & (1 << i)) != 0){
sum += fac[i];
}
}
facSum[mask] = sum;
}
}
public static void main(String[] args){
getAllFacSum();
MyScanner scanner = new MyScanner();
int num = scanner.nextInt();
for(int i = 0; i < num; i++){
long x = scanner.nextLong();
solve(x);
}
}
//public static PrintWriter out;
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 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 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 | 5d7c8066a2ae72221778c85e03c8d5a7 | 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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class CodeForces {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public FastScanner(File f){
try {
br=new BufferedReader(new FileReader(f));
st=new StringTokenizer("");
} catch(FileNotFoundException e){
br=new BufferedReader(new InputStreamReader(System.in));
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[] 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());
}
}
public static long factorial(int n){
if(n==0)return 1;
return (long)n*factorial(n-1);
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void sort (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sortReversed (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sort (long[]a){
ArrayList<Long> b = new ArrayList<>();
for(long i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
ans.add(i);
}
return ans;
}
static int binarySearchSmallerOrEqual(int arr[], int key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
static int binarySearchSmallerOrEqual(long arr[], long key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
public static int binarySearchStrictlySmaller(int[] arr, int target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
public static int binarySearchStrictlySmaller(long[] arr, long target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static int binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void init(int[]arr,int val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static void init(int[][]arr,int val){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=val;
}
}
}
static void init(long[]arr,long val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static<T> void init(ArrayList<ArrayList<T>>arr,int n){
for(int i=0;i<n;i++){
arr.add(new ArrayList());
}
}
static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target)
{
int start = 0, end = arr.size()-1;
if(end == 0) return -1;
if (target > arr.get(end).y) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid).y >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearchStrictlyGreater(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] <= target) {
start = mid + 1;
}
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
static long sum (int []a, int[][] vals){
long ans =0;
int m=0;
while(m<5){
for (int i=m+1;i<5;i+=2){
ans+=(long)vals[a[i]-1][a[i-1]-1];
ans+=(long)vals[a[i-1]-1][a[i]-1];
}
m++;
}
return ans;
}
public static long pow(long n, long pow) {
if (pow == 0) {
return 1;
}
long retval = n;
for (long i = 2; i <= pow; i++) {
retval *= n;
}
return retval;
}
static String reverse(String s){
StringBuffer b = new StringBuffer(s);
b.reverse();
return b.toString();
}
static String charToString (char[] arr){
String t="";
for(char c :arr){
t+=c;
}
return t;
}
int[] copy (int [] arr , int start){
int[] res = new int[arr.length-start];
for (int i=start;i<arr.length;i++)res[i-start]=arr[i];
return res;
}
static int[] swap(int [] A,int l,int r){
int[] B=new int[A.length];
for (int i=0;i<l;i++){
B[i]=A[i];
}
int k=0;
for (int i=r;i>=l;i--){
B[l+k]=A[i];
k++;
}
for (int i=r+1;i<A.length;i++){
B[i]=A[i];
}
return B;
}
static int mex (int[] d){
int [] a = Arrays.copyOf(d, d.length);
sort(a);
if(a[0]!=0)return 0;
int ans=1;
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1])continue;
if(a[i]==a[i-1]+1)ans++;
else break;
}
return ans;
}
static int[] mexes(int[] arr){
int[] freq = new int [100000+7];
for (int i:arr)freq[i]++;
int maxMex =0;
for (int i=0;i<=100000+7;i++){
if(freq[i]!=0)maxMex++;
else break;
}
int []ans = new int[arr.length];
ans[arr.length-1] = maxMex;
for (int i=arr.length-2;i>=0;i--){
freq[arr[i+1]]--;
if(freq[arr[i+1]]<=0){
if(arr[i+1]<maxMex)
maxMex=arr[i+1];
ans[i]=maxMex;
} else {
ans[i]=ans[i+1];
}
}
return ans;
}
static int [] freq (int[]arr,int max){
int []b = new int[max];
for (int i:arr)b[i]++;
return b;
}
static int[] prefixSum(int[] arr){
int [] a = new int[arr.length];
a[0]=arr[0];
for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i];
return a;
}
public static void main(String[] args) {
// StringBuilder sbd = new StringBuilder();
// PrintWriter out = new PrintWriter("output.txt");
// File input = new File("input.txt");
// FastScanner fs = new FastScanner(input);
//
FastScanner fs = new FastScanner();
// Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int testNumber =fs.nextInt();
ArrayList<Long> vals =new ArrayList<>();
for (int i=1;i<=14;i++){
vals.add(factorial(i));
}
// ArrayList<Integer> arr = new ArrayList<>();
for (int T =0;T<testNumber;T++){
// StringBuffer sbf = new StringBuffer();
long n = fs.nextLong();
int ans=10000000;
for (int i=0;i<=pow(2,vals.size());i++){
char [] c = reverse(Integer.toBinaryString(i)).toCharArray();
long sum=0;
int k=0;
for (int j=0;j<vals.size();j++){
if(j<c.length&&c[j]=='1'){
sum+=vals.get(j);
k++;
}
}
if(sum<=n){
for (char r:Long.toBinaryString(n-sum).toCharArray())if(r=='1')k++;
ans=Math.min(ans,k);
}
}
out.print(ans+"\n");
}
out.flush();
}
static class Pair {
int x;//a
int y;//k
public Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public boolean equals(Object o) {
if(o instanceof Pair){
if(o.hashCode()!=hashCode()){
return false;
} else {
return x==((Pair)o).x&&y==((Pair)o).y;
}
}
return false;
}
@Override
public int hashCode() {
return x+2*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 | 7a620826dc5b7ff260b3694685f80d83 | 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 FactorialsAndPowersOfTwo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
Set<Long> set=new HashSet<>();
long factorial=1;
long current=1;
while(factorial<(long)1e12){
set.add(factorial);
factorial*=current;
current++;
}
candidates=new ArrayList<>(set);
Collections.sort(candidates);
while(t!=0){
solve(br,pr);
t--;
}
pr.flush();
pr.close();
}
static List<Long> candidates;
public static void solve(BufferedReader br,PrintWriter pr) throws IOException{
long n=Long.parseLong(br.readLine());
int k=getPower(n);
int mask=(1<<14)-1;
int subset=mask;
while(subset!=0){
long sum=0;
int count=0;
for(int j=0;j<14;j++){
if((subset&(1<<j))!=0){
count++;
sum+=candidates.get(j);
}
}
if(sum<=n){
k=Math.min(k,getPower(n-sum)+count);
}
subset=(subset-1)&mask;
}
pr.println(k==Integer.MAX_VALUE?-1:k);
}
public static int getPower(long num){
return Long.bitCount(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 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 | 3ec00103bb12adc2343d09b297f46ea6 | 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_Factorials_and_Powers_of_Two{
public static long fact[];
public static int max=15;
static Data f[]=new Data[(1<<15)];
public static void solve(long num){
int max_bit=0;
while(fact[max_bit]<=num){
max_bit++;
}//System.out.println((1<<max_bit));
int res=Integer.MAX_VALUE;
for(int i=0;i<(1<<max_bit);i++){
if(f[i].num<=num){
res=Math.min(res,f[i].nbit+bit_count(num-f[i].num));
}
}
System.out.println(res);
}
private static void factinit() {
f[0]=new Data(0,0);
for(int mask=1;mask<(1<<max);mask++){
int bit_c=bit_count(mask);
int msb=msb_bit(mask);
f[mask]=new Data(f[mask^(1<<msb)].num+fact[msb],bit_c);
// System.out.println(f[mask]);
}
}
public static int bit_count(long num){
int count=0;
while(num>0){
if((num&1)==1)count++;
num=num>>1;
}
return count;
}
public static int msb_bit(long num){
for(int i=63;i>=0;i--){
if((num&(1l<<i))>0)return i;
}
return 0;
}
public static void initfact(int num){
fact=new long[num+3];
fact[0]=1;
for(long i=1;i<=num;i++){
fact[(int)i]=fact[(int)i-1]*i;
// System.out.println(fact[(int)i]);
}
}
public static void main(String args[])throws IOException{
Reader sc=new Reader();
int t =sc.nextInt();
initfact(15);
factinit();
while(t-->0){
long num=sc.nextLong();
solve(num);
}
}
public static <K,V> Map<K,V> getLimitedSizedCache(int size){
/*Returns an unlimited sized map*/
if(size==0){
return (LinkedHashMap<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>());
}
/*Returns the map with the limited size*/
Map<K, V> linkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>() {
protected boolean removeEldestEntry(Map.Entry<K, V> eldest)
{
return size() > size;
}
});
return linkedHashMap;
}
}
class BinarySearch<T extends Comparable<T>> {
T ele[];
int n;
public BinarySearch(T ele[],int n){
this.ele=(T[]) ele;
Arrays.sort(this.ele);
this.n=n;
}
public int lower_bound(T x){
//Return next smallest element greater than ewqual to the current element
int left=0;
int right=n-1;
while(left<=right){
int mid=left+(right-left)/2;
if(x.compareTo(ele[mid])==0)return mid;
if(x.compareTo(ele[mid])>0)left=mid+1;
else right=mid-1;
}
if(left ==n)return -1;
return left;
}
public int upper_bound(T x){
//Returns the highest element lss than equal to the current element
int left=0;
int right=n-1;
while(left<=right){
int mid=left+(right-left)/2;
if(x.compareTo(ele[mid])==0)return mid;
if(x.compareTo(ele[mid])>0)left=mid+1;
else right=mid-1;
}
return right;
}
}
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[1000000]; // 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 Data implements Comparable<Data>{
long num;
int nbit;
public Data(long num,int nbit){
this.num=num;
this.nbit=nbit;
}
public int compareTo(Data o){
return (int)(-o.num+num);
}
public String toString(){
return num+" "+nbit;
}
}
class Binary{
public String convertToBinaryString(long ele){
StringBuffer res=new StringBuffer();
while(ele>0){
if(ele%2==0)res.append(0+"");
else res.append(1+"");
ele=ele/2;
}
return res.reverse().toString();
}
}
class FenwickTree{
int bit[];
int size;
FenwickTree(int n){
this.size=n;
bit=new int[size];
}
public void modify(int index,int value){
while(index<size){
bit[index]+=value;
index=(index|(index+1));
}
}
public int get(int index){
int ans=0;
while(index>=0){
ans+=bit[index];
index=(index&(index+1))-1;
}
return ans;
}
}
class PAndC{
long c[][];
long mod;
public PAndC(int n,long mod){
c=new long[n+1][n+1];
this.mod=mod;
build(n);
}
public void build(int n){
for(int i=0;i<=n;i++){
c[i][0]=1;
c[i][i]=1;
for(int j=1;j<i;j++){
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
}
class Trie{
int trie[][];
int revind[];
int root[];
int tind,n;
int sz[];
int drev[];
public Trie(){
trie=new int[1000000][2];
root=new int[600000];
sz=new int[1000000];
tind=0;
n=0;
revind=new int[1000000];
drev=new int[20];
}
public void add(int ele){
// System.out.println(root[n]+" ");
n++;
tind++;
revind[tind]=n;
root[n]=tind;
addimpl(root[n-1],root[n],ele);
}
public void addimpl(int prev_root,int cur_root,int ele){
for(int i=18;i>=0;i--){
int edge=(ele&(1<<i))>0?1:0;
trie[cur_root][1-edge]=trie[prev_root][1-edge];
sz[cur_root]=sz[trie[cur_root][1-edge]];
tind++;
drev[i]=cur_root;
revind[tind]=n;
trie[cur_root][edge]=tind;
cur_root=tind;
prev_root=trie[prev_root][edge];
}
sz[cur_root]+=sz[prev_root]+1;
for(int i=0;i<=18;i++){
sz[drev[i]]=sz[trie[drev[i]][0]]+sz[trie[drev[i]][1]];
}
}
public void findmaxxor(int l,int r,int x){
int ans=0;
int cur_root=root[r];
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
if(revind[trie[cur_root][1-edge]]>=l){
cur_root=trie[cur_root][1-edge];
ans+=(1-edge)*(1<<i);
}else{
cur_root=trie[cur_root][edge];
ans+=(edge)*(1<<i);
}
}
System.out.println(ans);
}
public void findKthStatistic(int l,int r,int k){
//System.out.println("In 3");
int curr=root[r];
int curl=root[l-1];
int ans=0;
for(int i=18;i>=0;i--){
for(int j=0;j<2;j++){
if(sz[trie[curr][j]]-sz[trie[curl][j]]<k)
k-=sz[trie[curr][j]]-sz[trie[curl][j]];
else{
curr=trie[curr][j];
curl=trie[curl][j];
ans+=(j)*(1<<i);
break;
}
}
}
System.out.println(ans);
}
public void findSmallest(int l,int r,int x){
//System.out.println("In 4");
int curr=root[r];
int curl=root[l-1];
int countl=0,countr=0;
// System.out.println(curl+" "+curr);
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
// System.out.println(trie[curl][edge]+" "+trie[curr][edge]+" "+sz[curl]+" "+sz[curr]);
if(edge==1){
countr+=sz[trie[curr][0]];
countl+=sz[trie[curl][0]];
}
curr=trie[curr][edge];
curl=trie[curl][edge];
}
countl+=sz[curl];
countr+=sz[curr];
System.out.println(countr-countl);
}
}
class Printer{
public <T> T printArray(T obj[] ,String details){
System.out.println(details);
for(int i=0;i<obj.length;i++)
System.out.print(obj[i]+" ");
System.out.println();
return obj[0];
}
public <T> void print(T obj,String details){
System.out.println(details+" "+obj);
}
}
class Node{
long weight;
int vertex;
public Node(int vertex,long weight){
this.vertex=vertex;
this.weight=weight;
}
public String toString(){
return vertex+" "+weight;
}
}
class Graph{
int nv; //0 indexing i.e vertices starts from 0 input as 1 indexed for add Edge
List<List<Node>> adj;
boolean visited[];
public Graph(int n){
adj=new ArrayList<>();
this.nv=n;
// visited=new boolean[nv];
for(int i=0;i<n;i++)
adj.add(new ArrayList<Node>());
}
public void addEdge(int u,int v,long weight){
u--;v--;
Node first=new Node(v,weight);
Node second=new Node(u,weight);
adj.get(v).add(second);
adj.get(u).add(first);
}
public void dfscheck(int u,long curweight){
visited[u]=true;
for(Node i:adj.get(u)){
if(visited[i.vertex]==false&&(i.weight|curweight)==curweight)
dfscheck(i.vertex,curweight);
}
}
long maxweight;
public void clear(){
this.adj=null;
this.nv=0;
}
public void solve() {
maxweight=(1l<<32)-1;
dfsutil(31);
System.out.println(maxweight);
}
public void dfsutil(int msb){
if(msb<0)return;
maxweight-=(1l<<msb);
visited=new boolean[nv];
dfscheck(0,maxweight);
for(int i=0;i<nv;i++)
{
if(visited[i]==false)
{maxweight+=(1<<msb);
break;}
}
dfsutil(msb-1);
}
// public boolean TopologicalSort() {
// top=new int[nv];
// int cnt=0;
// int indegree[]=new int[nv];
// for(int i=0;i<nv;i++){
// for(int j:adj.get(i)){
// indegree[j]++;
// }
// }
// Deque<Integer> q=new LinkedList<Integer>();
// for(int i=0;i<nv;i++){
// if(indegree[i]==0){
// q.addLast(i);
// }
// }
// while(q.size()>0){
// int tele=q.pop();
// top[tele]=cnt++;
// for(int j:adj.get(tele)){
// indegree[j]--;
// if(indegree[j]==0)
// q.addLast(j);
// }
// }
// return cnt==nv;
// }
// public boolean isBipartiteGraph(){
// col=new Integer[nv];
// visited=new boolean[nv];
// for(int i=0;i<nv;i++){
// if(visited[i]==false){
// col[i]=0;
// dfs(i);
// }
// }
}
class DSU{
int []parent;
int rank[];
int n;
public DSU(int n){
this.n=n;
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++)
{ parent[i]=i;
rank[i]=1;
}
}
public int find(int i){
if(parent[i]==i)return i;
return parent[i]=find(parent[i]);
}
public boolean union(int a,int b){
int pa=find(a);
int pb=find(b);
if(pa==pb)return false;
if(rank[pa]>rank[pb]){
parent[pb]=pa;
rank[pa]+=rank[pb];
}
else{
parent[pa]=pb;
rank[pb]+=rank[pa];
}
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 | f3d83d9f8559b4f145e5462471feab34 | 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.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.io.*;
public class A {
BufferedReader br;
StringTokenizer st;
public A(){ // constructor
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[] 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 long gcd(long a, long b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
static long mod = (int)1e9 + 7;
static int dir[][] = {{1, 0}, {-1, 0}, {0 ,1}, {0, -1}};
static boolean isPrime(long n){
if(n <= 1){
return false;
}
else if(n == 2){
return true;
}
else if(n % 2 == 0){
return false;
}
for(long i = 3; i * i <= n; i += 2){
if(n % i == 0){
return false;
}
}
return true;
}
static int findPar(int u, int par[])
{
if(par[u] == u)
{
return u;
}
return par[u] = findPar(par[u], par);
}
static void merge(int p1, int p2, int par[], int size[])
{
if(size[p1] > size[p2])
{
size[p1] += size[p2];
par[p2] = p1;
}
else
{
size[p2] += size[p1];
par[p1] = p2;
}
}
public static void main(String[] args) throws Exception{
A in = new A();
PrintWriter out = new PrintWriter(System.out);
/* array of arraylist
* ArrayList<Integer> arr[] = new ArrayList[n];
*/
/* If in any problem there are two operations --- like 2 ^ power, 4 ^ power and something can come common then first take all possible elements
* of both and add in the set and then take subsequence.
*/
long fact[] = new long[16];
fact[0] = 1;
for(int i = 1; i < 16; i++)
{
fact[i] = fact[i - 1] * i;
}
int test = in.nextInt();
while(test-- > 0)
{
long n = in.nextLong();
int min = 1000;
for(int i = 0; i < (1 << 15); i++)
{
long sum = 0;
int k = 0;
for(int j = 0; j < 15; j++)
{
if((i & (1 << j)) > 0)
{
k++;
sum += fact[j + 1];
}
}
if(sum <= n)
{
min = Math.min(min, binary(n - sum) + k);
}
}
out.println(min);
}
out.flush();
out.close();
}
static int binary(long n)
{
int cnt = 0;
while(n > 0)
{
cnt++;
n = n & (n - 1);
}
return cnt;
}
} | 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 | bcf5729e96b087a6a8d70125e248c466 | 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 cr[][]=new long[1001][1001];
//static double EPS = 1e-7;
static long mod=1000000007;
static long val=0;
public static void main(String[] args)
{
FScanner sc = new FScanner();
//Arrays.fill(prime, true);
//sieve();
//ncr();
int t=sc.nextInt();
ArrayList<Long> fac=new ArrayList<>();
solve(fac);
StringBuilder sb = new StringBuilder();
while(t-->0)
{
long n=sc.nextLong();
long result = Long.bitCount(n);
for(int mask=0;mask<(1<<14);mask++){
long sum=n;
for(int i=0;i<14;i++){
if((mask&(1<<i))!=0) {
sum -= fac.get(i+1);
}
}
if(sum<0) continue;
result = Math.min(result,Integer.bitCount(mask)+Long.bitCount(sum));
}
sb.append(result);
sb.append("\n");
}
System.out.println(sb.toString());
}
public static int binarycount(long val)
{
int count=0;
while(val>0 )
{
if( (val&1)==1 )
{
count++;
}
val=val>>1;
}
return count;
}
public static void solve(ArrayList<Long> fac)
{
long val=1;
for(int i=1;i<=15;i++)
{
val=val*i;
fac.add(val);
}
}
public static long gcd(long a,long b)
{
return b==0 ? a:gcd(b,a%b);
}
/* public static void sieve()
{ prime[0]=prime[1]=false;
int n=1000000;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
*/
public static void ncr()
{
cr[0][0]=1;
for(int i=1;i<=1000;i++)
{
cr[i][0]=1;
for(int j=1;j<i;j++)
{
cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod;
}
cr[i][i]=1;
}
}
}
class pair
//implements Comparable<pair>
{
int a;int b;
pair(int a,int b)
{
this.b=b;
this.a=a;
}
}
class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
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;
}
float nextFloat(){
return Float.parseFloat(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 | 9e8f4ca4b11d4c706d3237c0aeb6b90c | 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 codeMaster {
static long[] fac;
public static void tryAll(int i, long rem, HashSet<Pair> comb, Stack<Integer> tmp){
if(i >= fac.length){
comb.add(new Pair(rem, tmp.size()));
return;
}
if(rem - fac[i] >= 0){
tmp.add(1);
tryAll(i + 1, rem - fac[i], comb, tmp);
tmp.pop();
}
tryAll(i + 1, rem, comb, tmp);
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
fac = new long[15];
for(int i = 1; i<15; i++){
long prev = 1;
for(int j = i; j>=1; j--)prev*=j;
fac[i-1] = prev;
}
while(tc-->0){
long n = sc.nextLong();
HashSet<Pair> comb = new HashSet<>();
tryAll(0, n, comb, new Stack<>());
int ans = Integer.MAX_VALUE;
for(Pair x: comb){
int temp = 0;
for(int i = 0; i < 63; i++) {
if (((1L << i) & x.x) != 0) temp++;
}
ans = (int) Math.min(ans, temp + x.y);
}
pw.println(ans);
}
pw.flush();
}
public static boolean isSorted(Long[] arr){
boolean f = true;
for(int i = 1; i<arr.length; i++){
if(arr[i] < arr[i - 1]){
f = false;
break;
}
}
return f;
}
public static int binary_Search(long key, Long[] arr, int start){
int low = start;
int high = arr.length;
int mid = (low + high) / 2;
while(low < high){
mid = (low + high) / 2;
if(arr[mid] == key)break;
else if(arr[mid] > key){
high = mid;
}else{
low = mid;
}
}
return mid;
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
static class SegmentTree{
long[]array,sTree;
int N;
public SegmentTree(long arr[]) {
array=arr;
N=arr.length-1;
sTree=new long[N<<1];
}
void update(int idx){
array[idx]++;
idx+=N-1;
sTree[idx]++;
while(idx>1) {
idx>>=1;
sTree[idx]++;
}
}
void set(int idx, long val){
array[idx]++;
idx+=N-1;
sTree[idx]++;
while(idx>1) {
idx>>=1;
sTree[idx]++;
}
}
long query(int l,int r) {
return query(1,1,N,l,r);
}
long query(int node,int s,int e,int l,int r) {
if(l>e || r<s)return 0;
if(l<=s && r>=e)return sTree[node];
int mid=(s+e)>>1;
return query(node<<1,s,mid,l,r)+query(node<<1|1,mid+1,e,l,r);
}
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair implements Comparable<Pair>{
long x;
long y;
public Pair(long x, long y){
this.x = x;
this.y = y;
}
public int compareTo(Pair x){
return (int) (this.x - x.x);
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
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();
}
}
}
| 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 | 89253937689846d4fa59a916c2278cee | 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 int ans= Integer.MAX_VALUE;
public static void main(String[] args) {
MyScanner s = new MyScanner();
int t = s.nextInt();
ArrayList<Long> factorials = new ArrayList<>();
ArrayList<Long> powers = new ArrayList<>();
long a = 1;
long multiply = 2;
while(a<=1000000000000L)
{
factorials.add(a);
a = a*multiply;
multiply++;
}
a = 1;
while(a<=1000000000000L)
{
powers.add(a);
a = a<<1;
}
ArrayList<Item> facSums = new ArrayList<>();
subsetSums(factorials, 0,factorials.size()-1,0L,0,facSums);
for(int f = 0;f<t;f++)
{
long n = s.nextLong();
ans = Integer.MAX_VALUE;
for(int i = 0;i<facSums.size();i++)
{
long newn = n-facSums.get(i).val;
ans = Math.min(Long.bitCount(newn)+facSums.get(i).count,ans);
}
System.out.println(ans);
}
}
static void subsetSums(ArrayList<Long> arr, int l, int r, long sum,int count,ArrayList<Item> res)
{
if (l > r) {
res.add(new Item(count, sum));
return;
}
subsetSums(arr, l + 1, r, sum + arr.get(l),count+1,res);
subsetSums(arr, l + 1, r, sum,count,res);
}
// static void solve(ArrayList<Long> fac, ArrayList<Long> powers,long n,int steps)
// {
// if(n==0)
// {
// ans = Math.min(ans,steps);
// return;
// }
// for(long i:fac)
// {
// if(i<=n)
// {
// solve(fac, powers, n-i,steps+1);
// }else
// {
// break;
// }
// }
// for(long i:powers)
// {
// if(i<=n)
// {
// solve(fac, powers, n-i,steps+1);
// }else
// {
// break;
// }
// }
// }
static long findClosest(ArrayList<Long> fac,ArrayList<Long> powers,long n,HashSet<Long> set)
{
long bigFac = 1;
long bigPow = 1;
for(int i = 0;i<fac.size();i++)
{
if(set.contains(fac.get(i)))
{
bigFac = fac.get(i-1);
break;
}
if(fac.get(i)<n)
{
}else if(fac.get(i)==n)
{
return n;
}else
{
bigFac = fac.get(i-1);
break;
}
}
for(int i = 0;i<powers.size();i++)
{
if(set.contains(powers.get(i)))
{
bigPow = powers.get(i-1);
break;
}
if(powers.get(i)<n)
{
}else if(powers.get(i)==n)
{
return n;
}else
{
bigPow = powers.get(i-1);
break;
}
}
if(bigFac>bigPow)
{
return bigFac;
}else{
return bigPow;
}
}
}
class Item
{
int count;
long val;
Item(int count,long val)
{
this.count = count;
this.val = val;
}
}
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 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 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 | 8041a020f695428781969584961cd43a | 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.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class C {
/**
* Template @author William Fiset, [email protected]
*
*/
static InputReader in = new InputReader(System.in);
private static StringBuilder sb = new StringBuilder();
static PrintWriter out = new PrintWriter(System.out);
static class InputReader {
private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
private static final InputStream DEFAULT_STREAM = System.in;
private static final int MAX_DECIMAL_PRECISION = 21;
private int c;
private byte[] buf;
private int bufferSize, bufIndex, numBytesRead;
private InputStream stream;
private static final byte EOF = -1;
private static final byte NEW_LINE = 10;
private static final byte SPACE = 32;
private static final byte DASH = 45;
private static final byte DOT = 46;
private char[] charBuffer;
private static byte[] bytes = new byte[58];
private static int[] ints = new int[58];
private static char[] chars = new char[128];
static {
char ch = ' ';
int value = 0;
byte _byte = 0;
for (int i = 48; i < 58; i++) {
bytes[i] = _byte++;
}
for (int i = 48; i < 58; i++) {
ints[i] = value++;
}
for (int i = 32; i < 128; i++) {
chars[i] = ch++;
}
}
private static final double[][] doubles = {
{0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d,
0.0000000000d, 0.00000000000d, 0.000000000000d,
0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d,
0.00000000000000000d, 0.000000000000000000d,
0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d},
{0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d,
0.0000000001d, 0.00000000001d, 0.000000000001d,
0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d,
0.00000000000000001d, 0.000000000000000001d,
0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d},
{0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d,
0.0000000002d, 0.00000000002d, 0.000000000002d,
0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d,
0.00000000000000002d, 0.000000000000000002d,
0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d},
{0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d,
0.0000000003d, 0.00000000003d, 0.000000000003d,
0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d,
0.00000000000000003d, 0.000000000000000003d,
0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d},
{0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d,
0.0000000004d, 0.00000000004d, 0.000000000004d,
0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d,
0.00000000000000004d, 0.000000000000000004d,
0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d},
{0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d,
0.0000000005d, 0.00000000005d, 0.000000000005d,
0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d,
0.00000000000000005d, 0.000000000000000005d,
0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d},
{0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d,
0.0000000006d, 0.00000000006d, 0.000000000006d,
0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d,
0.00000000000000006d, 0.000000000000000006d,
0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d},
{0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d,
0.0000000007d, 0.00000000007d, 0.000000000007d,
0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d,
0.00000000000000007d, 0.000000000000000007d,
0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d},
{0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d,
0.0000000008d, 0.00000000008d, 0.000000000008d,
0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d,
0.00000000000000008d, 0.000000000000000008d,
0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d},
{0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d,
0.0000000009d, 0.00000000009d, 0.000000000009d,
0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d,
0.00000000000000009d, 0.000000000000000009d,
0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d}
};
public InputReader() {
this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE);
}
public InputReader(int bufferSize) {
this(DEFAULT_STREAM, bufferSize);
}
public InputReader(InputStream stream) {
this(stream, DEFAULT_BUFFER_SIZE);
}
public InputReader(InputStream stream, int bufferSize) {
if (stream == null || bufferSize <= 0) {
throw new IllegalArgumentException();
}
buf = new byte[bufferSize];
charBuffer = new char[128];
this.bufferSize = bufferSize;
this.stream = stream;
}
private byte read() throws IOException {
if (numBytesRead == EOF) {
throw new IOException();
}
if (bufIndex >= numBytesRead) {
bufIndex = 0;
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) {
return EOF;
}
}
return buf[bufIndex++];
}
private void doubleCharBufferSize() {
char[] newBuffer = new char[charBuffer.length << 1];
for (int i = 0; i < charBuffer.length; i++) {
newBuffer[i] = charBuffer[i];
}
charBuffer = newBuffer;
}
private int readJunk(int token) throws IOException {
if (numBytesRead == EOF) {
return EOF;
}
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > token) {
return 0;
}
bufIndex++;
}
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) {
return EOF;
}
bufIndex = 0;
} while (true);
}
public byte nextByte() throws IOException {
return (byte) nextInt();
}
public int nextInt() throws IOException {
if (readJunk(DASH - 1) == EOF) {
throw new IOException();
}
int sgn = 1, res = 0;
c = buf[bufIndex];
if (c == DASH) {
sgn = -1;
bufIndex++;
}
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
res = (res << 3) + (res << 1);
res += ints[buf[bufIndex++]];
} else {
bufIndex++;
return res * sgn;
}
}
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) {
return res * sgn;
}
bufIndex = 0;
} while (true);
}
public int[] nextIntArray(int n) throws IOException {
int[] ar = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = nextInt();
}
return ar;
}
public void close() throws IOException {
stream.close();
}
public long nextLong() throws IOException {
if (readJunk(DASH - 1) == EOF) {
throw new IOException();
}
int sgn = 1;
long res = 0L;
c = buf[bufIndex];
if (c == DASH) {
sgn = -1;
bufIndex++;
}
do {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
res = (res << 3) + (res << 1);
res += ints[buf[bufIndex++]];
} else {
bufIndex++;
return res * sgn;
}
}
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) {
return res * sgn;
}
bufIndex = 0;
} while (true);
}
public String nextString() throws IOException {
if (numBytesRead == EOF) {
return null;
}
if (readJunk(SPACE) == EOF) {
return null;
}
for (int i = 0;;) {
while (bufIndex < numBytesRead) {
if (buf[bufIndex] > SPACE) {
if (i == charBuffer.length) {
doubleCharBufferSize();
}
charBuffer[i++] = (char) buf[bufIndex++];
} else {
bufIndex++;
return new String(charBuffer, 0, i);
}
}
// Reload buffer
numBytesRead = stream.read(buf);
if (numBytesRead == EOF) {
return new String(charBuffer, 0, i);
}
bufIndex = 0;
}
}
}
public static void main(String args[]) throws Exception {
int t = in.nextInt();
while (t-- != 0) {
solve();
}
out.print(sb.toString());
out.close();
}
static void solve() throws IOException {
long n = in.nextLong();
long fact = 1;
List<Long> facts = new ArrayList();
int i = 1;
while (fact <= n) {
facts.add(fact);
i++;
fact *= i;
}
int min = countSetBits(n);
int m = facts.size();
for (i = 0; i <= m; i++) {
boolean[] b = new boolean[m];
List<Long> list = new ArrayList();
subset(facts,i,0,0,b, list);
for (long j : list) {
min = Math.min(min, n >= j ? i + countSetBits(n - j) : min);
}
}
sb.append(min).append("\n");
}
static int countSetBits(long n) {
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
public static void subset(List<Long> list, int k, int start, int currLen, boolean[] used, List<Long> hehe) {
if (currLen == k) {
long sum = 0;
for (int i = 0; i < list.size(); i++) {
if (used[i] == true) {
sum += list.get(i);
}
}
hehe.add(sum);
return;
}
if (start == list.size()) {
return;
}
// For every index we have two options,
// 1.. Either we select it, means put true in used[] and make currLen+1
used[start] = true;
subset(list, k, start + 1, currLen + 1, used, hehe);
// 2.. OR we dont select it, means put false in used[] and dont increase
// currLen
used[start] = false;
subset(list, k, start + 1, currLen, used, hehe);
}
}
| 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 | 50c5a481e9a37d49a2bca80aa3e9d3a3 | 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 tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n, l, k;
static TreeSet<Integer>[] ad, ad1, ad2;
static ArrayList<int[]>[] quer;
static int[][] remove, add;
static int[][] memo;
static boolean vis[];
static long[] inv, f, ncr[];
static HashMap<Integer, Integer> hm;
static int[] pre, suf, Smax[], Smin[];
static int idmax, idmin;
static ArrayList<Integer> av;
static HashMap<Integer, Integer> mm;
static boolean[] msks;
static int[] lazy[], lazyCount;
static int[] a, b;
static long ans, tem, c;
static ArrayList<Integer> gl;
static int[] ar;
static String s;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
TreeSet<Long> fac = new TreeSet<>();
long k = 1;
long[] f = new long[16];
f[0] = 1;
for (long i = 1; i <= 15; i++) {
k *= i;
// System.out.println(Long.toBinaryString(k));
fac.add(k);
f[(int) i] = k;
}
TreeMap<Long, Integer> sums = new TreeMap<>();
int mask = 0;
while (mask < 1 << 16) {
long sum = 0;
for (int b = 0; b < 16; b++)
if (((1 << b) & mask) != 0) {
sum += f[b];
}
sums.put(sum, Long.bitCount(mask));
mask++;
}
while (t-- > 0) {
long n = sc.nextLong();
int ans = Long.bitCount(n);
for (long w : sums.keySet()) {
if (w <= n)
ans = Math.min(ans, Long.bitCount(n - w) + sums.get(w));
}
out.println(ans);
}
out.flush();
}
// 1 2 6 24
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
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 double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].longValue();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | 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 | 0472a28b3e40424bc52ea1e143ee167f | 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 long n ;
static int min ;
public static void solve(int idx, int cnt, long sum){
if(idx == arr.length){
long x = n-sum ;
min = Math.min(min, cnt+Long.bitCount(x)) ;
return ;
}
if(sum+arr[idx]<= n)
solve(idx+1, cnt+1, sum+arr[idx]) ;
solve(idx+1, cnt, sum) ;
}
public static void doJob() throws Exception{
//select between doJob and doJobT
n = sc.nextLong() ;
min = Integer.MAX_VALUE ;
solve(0,0,0) ;
pw.println(min) ;
}
public static void doJobT() throws Exception{
arr = new Long [12] ;
arr[0] = 6L;
int num = 4 ;
for(int i=1 ; i<arr.length ; i++){
arr[i] = num * arr[i-1] ;
num++;
}
int t = sc.nextInt() ;
while(t-->0){
doJob() ;
}
}
public static void main(String[] args) throws Exception{
sc = new Scanner(System.in) ;
pw = new PrintWriter(System.out) ;
//Thread.sleep(2000);
//doJob() ;
doJobT();
pw.flush();
pw.close();
}
/*-----------------------------------------Helpers--------------------------------------------*/
static Scanner sc;
static PrintWriter pw ;
static class Scanner{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String r) throws Exception {
br = new BufferedReader(new FileReader(new File(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 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();
}
}
//for descending order
static Comparator<PairI> cI = (p1,p2) -> (p1.x==p2.x)?p2.y-p1.y : p2.x-p1.x ;
static Comparator<PairL> cL =
(p1,p2) -> (p1.x==p2.x)? Long.compare(p2.y, p1.y) : Long.compare(p2.x, p1.x) ;
//ascending (by default)
static class PairI implements Comparable<PairI>{
int x;
int y;
public PairI(int xx, int yy){
x = xx;
y = yy;
}
public String toString(){
return "("+x + " " + y +")" ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PairI other = (PairI) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public int compareTo(PairI other) {
// TODO Auto-generated method stub
return (this.x==other.x)? this.y - other.y : this.x - other.x ;
}
}
static class PairL implements Comparable <PairL> {
long x;
long y;
public PairL(long x, long y) {
this.x = x;
this.y = y;
}
public String toString(){
return "("+x + " " + y +")" ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (x ^ (x >>> 32));
result = prime * result + (int) (y ^ (y >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PairL other = (PairL) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public int compareTo(PairL other) {
// TODO Auto-generated method stub
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.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 | 915a1e3a45a0e0e0a3ecfcced25ca134 | 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 long n ;
static int min ;
public static void solve(int idx, int msk, long sum){
if(idx == arr.length){
long x = n-sum ;
min = Math.min(min, Integer.bitCount(msk)+Long.bitCount(x)) ;
return ;
}
if(sum+arr[idx]<= n)
solve(idx+1, msk|(1<<idx), sum+arr[idx]) ;
solve(idx+1, msk, sum) ;
}
public static void doJob() throws Exception{
//select between doJob and doJobT
n = sc.nextLong() ;
min = Integer.MAX_VALUE ;
solve(0,0,0) ;
pw.println(min) ;
}
public static void doJobT() throws Exception{
arr = new Long [12] ;
arr[0] = 6L;
int num = 4 ;
for(int i=1 ; i<arr.length ; i++){
arr[i] = num * arr[i-1] ;
num++;
}
int t = sc.nextInt() ;
while(t-->0){
doJob() ;
}
}
public static void main(String[] args) throws Exception{
sc = new Scanner(System.in) ;
pw = new PrintWriter(System.out) ;
//Thread.sleep(2000);
//doJob() ;
doJobT();
pw.flush();
pw.close();
}
/*-----------------------------------------Helpers--------------------------------------------*/
static Scanner sc;
static PrintWriter pw ;
static class Scanner{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String r) throws Exception {
br = new BufferedReader(new FileReader(new File(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 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();
}
}
//for descending order
static Comparator<PairI> cI = (p1,p2) -> (p1.x==p2.x)?p2.y-p1.y : p2.x-p1.x ;
static Comparator<PairL> cL =
(p1,p2) -> (p1.x==p2.x)? Long.compare(p2.y, p1.y) : Long.compare(p2.x, p1.x) ;
//ascending (by default)
static class PairI implements Comparable<PairI>{
int x;
int y;
public PairI(int xx, int yy){
x = xx;
y = yy;
}
public String toString(){
return "("+x + " " + y +")" ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PairI other = (PairI) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public int compareTo(PairI other) {
// TODO Auto-generated method stub
return (this.x==other.x)? this.y - other.y : this.x - other.x ;
}
}
static class PairL implements Comparable <PairL> {
long x;
long y;
public PairL(long x, long y) {
this.x = x;
this.y = y;
}
public String toString(){
return "("+x + " " + y +")" ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (x ^ (x >>> 32));
result = prime * result + (int) (y ^ (y >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PairL other = (PairL) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public int compareTo(PairL other) {
// TODO Auto-generated method stub
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.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 | 977b006f810d02cab26db3fc6a096253 | 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 Ac {
static int MOD = 998244353;
static int MAX = (int)1e8;
static Random rand = new Random();
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = i();
// out.println();
// out.println();
// randArr(10, 10, 10);
long max = (long)1e12;
Set<Long> set = new HashSet<>();
int i = 1;
long cur = 1;
set.add(cur);
while (true){
cur *= i;
i++;
if(cur > max){
break;
}else{
set.add(cur);
}
}
long[] arr = new long[set.size()];
int idx = 0;
for (long j : set){
arr[idx++] = j;
}
// for (long j : arr){
// out.println(j);
// }
// out.println(arr[1] + " " + arr[5]);
while (t-- > 0){
out.println(sol(arr));
// int[] ans = sol();
// for (int i = 0; i < ans.length; i++){
// out.print(ans[i] + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
out.flush();
out.close();
}
static int sol(long[] arr){
int n = arr.length;
long k = l();
int ans = 10000;
// out.println((long)1 << 52);
// out.println(Integer.MAX_VALUE);
head:for (int i = 0; i < 1 << n; i++){
// out.println(i);
long sum = k;
int bit = 0;
for (int j = 0; j < n; j++){
int cur = (i >> j) & 1;
if (cur == 1){
sum -= arr[j];
// out.println(arr[j]);
}
bit+=cur;
if (sum == 0){
if (bit < ans){
ans = bit;
}
continue head;
} else if (sum < 0){
continue head;
}
}
// while (sum != 0){
// sum &= sum - 1;
// bit++;
// }
bit += Long.bitCount(sum);
if(bit < ans){
ans = bit;
}
}
return ans ;
}
static void randArr(int num, int n, int val){
for (int i = 0; i < num; i++){
int len = rand.nextInt(n + 1);
System.out.println(len);
for (int j = 0; j < len; j++){
System.out.print(rand.nextInt(val) + 1 + " ");
}
System.out.println();
}
}
static void shuffle(long a[], int n){
for (int i = 0; i < n; i++) {
int t = (int)Math.random() * a.length;
long x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static int gcd(int a, int b){
return a % b == 0 ? a : gcd(b, a % b);
}
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 int[] inputI(int n) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i();
}
return nums;
}
static long[] inputL(int n) {
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = l();
}
return nums;
}
}
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 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 | c6aa3944785d8ae516fc6efcf4bc58e0 | 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 Ac {
static int MOD = 998244353;
static int MAX = (int)1e8;
static Random rand = new Random();
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = i();
// out.println();
// out.println();
// randArr(10, 10, 10);
long max = (long)1e12;
Set<Long> set = new HashSet<>();
int i = 1;
long cur = 1;
set.add(cur);
while (true){
cur *= i;
i++;
if(cur > max){
break;
}else{
set.add(cur);
}
}
long[] arr = new long[set.size()];
int idx = 0;
for (long j : set){
arr[idx++] = j;
}
// for (long j : arr){
// out.println(j);
// }
// out.println(arr[1] + " " + arr[5]);
while (t-- > 0){
out.println(sol(arr));
// int[] ans = sol();
// for (int i = 0; i < ans.length; i++){
// out.print(ans[i] + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
out.flush();
out.close();
}
static int sol(long[] arr){
int n = arr.length;
long k = l();
int ans = 10000;
// out.println((long)1 << 52);
// out.println(Integer.MAX_VALUE);
head:for (int i = 0; i < 1 << n; i++){
// out.println(i);
long sum = k;
int bit = 0;
for (int j = 0; j < n; j++){
int cur = (i >> j) & 1;
if (cur == 1){
sum -= arr[j];
// out.println(arr[j]);
}
bit+=cur;
if (sum == 0){
if (bit < ans){
ans = bit;
}
continue head;
} else if (sum < 0){
continue head;
}
}
while (sum != 0){
sum &= sum - 1;
bit++;
}
if(bit < ans){
ans = bit;
}
}
return ans ;
}
static void randArr(int num, int n, int val){
for (int i = 0; i < num; i++){
int len = rand.nextInt(n + 1);
System.out.println(len);
for (int j = 0; j < len; j++){
System.out.print(rand.nextInt(val) + 1 + " ");
}
System.out.println();
}
}
static void shuffle(long a[], int n){
for (int i = 0; i < n; i++) {
int t = (int)Math.random() * a.length;
long x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static int gcd(int a, int b){
return a % b == 0 ? a : gcd(b, a % b);
}
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 int[] inputI(int n) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i();
}
return nums;
}
static long[] inputL(int n) {
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = l();
}
return nums;
}
}
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 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 | 1bb403ed971c0c04973f560746166c81 | 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 Practice1 {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
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];
}
int nC = input.nextInt();
for (int loop=0; loop<nC; loop++) {
long inp = input.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]));
}
out.println(res);
}
}
out.close();
}
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 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 | 672d22f9003f977cbffa7ec67387c3b7 | 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.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) throws IOException {
initReader();
int m=nextInt();
long[]f=new long[20];
f[1]=1;f[0]=1;
for(int i=2;i<=15;i++)f[i]=f[i-1]*i;
while (m--!=0){
long x=nextLong();
int min=INF;
int sum=(1<<15);
for(int i=0;i<sum;i++){
int cnt=0;
long t=x;
for(int j=0;j<=14;j++){
if((i>>j&1)==1){
t-=f[j];
cnt++;
}
}
if(t<0)continue;
while (t!=0){
if((t&1)==1)cnt++;
t>>=1;
}
min=Math.min(min,cnt);
}
pw.println(min);
}
pw.close();
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
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 char nextChar() throws IOException {
return next().charAt(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 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 | a55509090e47a600f0cc813d96be0c1e | 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 static java.lang.Math.*;
import java.io.*;
public class S {
public static int surv = 0;
public static long f[];
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
f = new long[14];
long st = 1;
int razy = 2;
for(int i = 0; i < f.length; i++){
f[i] = st;
st *= razy;
razy++;
}
while(t > 0){
t--;
long n = Long.parseLong(br.readLine());
solve(n, sb);
}
System.out.println(sb.toString());
}
final static int MOD = 1000000007;
public static void solve(long n, StringBuilder sb){
int ans = perm(n, 0, 0);
sb.append(ans + "\n");
}
public static int perm(long n, int level, int cnt){
//System.out.println(n);
if(n == 0){
return cnt;
}
if(n < 0){
return Integer.MAX_VALUE;
}
if(level == f.length){
if(n < 0)return Integer.MAX_VALUE;
int ans = cnt + checkP(n);
return ans;
}
int first = perm(n - f[level], level + 1, cnt + 1);
int second = perm(n, level + 1, cnt);
return Math.min(first, second);
}
public static int checkP(long n){
int ans = 0;
while(n > 0){
ans += (n & 1);
n >>= 1;
}
return ans;
}
public static List<int[]> primeFactorization(int n){
List<Integer> primes = sievePrimes(1000000);
List<int[]> ans = new ArrayList<int[]>();
for(int i = 0; (long)primes.get(i)*(long)primes.get(i) <= n; i++){
int curr = primes.get(i);
if(n % curr == 0){
int a[] = new int[]{curr, 0};
while(n % curr == 0){
n /= curr;
a[1]++;
}
ans.add(a);
}
}
if(n > 1){
ans.add(new int[]{n, 1});
}
return ans;
}
public static List<Integer> sievePrimes(int limit){
boolean p[] = new boolean[limit+1];
for(int i = 2; i*i <= p.length; i++){
if(!p[i]){
for(int j = i*i; j < p.length; j += i){
p[j] = true;
}
}
}
List<Integer> primes = new ArrayList<Integer>();
for(int i = 2; i < p.length; i++){
if(!p[i])primes.add(i);
}
return primes;
}
public static long powerr(long base, long exp){
if(exp < 2)return base;
if(exp % 2 == 0){
long ans = powerr(base, exp/2) % MOD;
ans *= ans;
ans %= MOD;
return ans;
}else{
return (((powerr(base, exp-1)) % MOD) * base) % MOD;
}
}
public static long power(long a, long b){
if(b == 0)return 1l;
long ans = power(a, b/2);
ans *= ans;
ans %= MOD;
if(b % 2 != 0){
ans *= a;
}
return ans % MOD;
}
public static int logLong(long a){
int ans = 0;
long b = 1;
while(b < a){
b*=2;
ans++;
}
return ans;
}
public static void sort(int a[]){
List<Integer> l = new ArrayList<Integer>();
for(int val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(int val : l){
a[k++] = val;
}
}
public static void sortLong(long a[]){
List<Long> l = new ArrayList<Long>();
for(long val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(long val : l){
a[k++] = val;
}
}
public static boolean isPal(String s){
int l = 0;
int r = s.length() - 1;
while(l <= r){
if(s.charAt(l) != s.charAt(r)){
return false;
}
l++;
r--;
}
return true;
}
/* public static int gcd(int a, int b){
if(b > a){
int temp = a;
a = b;
b = temp;
}
if(b == 0)return a;
return gcd(b, a%b);
}*/
public static long gcd(long a, long b){
if(b > a){
long temp = a;
a = b;
b = temp;
}
if(b == 0)return a;
return gcd(b, a%b);
}
// public static long lcm(long a, long b){
// return a * b/gcd(a, b);
// }
/*public static class DJSet{
public int a[];
public DJSet(int n){
this.a = new int[n];
Arrays.fill(a, -1);
}
public int find(int val){
if(a[val] >= 0){
a[val] = find(a[val]);
return a[val];
}else{
return val;
}
}
public boolean union(int val1, int val2){
int p1 = find(val1);
int p2 = find(val2);
if(p1 == p2){
return false;
}
int size1 = Math.abs(a[p1]);
int size2 = Math.abs(a[p2]);
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
return true;
}*/
/*
public static class DSU{
public int a[];
public DSU(int size){
this.a = new int[size];
Arrays.fill(a, -1);
}
public int find(int u){
if(a[u] < 0)return u;
a[u] = find(a[u]);
return a[u];
}
public boolean union(int u, int v){
int p1 = find(u);
int p2 = find(v);
if(p1 == p2)return false;
int size1 = a[p1] * -1;
int size2 = a[p2] * -1;
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
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 | e45316b6164810b124197c97b698fad5 | 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 long setBitCount(long n){
long c=0;
while(n>0){
n=n&(n-1);
c++;
}
return c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
long n=sc.nextLong();
long max= 1000000000000l;
ArrayList<Long> fact=new ArrayList<>();
long f=6,num=4;
fact.add(1l);
fact.add(2l);
while(f<=1000000000000l){
fact.add(f);
f*=num;
num++;
}
long ans=Long.MAX_VALUE;
for(int i=0;i<(1<<14);i++){
long sum=0,mask=0;
for(int j=0;j<14;j++){
int b=(1<<j);
if((i&b)>0){
sum+=fact.get(j);
mask++;
}
}
if(sum>n){
break;
}
long temp=mask + setBitCount(n-sum);
ans=Math.min(ans,temp);
}
if(ans==Long.MAX_VALUE)
System.out.println(-1);
else
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 | 16909285607f297deb83198aa183e6f2 | 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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import javax.print.DocFlavor.INPUT_STREAM;
public class Main {
public static void main(String[] args) throws Exception {
Sol obj=new Sol();
obj.runner();
}
}
class Sol{
FastScanner fs=new FastScanner();
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
void runner() throws Exception{
int T=1;
//T=sc.nextInt();
preCompute();
T=fs.nextInt();
while(T-->0) {
solve(T);
}
out.close();
System.gc();
}
Map<Long, Integer> map=new HashMap<>();
private void preCompute() {
// TODO Auto-generated method stub
long x=1;
long mul=2;
ArrayList<Long> list=new ArrayList<>();
while( x < Math.pow(10, 13) ) {
list.add(x);
x=x*mul;
mul++;
}
combination( list , 0 ,0, 0);
}
private void combination(ArrayList<Long> list2, int i, long sum , int cnt) {
if( i==list2.size() ) {
return;
}
if( map.getOrDefault(sum, Integer.MAX_VALUE) > cnt )
map.put( sum , cnt);
combination(list2, i+1, sum+list2.get(i) , cnt+1 );
combination(list2, i+1, sum , cnt);
}
private void solve(int T) throws Exception {
long n=fs.nextLong();
long ans=Long.bitCount(n);
for( long key : map.keySet() ) {
if( key <= n ) {
ans=Math.min( ans , map.get( key ) + Long.bitCount( n- key ) );
}
}
System.out.println(ans);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st ;
FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
FastScanner(String file) {
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
st = new StringTokenizer("");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
}
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());
}
String readLine() throws IOException{
return br.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 | c5ffbab6deee3b4175f89e3849339353 | 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 Program {
public static void println(String str) {
System.out.println(str);
}
public static void println(int str) {
System.out.println(str);
}
public static void print(String str) {
System.out.print(str);
}
public static void printArr(int[] arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
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;
}
}
public static void main(String[] args){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
// code
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int tt=0; tt<t; tt++) {
long n = sc.nextLong();
// int[] arr = new int[n];
// for(int i=0;i<n;i++) {
// arr[i] = sc.nextInt();
// }
find(n);
}
return;
}
public static int find(long n) {
List<Long> facts = new ArrayList();
long fact = 1L;
int num = 2;
while(num<=16) {
facts.add(fact);
fact *= num;
num++;
}
// println(facts.toString());
Map<Long, Integer> factNums = new HashMap();
getAllComb(factNums, 0, facts, 0, 0);
factNums.put(0L, 0);
// println(facts.toString());
// println(factNums.size());
// println(factNums.toString());
int answer = Integer.MAX_VALUE;
for(long f: factNums.keySet()) {
long diff = n - f;
if(diff>=0) {
String w = Long.toBinaryString(diff);
int num2 = 0;
for (int i = 0; i < w.length(); i++) {
num2 += (w.charAt(i) - '0');
}
// System.out.println(num2+"XXX"+twoSumBinary);
int count = num2+factNums.get(f);
answer = Math.min(answer, count);
// System.out.println(answer+"ZZ"+twoSumBinary);
}
}
System.out.println(answer);
return 0;
}
public static void getAllComb(Map<Long, Integer> factNums, int start, List<Long> facts, long prevSum, int count) {
for(int i=start;i<facts.size();i++) {
long sum = prevSum+facts.get(i);
factNums.put(sum, count+1);
getAllComb(factNums, i+1, facts, sum, count+1);
}
}
} | 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 | b0beb96186253b1274e9de7ce332de47 | 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 {
static class Pair
{
int f;int s; //
Pair(){}
Pair(int f,int s){ this.f=f;this.s=s;}
}
static class Fast {
BufferedReader br;
StringTokenizer st;
public Fast() {
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());
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readArray1(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/* static long noOfDivisor(long a)
{
long count=0;
long t=a;
for(long i=1;i<=(int)Math.sqrt(a);i++)
{
if(a%i==0)
count+=2;
}
if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a)))
{
count--;
}
return count;
}*/
static boolean isPrime(long a) {
for (long i = 2; i <= (long) Math.sqrt(a); i++) {
if (a % i == 0)
return false;
}
return true;
}
static void primeFact(int n) {
int temp = n;
HashMap<Integer, Integer> h = new HashMap<>();
for (int i = 2; i * i <= n; i++) {
if (temp % i == 0) {
int c = 0;
while (temp % i == 0) {
c++;
temp /= i;
}
h.put(i, c);
}
}
if (temp != 1)
h.put(temp, 1);
}
static void reverseArray(int a[]) {
int n = a.length;
for (int i = 0; i < n / 2; i++) {
a[i] = a[i] ^ a[n - i - 1];
a[n - i - 1] = a[i] ^ a[n - i - 1];
a[i] = a[i] ^ a[n - i - 1];
}
}
static int setbits(long n)
{
int r=0;
long t=n;
while(t>0)
{
if((t&1)==1)
r++;
t=t>>1;
}
return r;
}
static long factless(long n)
{
int f=1;
for(int i=1;f*i<=n;)
{
f=f*i;
i++;
}
return f;
}
static int rec(long a,HashSet<Long> hs)
{
if(a==0)
return 0;
if(a==1||(a&(a-1))==0)
return 1;
long a1=factless(a);
hs.add(a1);
if(!hs.contains(a-a1))
return 1+rec(a-a1,hs);
else
return 1+setbits(a-a1);
}
static long factorial(int n)
{
if(n<2)
return 1;
return n*factorial(n-1);
}
public static void main(String args[]) throws IOException {
Fast sc = new Fast();
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while (t1-- > 0)
{
long n=sc.nextLong();
int ans=Integer.MAX_VALUE;
long temp=n;
long fact[]=new long[15];
for(int i=0;i<fact.length;i++)
{
fact[i]=factorial(i);
}
for(int i=0;i<(1<<14);i++)
{
long s1=0;int c=0;
for(int j=0;j<14;j++)
{
if((i&(1<<j))!=0)
{
s1 += fact[j+1];
c++;
}
}
if(s1<=n)
{
int c1=setbits(n-s1);
c+=c1;
if(ans>c)
{
ans=c;
// out.println(c+" "+c1+" "+s1);
}
}
}
out.println(ans);
/* HashSet<Long> hs=new HashSet<>();
// while(temp>0)
// {
int sbit=setbits(n);
int s1=rec(temp,hs);
// }
ans=Math.min(sbit,s1);
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 | b13f3248d2991e3a9c175629256ea39a | 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 static java.lang.Math.*;
public class Main {
static Scanner scn = new Scanner(System.in);
static StringBuilder sb = new StringBuilder();
public static void main(String[] ScoobyDoobyDo) {
long[] fact = new long[16];
fact[0] = 1L; fact[1] = 1L;
for(int i = 2; i < 16; i++) fact[i] = fact[i - 1] * (long) i;
List<Long> f = new ArrayList<>();
for(int i = 1; i <= 15; i++) f.add(fact[i]);
store = new ArrayList<>();
rec(f, 0, 0L, 0, f.size());
Collections.sort(store, new Comparator<long[]>(){
public int compare(long[] x, long[] y) {
if(x[0] > y[0]) return 1;
else if(x[0] < y[0]) return -1;
else return 0;
}
});
int t = scn.nextInt();
for(int tests = 0; tests < t; tests++) solve();
System.out.println(sb);
}
public static void solve() {
long n = scn.nextLong();
long[] bits = go(n);
long cnt = 0;
for(int i = 0; i < 64; i++) cnt += bits[i];
int sz = store.size();
long res = cnt;
for(int i = 0; i < sz; i++) {
if(store.get(i)[0] > n) break;
long[] b = go(n - store.get(i)[0]);
cnt = 0;
for(int j = 0; j < 64; j++) if(b[j] == 1) cnt++;
res = min(res, store.get(i)[1] + cnt);
}
sb.append(res + "\n");
}
static List<long[]> store;
public static void rec(List<Long> f, int i, long s, int cnt, int n) {
if(i == n && cnt != 0) {
store.add(new long[] {s, cnt});
return;
}
if(i == n) return;
rec(f, i + 1, s + f.get(i), cnt + 1, n);
rec(f, i + 1, s, cnt, n);
}
public static long[] go(long x) {
long[] bits = new long[64];
for(int i = 0; i < 64; i++)
bits[64 - i - 1] = ((x >> (long) i) & 1L);
return bits;
}
} | 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 | 4d3942a4c648fa6c47901e992db689b1 | 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 CF.CF774;
import java.io.*;
import java.util.*;
import java.math.*;
/**
* @Author: merickbao
* @Created_Time: 2022-03-05 20:29
* @Description:
*/
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
// code here
Task solver = new C();
solver.solve(1, in, out);
out.close();
}
}
class C implements Task {
static long[] f = {
1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L,
3628800L, 39916800L, 479001600L, 6227020800L, 87178291200L
};
@Override
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.readInt();
while (t-- > 0) {
long n = in.readLong();
int ans = Long.bitCount(n);
for (int mask = 1; mask < 1 << f.length; mask++) {
if (Integer.bitCount(mask) >= ans) continue;
long cnt = 0;
for (int j = 0; j < f.length; j++) {
if (((mask >> j) & 1) == 1) {
cnt += f[j];
if (cnt > n) break;
}
}
if (cnt <= n) {
ans = Math.min(ans, Integer.bitCount(mask) + Long.bitCount(n - cnt));
}
}
System.out.println(ans);
}
}
}
interface Task {
public void solve(int testNumber, InputReader in, OutputWriter out);
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public int readInt() {
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 readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
public InputReader.SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(InputReader.SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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(char[] array) {
writer.print(array);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(long[] array) {
print(array);
writer.println();
}
public void printLine() {
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void print(char i) {
writer.print(i);
}
public void printLine(char i) {
writer.println(i);
}
public void printLine(char[] array) {
writer.println(array);
}
public void printFormat(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
public void print(long i) {
writer.print(i);
}
public void printLine(long i) {
writer.println(i);
}
public void print(int i) {
writer.print(i);
}
public void printLine(int i) {
writer.println(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 | dcc325f5645f859d5e97fbcba8ee8847 | 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 CF.CF774;
import java.io.*;
import java.util.*;
import java.math.*;
/**
* @Author: merickbao
* @Created_Time: 2022-03-05 20:29
* @Description:
*/
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
// code here
Task solver = new C();
solver.solve(1, in, out);
out.close();
}
}
class C implements Task {
static long[] f = {
1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L,
3628800L, 39916800L, 479001600L, 6227020800L, 87178291200L
};
@Override
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.readInt();
while (t-- > 0) {
long n = in.readLong();
int ans = Long.bitCount(n);
for (int mask = 1; mask < 1 << f.length; mask++) {
long cnt = 0;
for (int j = 0; j < f.length; j++) {
if (((mask >> j) & 1) == 1) {
cnt += f[j];
if (cnt > n) break;
}
}
if (cnt <= n) {
ans = Math.min(ans, Integer.bitCount(mask) + Long.bitCount(n - cnt));
}
}
System.out.println(ans);
}
}
}
interface Task {
public void solve(int testNumber, InputReader in, OutputWriter out);
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public int readInt() {
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 readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return readString();
}
public InputReader.SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(InputReader.SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
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(char[] array) {
writer.print(array);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(long[] array) {
print(array);
writer.println();
}
public void printLine() {
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void print(char i) {
writer.print(i);
}
public void printLine(char i) {
writer.println(i);
}
public void printLine(char[] array) {
writer.println(array);
}
public void printFormat(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
public void print(long i) {
writer.print(i);
}
public void printLine(long i) {
writer.println(i);
}
public void print(int i) {
writer.print(i);
}
public void printLine(int i) {
writer.println(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 | e2e3e2b5607fb5a1abafcfc8cc136cfc | 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 FactorialsandPowerofTwo{
public static void main(String[] args) {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Scanner sc= new Scanner (System.in);
//Code From Here----
int t =fr.nextInt();
long [] arr = new long [16];
arr[0]=6;
for (int i = 1; i < arr.length; i++) {
arr[i]=arr[i-1]*(i+3);
}
while (t-->0) {
long n = fr.nextLong();
if(n==1)
{
out.println(1);
}
else
{
long ans = Long.MAX_VALUE;
for (long i = 0; i < (1<<16); i++) {
long temp = n;
for (int j = 0 ; j < 16; j++) {
if (((i>>j)&1) == 1) {
if(temp-arr[j]>=0)
temp-=arr[j];
}
}
ans=Math.min(ans,Long.bitCount(temp)+Long.bitCount(i));
}
if(ans==Long.MAX_VALUE)
{
out.println(-1);
}
else
{
out.println(ans);
}
}
}
out.flush();
sc.close();
}
//This RadixSort() is for long method
public static long[] radixSort(long[] f){ return radixSort(f, f.length); }
public static long[] radixSort(long[] f, int n)
{
long[] to = new long[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
long[] d = f; f = to; to = d;
}
return f;
}
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 int lowerBound(long a[], long 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(long a[], long x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1L;
if (a[m] <= x) l = m;
else r = m;
}
return l + 1;
}
static int upperBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k >= arr[mid]) {
start = mid + 1;
} else {
end = mid;
}
}
if (start < N && arr[start] <= k) {
start++;
}
return start;
}
static long lowerBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k <= arr[mid]) {
end = mid;
} else {
start = mid + 1;
}
}
if (start < N && arr[start] < k) {
start++;
}
return start;
}
// Bitwise and for long
private static final byte[] bitPositions(long n) {
final byte[] result = new byte[16];
for (int i = 0; n != 0L; i++) {
result[i] = (byte) ((byte) Long.numberOfTrailingZeros(n) + 1);
n &= n - 1L; // Change least-significant one bit to a zero bit.
}
return result;
}
// For Fast Input ----
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 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 | 8032b3e0325f9a2f39eb00aeaa99c10e | 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.*;
// import java.math.*;
public class Main {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
// static long mod=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
static ArrayList<Integer> graph[];
// static ArrayList<ArrayList<Integer>> graph;
static long fact[];
static int seg[];
static int dp[][];
// static long dp[][];
static HashSet<Long> hs;
public static void main (String[] args) throws java.lang.Exception
{
// code goes here
int t=I();
outer:while(t-->0)
{
long n=L();
ArrayList<Long> arr=new ArrayList<>();
long prod=1;
for(int i=2;i<=15;i++){
prod*=i;
if((prod&(prod-1))>0){
arr.add(prod);
}
}
out.println(fun(arr,0,n));
}
out.close();
}
public static int fun(ArrayList<Long> arr,int i,long n)
{
if(i==arr.size()){
int cnt=0;
for(int j=0;j<40;j++){
if((n&(1L<<j))>0){
cnt++;
}
}
return cnt;
}
if(arr.get(i)<=n){
int p1=1+fun(arr,i+1,n-arr.get(i));
int p2=fun(arr,i+1,n);
return min(p1,p2);
}else{
return fun(arr,i+1,n);
}
}
public static class pair
{
int a;
int b;
public pair(int aa,int bb)
{
a=aa;
b=bb;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(pair p1,pair p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
// sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static void DFS(int s,boolean visited[])
{
visited[s]=true;
for(int i:graph[s]){
if(!visited[i]){
DFS(i,visited);
}
}
}
public static void setGraph(int n,int m)
{
graph=new ArrayList[n+1];
for(int i=0;i<=n;i++){
graph[i]=new ArrayList<>();
}
for(int i=0;i<m;i++){
int u=I(),v=I();
graph[u].add(v);
graph[v].add(u);
}
}
//LOWER_BOUND and UPPER_BOUND functions
//It returns answer according to zero based indexing.
public static int lower_bound(long arr[],long X,int start, int end) //start=0,end=n-1
{
if(start>end)return -1;
if(arr[arr.length-1]<X)return end;
if(arr[0]>X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
// if(arr[mid]==X){ //Returns last index of lower bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
if(arr[mid]==X){ //Returns first index of lower bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid-1;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid;
}else{
left=mid+1;
}
}
}
return left;
}
//It returns answer according to zero based indexing.
public static int upper_bound(long arr[],long X,int start,int end) //start=0,end=n-1
{
if(arr[0]>=X)return start;
if(arr[arr.length-1]<X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr[mid]==X){ //returns first index of upper bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
// if(arr[mid]==X){ //returns last index of upper bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid+1;
}else{
left=mid+1;
}
}
}
return left;
}
//END
//Segment Tree Code
public static void buildTree(int si,int ss,int se)
{
if(ss==se){
seg[si]=ss;
return;
}
int mid=(ss+se)/2;
buildTree(2*si+1,ss,mid);
buildTree(2*si+2,mid+1,se);
seg[si]=max(seg[2*si+1],seg[2*si+2]);
}
public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b)
{
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a.get(i)<=b.get(j)){
f.add(a.get(i));
i++;
}else{
f.add(b.get(j));
j++;
}
}
while(i<a.size()){
f.add(a.get(i));
i++;
}
while(j<b.size()){
f.add(b.get(j));
j++;
}
}
public static void update(int si,int ss,int se,int pos,int val)
{
if(ss==se){
seg[si]=(int)add(seg[si],val);
return;
}
int mid=(ss+se)/2;
if(pos<=mid){
update(2*si+1,ss,mid,pos,val);
}else{
update(2*si+2,mid+1,se,pos,val);
}
seg[si]=(int)add(seg[2*si+1],seg[2*si+2]);
}
public static int query(int si,int ss,int se,int qs,int qe)
{
if(qs>se || qe<ss)return -1;
if(ss>=qs && se<=qe)return seg[si];
int mid=(ss+se)/2;
return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe));
}
//Segment Tree Code end
//Prefix Function of KMP Algorithm
public static int[] KMP(char c[],int n)
{
int pi[]=new int[n];
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0 && c[i]!=c[j]){
j=pi[j-1];
}
if(c[i]==c[j])j++;
pi[i]=j;
}
return pi;
}
public static long kadane(long a[],int n) //largest sum subarray
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static long nPr(int n,int r)
{
long ans=divide(fact(n),fact(n-r),mod);
return ans;
}
public static long nCr(int n,int r)
{
long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod);
return ans;
}
public static boolean isSorted(int a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static boolean isSorted(long a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static int computeXOR(int n) //compute XOR of all numbers between 1 to n.
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static int np2(int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
public static int hp2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long hp2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
int farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p)
{
for(;x<n;x+=x&(-x))
{
farr[x]+=p;
}
}
public int get(int x)
{
int ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU
{
int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
public static HashMap<Integer,Integer> primeFact(int a)
{
// HashSet<Long> arr=new HashSet<>();
HashMap<Integer,Integer> hm=new HashMap<>();
int p=0;
while(a%2==0){
// arr.add(2L);
p++;
a=a/2;
}
hm.put(2,hm.getOrDefault(2,0)+p);
for(int i=3;i*i<=a;i+=2){
p=0;
while(a%i==0){
// arr.add(i);
p++;
a=a/i;
}
hm.put(i,hm.getOrDefault(i,0)+p);
}
if(a>2){
// arr.add(a);
hm.put(a,hm.getOrDefault(a,0)+1);
}
// return arr;
return hm;
}
public static boolean isInteger(double N)
{
int X = (int)N;
double temp2 = N - X;
if (temp2 > 0)
{
return false;
}
return true;
}
public static boolean isPalindrome(String s)
{
int n=s.length();
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
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;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean isPrime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]);
}
out.println();
}
public static void printArray(String a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(pair a[])
{
for(pair p:a){
out.println(p.a+"->"+p.b);
}
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<?> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapI(HashMap<?,?> hm){
for(Map.Entry<?,?> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMap(HashMap<Long,ArrayList<Integer>> hm){
for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){
out.print(e.getKey()+"->");
ArrayList<Integer> arr=e.getValue();
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}out.println();
}
}
//Modular Arithmetic
public static long add(long a,long b)
{
a+=b;
if(a>=mod)a-=mod;
return a;
}
public static long sub(long a,long b)
{
a-=b;
if(a<0)a+=mod;
return a;
}
public static long mul(long a,long b)
{
return ((a%mod)*(b%mod))%mod;
}
public static long divide(long a,long b,long m)
{
a=mul(a,modInverse(b,m));
return a;
}
public static long modInverse(long a,long m)
{
int x=0,y=0;
own p=new own(x,y);
long g=gcdExt(a,m,p);
if(g!=1){
out.println("inverse does not exists");
return -1;
}else{
long res=((p.a%m)+m)%m;
return res;
}
}
public static long gcdExt(long a,long b,own p)
{
if(b==0){
p.a=1;
p.b=0;
return a;
}
int x1=0,y1=0;
own p1=new own(x1,y1);
long gcd=gcdExt(b,a%b,p1);
p.b=p1.a - (a/b) * p1.b;
p.a=p1.b;
return gcd;
}
public static long pwr(long m,long n)
{
long res=1;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m);
}
n=n>>1;
m=(m*m);
}
return res;
}
public static long modpwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static class own
{
long a;
long b;
public own(long val,long index)
{
a=val;
b=index;
}
}
//Modular Airthmetic
public static void sort(int[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(char[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
char tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
//max & min
public static int max(int a,int b){return Math.max(a,b);}
public static int min(int a,int b){return Math.min(a,b);}
public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));}
public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));}
public static long max(long a,long b){return Math.max(a,b);}
public static long min(long a,long b){return Math.min(a,b);}
public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));}
public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));}
public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
//end
public static int[] I(int n){int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;}
public static long[] IL(int n){long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;}
public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
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 I(){ return Integer.parseInt(next());}
long L(){ return Long.parseLong(next());}
double D(){return Double.parseDouble(next());}
String S(){
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 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 | 42ef34068874a1ca87b9d62d6fa56a46 | 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 codeforces.round774div2;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
//initReaderPrinter(true);
initReaderPrinter(false);
solve(in.nextInt());
//solve(1);
}
/*
General tips
1. It is ok to fail, but it is not ok to fail for the same mistakes over and over!
2. Train smarter, not harder!
3. If you find an answer and want to return immediately, don't forget to flush before return!
*/
/*
Read before practice
1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level;
2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else;
3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked;
4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list
and re-try in the future.
5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly;
6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial.
If getting stuck, skip it and solve other similar level problems.
Wait for 1 week then try to solve again. Only read editorial after you solved a problem.
7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already.
8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you
rushed to coding!
*/
/*
Read before contests and lockout 1 v 1
Mistakes you've made in the past contests:
1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked;
2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step
by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that
you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a
thorough idea steps!
3. Forgot about possible integer overflow;
When stuck:
1. Understand problem statements? Walked through test examples?
2. Take a step back and think about other approaches?
3. Check rank board to see if you can skip to work on a possibly easier problem?
4. If none of the above works, take a guess?
*/
static List<Long> factor;
static long[] p;
static void preprocess() {
p = new long[42];
p[0] = 1;
for(int i = 1; i < 42; i++) {
p[i] = p[i - 1] * 2;
}
factor = new ArrayList<>();
factor.add(1L);
long f = 1;
for(int i = 2; i <= 19; i++) {
f = f * i;
if(f <= (long)1e12) {
factor.add(f);
}
else break;
}
}
static void solve(int testCnt) {
preprocess();
for (int testNumber = 0; testNumber < testCnt; testNumber++) {
long n = in.nextLong();
int k = factor.size();
int ans = -1;
for(int s = 0; s < (1 << k); s++) {
long n1 = n;
List<Long> usedFactor = new ArrayList<>();
for(int i = 0; i < k; i++) {
if(((s >>> i) & 1) != 0) {
usedFactor.add(factor.get(i));
n1 -= factor.get(i);
}
}
int x = compute(n1, usedFactor);
if(x >= 0) {
if(ans < 0) {
ans = usedFactor.size() + x;
}
else {
ans = min(ans, usedFactor.size() + x);
}
}
}
out.println(ans);
}
out.close();
}
//find min cnt of unique 2^k that sum to n
static int compute(long n, List<Long> used) {
if(n == 0) return 0;
int cnt = 0;
for(int i = 41; i >= 0; i--) {
if(p[i] <= n && !used.contains(p[i])) {
n -= p[i];
cnt++;
}
}
return n == 0 ? cnt : -1;
}
static void initReaderPrinter(boolean test) {
if (test) {
try {
in = new InputReader(new FileInputStream("src/input.in"));
out = new PrintWriter(new FileOutputStream("src/output.out"));
} catch (IOException e) {
e.printStackTrace();
}
} else {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !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());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
Integer[] nextIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitive(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitiveOneIndexed(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
return a;
}
Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitive(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitiveOneIndexed(int n) {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextLong();
return a;
}
String[] nextStringArray(int n) {
String[] g = new String[n];
for (int i = 0; i < n; i++) g[i] = next();
return g;
}
List<Integer>[] readGraphOneIndexed(int n, int m) {
List<Integer>[] adj = new List[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt();
int v = nextInt();
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
List<Integer>[] readGraphZeroIndexed(int n, int m) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
/*
A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) {
int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
edgeCntForEachNode[v]++;
end1[i] = u;
end2[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[end1[i]][idxForEachNode[end1[i]]] = end2[i];
idxForEachNode[end1[i]]++;
adj[end2[i]][idxForEachNode[end2[i]]] = end1[i];
idxForEachNode[end2[i]]++;
}
return adj;
}
/*
A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) {
int[] from = new int[edgeCnt], to = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
//from u to v: u -> v
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
from[i] = u;
to[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[from[i]][idxForEachNode[from[i]]] = to[i];
idxForEachNode[from[i]]++;
}
return adj;
}
}
} | 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 | 76c71d12726c356b981c99af73075a7b | 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 codeforces.round774div2;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
//initReaderPrinter(true);
initReaderPrinter(false);
solve(in.nextInt());
//solve(1);
}
/*
General tips
1. It is ok to fail, but it is not ok to fail for the same mistakes over and over!
2. Train smarter, not harder!
3. If you find an answer and want to return immediately, don't forget to flush before return!
*/
/*
Read before practice
1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level;
2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else;
3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked;
4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list
and re-try in the future.
5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly;
6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial.
If getting stuck, skip it and solve other similar level problems.
Wait for 1 week then try to solve again. Only read editorial after you solved a problem.
7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already.
8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you
rushed to coding!
*/
/*
Read before contests and lockout 1 v 1
Mistakes you've made in the past contests:
1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked;
2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step
by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that
you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a
thorough idea steps!
3. Forgot about possible integer overflow;
When stuck:
1. Understand problem statements? Walked through test examples?
2. Take a step back and think about other approaches?
3. Check rank board to see if you can skip to work on a possibly easier problem?
4. If none of the above works, take a guess?
*/
static List<Long> factor;
static long[] p;
static void preprocess() {
p = new long[50];
p[0] = 1;
for(int i = 1; i < 50; i++) {
p[i] = p[i - 1] * 2;
}
factor = new ArrayList<>();
factor.add(1L);
long f = 1;
for(int i = 2; i <= 19; i++) {
f = f * i;
if(f <= (long)1e12) {
factor.add(f);
}
else break;
}
}
static void solve(int testCnt) {
preprocess();
for (int testNumber = 0; testNumber < testCnt; testNumber++) {
long n = in.nextLong();
int k = factor.size();
int ans = -1;
for(int s = 0; s < (1 << k); s++) {
long n1 = n;
List<Long> usedFactor = new ArrayList<>();
for(int i = 0; i < k; i++) {
if(((s >>> i) & 1) != 0) {
usedFactor.add(factor.get(i));
n1 -= factor.get(i);
}
}
int x = compute(n1, usedFactor);
if(x >= 0) {
if(ans < 0) {
ans = usedFactor.size() + x;
}
else {
ans = min(ans, usedFactor.size() + x);
}
}
}
out.println(ans);
}
out.close();
}
//find min cnt of unique 2^k that sum to n
static int compute(long n, List<Long> used) {
if(n == 0) return 0;
int cnt = 0;
for(int i = 49; i >= 0; i--) {
if(p[i] <= n && !used.contains(p[i])) {
n -= p[i];
cnt++;
}
}
return n == 0 ? cnt : -1;
}
static void initReaderPrinter(boolean test) {
if (test) {
try {
in = new InputReader(new FileInputStream("src/input.in"));
out = new PrintWriter(new FileOutputStream("src/output.out"));
} catch (IOException e) {
e.printStackTrace();
}
} else {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !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());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
Integer[] nextIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitive(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitiveOneIndexed(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
return a;
}
Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitive(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitiveOneIndexed(int n) {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextLong();
return a;
}
String[] nextStringArray(int n) {
String[] g = new String[n];
for (int i = 0; i < n; i++) g[i] = next();
return g;
}
List<Integer>[] readGraphOneIndexed(int n, int m) {
List<Integer>[] adj = new List[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt();
int v = nextInt();
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
List<Integer>[] readGraphZeroIndexed(int n, int m) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
/*
A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) {
int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
edgeCntForEachNode[v]++;
end1[i] = u;
end2[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[end1[i]][idxForEachNode[end1[i]]] = end2[i];
idxForEachNode[end1[i]]++;
adj[end2[i]][idxForEachNode[end2[i]]] = end1[i];
idxForEachNode[end2[i]]++;
}
return adj;
}
/*
A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) {
int[] from = new int[edgeCnt], to = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
//from u to v: u -> v
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
from[i] = u;
to[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[from[i]][idxForEachNode[from[i]]] = to[i];
idxForEachNode[from[i]]++;
}
return adj;
}
}
} | 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 | c06efc4aa8321fa917e13a4728a301f9 | 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 | /*
* Everything is Hard
* Before Easy
* Jai Mata Dii
*/
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 mod = (long)(1e9+7);
// static long mod = 998244353;
// static Scanner sc = new Scanner(System.in);
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args) {
int ttt = 1;
ttt = sc.nextInt();
z :for(int tc=1;tc<=ttt;tc++){
long n = sc.nextLong();
HashSet<Long> po2 = new HashSet<>();
ArrayList<Long> fac = new ArrayList<>();
long cur = 1, i = 2;
while(cur <= n) {
fac.add(cur);
cur = cur * i;
i++;
}
for(i=0;i<50;i++) {
if((n&(1L<<i)) != 0) {
po2.add(i);
}
}
// System.out.println(po2);
// System.out.println(fac);
long ans = solve(0, n, fac, false);
// System.out.println(po2.size()+" "+ext);
out.write((ans)+"\n");
}
out.close();
}
private static long solve(int i, long n, ArrayList<Long> fac, boolean is2) {
if(i == fac.size()) {
return find(n, is2);
}
long ans = Integer.MAX_VALUE;
if(fac.get(i)<=n) {
long cur = solve(i+1, n-fac.get(i), fac, fac.get(i) == 2);
if(cur != -1) ans = 1 + cur;
}
long cur = solve(i+1, n, fac, is2);
if(cur != -1) ans = Math.min(ans, cur);
if(ans == Integer.MAX_VALUE) ans = -1;
return ans;
}
private static long find(long n, boolean is2) {
int cur = 0;
for(long i=0;i<42;i++) {
if((n&(1L<<i)) != 0) {
if(1 << i == 2 && is2) return -1;
cur++;
}
}
return cur;
}
private static long add(HashSet<Long> po2, long n) {
int cur = 0;
for(long i=0;i<42;i++) {
if((n&(1L<<i)) != 0) {
po2.add(i);
}
}
return cur;
}
static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); }
private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);}
private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.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 | 8b62c5005b2d9f6885d68cb52a3758f3 | 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.util.Arrays.sort;
public class CodeforcesTemp {
static Reader scan = new Reader();
static FastPrinter out = new FastPrinter();
static long[] fact;
static int k;
public static void main(String[] args) throws IOException {
int tt = scan.nextInt();
// int tt = 1;
fact=new long[15];
init();
StringBuilder sb=new StringBuilder();
outer:
for (int tc = 1; tc <= tt; tc++) {
long n= scan.nextLong();
k=Integer.MAX_VALUE;
dfs(n,1,0);
sb.append(k);
sb.append("\n");
}
out.println(sb.toString());
out.flush();
out.close();
}
private static void dfs(long n, int i,int cnt) {
if(i==15){
int bits=Long.bitCount(n);
k=Math.min(k,bits+cnt);
return;
}
if((n-fact[i])>=0){
dfs(n-fact[i],i+1,cnt+1);
}
dfs(n,i+1,cnt);
}
private static void init() {
long prod=1;
for (long i = 1; i <=14; i++) {
prod*=i;fact[(int) i]=prod;
}
}
static class Reader {
private final 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 Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
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') {
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("not number"));
}
}
} 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("not number"));
}
}
}
}
throw new ArithmeticException(
String.format(" overflows long."));
}
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(long length) {
long[] array = new long[(int) 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 Integer[] nextIntegerArray(int length) {
Integer[] array = new Integer[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
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;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
this.printArray(arr[i]);
}
}
public void printCharMatrix(char[][] arr, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this.print(arr[i][j] + " ");
}
this.println();
}
}
}
static Random __r = new Random();
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] arr, int i, int j) {
while (i < j) {
char temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(int[] arr, int i, int j) {
while (i < j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(long[] arr, int i, int j) {
while (i < j) {
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
private static int getlowest(int l) {
if (l >= 1000000000) return 1000000000;
if (l >= 100000000) return 100000000;
if (l >= 10000000) return 10000000;
if (l >= 1000000) return 1000000;
if (l >= 100000) return 100000;
if (l >= 10000) return 10000;
if (l >= 1000) return 1000;
if (l >= 100) return 100;
if (l >= 10) return 10;
return 1;
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
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 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 | 31c2ca99afdc411902cecb1380e75a24 | 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.math.BigInteger;
import java.util.*;
public class Main {
static PrintWriter pw;
static long[] fac, invfac;
static long mod = (int) 1e9 + 7;
static Stack<Integer> st;
static boolean vis[];
// int MAXN = (int) 2e5 + 2; // if n =1e5 ,MAXN = 2*n+2
// facc(MAXN);
// invfacc(MAXN);
static long[] arr;
static int min;
public static void main(String[] args) throws Exception {
pw = new PrintWriter(System.out);
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long n = sc.nextLong();
arr = new long[15];
for (int i = 0; i <= 14; i++) {
arr[i] = fac(i);
}
min = Integer.MAX_VALUE;
solve(0, n, 0, 0);
pw.println(min);
}
pw.flush();
}
public static void solve(int idx, long target, long takenSum, int takenNum) {
if (takenSum > target)
return;
if (idx == arr.length) {
if (takenNum + Long.bitCount(target - takenSum) < min)
min = takenNum + Long.bitCount(target - takenSum);
return;
}
solve(idx + 1, target, takenSum + arr[idx], takenNum + 1);
solve(idx + 1, target, takenSum, takenNum);
}
public static long sqrt(long n) {
long low = 0;
long high = n + 1;
while (high - low > 1) {
long mid = (low + high) / 2;
if (mid * mid <= n)
low = mid;
else
high = mid;
}
return low;
}
public static int log2(Long N) // log base 2
{
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
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;
}
static void facc(int n) {
fac = new long[n];
fac[0] = 1;
for (int i = 1; i < n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
}
}
static void invfacc(int n) {
invfac = new long[n];
for (int i = 0; i < n; i++) {
invfac[i] = modinverse(fac[i], mod);
}
}
public static long modinverse(long a, long mod) {
return Pow(a, mod - 2, mod);
}
static long Pow(long a, long e, long mod) // O(log e)
{
a %= mod;
long res = 1l;
while (e > 0) {
if ((e & 1) == 1)
res = (res * a) % mod;
a = (a * a) % mod;
e >>= 1l;
}
return res;
}
static long nCr(int n, int r) {
return fac[n] * invfac[r] % mod * invfac[n - r] % mod;
}
static class Pair implements Comparable<Pair> {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public int compareTo(Pair p) {
if (this.x == p.x)
return this.y - p.y;
return this.x - p.x;
}
}
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 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 | 93802b6e21032c43735e7b82e3d181ee | 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 int dp[][];
static int mod = (int) 1e9 + 7;
static int n;
static HashSet<Long> hSet;
static long arr[];
static long target;
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
arr= new long [12];
n=12;
for (int i = 3; i <= 14; i++) {
arr[i-3]=fac(i);
}
int t=sc.nextInt();
while(t-->0) {
target= sc.nextLong();
pw.println(solve(0, 0, 0));
}
pw.close();
}
public static long solve(int idx,int count,long acc) {
if(acc>target)
return (long) 1e18;
if(idx ==n)
return count +Long.bitCount(target-acc);
long take = solve(idx+1, count+1, acc+arr[idx]);
long leave = solve(idx+1, count, acc);
return Math.min(take, leave);
}
public static int dfsOnAdjacencyList(ArrayList<Integer>[] graph, int curr, boolean[] vis, int m, int conscats,
int[] cats) { // same DFS code but a7la mn ellyfoo2 fel writing
if (conscats > m)
return 0;
if (graph[curr].size() == 1 && curr != 1) {
if (cats[curr] == 1)
conscats++;
else
conscats = 0;
if (conscats > m)
return 0;
return 1;
}
vis[curr] = true;
if (cats[curr] == 1)
conscats++;
else
conscats = 0;
int res = 0;
for (int x : graph[curr]) {
if (!vis[x])
res += dfsOnAdjacencyList(graph, x, vis, m, conscats, cats);
}
return res;
}
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);
}
static class Pair implements Comparable<Pair> {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public int compareTo(Pair p) {
if (this.x == p.x)
return this.y - p.y;
return this.x - p.x;
}
}
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 | ac5ed986f06edab060b5b22739b6f4ec | 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 A { //
public static void main(String[] args) throws IOException {
long[] fac = new long[16];
fac[0] = 1;
for (int i = 1; i < fac.length; i++) fac[i] = fac[i - 1] * i;
long[] sub = new long[1 << (fac.length - 1)];
int[] cnt = new int[sub.length];
for (int i = 1; i < sub.length; i++) {
int mask = i;
for (int b = 1; mask > 0; b++) {
if ((mask & 1) == 1) {
sub[i] += fac[b];
cnt[i]++;
}
mask >>= 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 = Integer.MAX_VALUE;
for (int i = 0; i < sub.length && sub[i] <= n; i++) {
int cur = cnt[i] + popCnt(n - sub[i]);
min = Math.min(min, cur);
}
bw.write(min + "\n");
}
br.close();
bw.close();
}
public static int popCnt(long i) {
int ans = 0;
while (i > 0) {
if ((i & 1) == 1) ans++;
i >>= 1;
}
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(int[] a, int n) {
for (int i = 0; i < n; i++) {
System.out.print(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 | b39dcdaeec6763df8ebf6659c3358d44 | 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.security.KeyStore.Entry;
import java.util.*;
public class Odd_Selection {
static InputStreamReader r = new InputStreamReader(System.in);
static BufferedReader br = new BufferedReader(r);
static PrintWriter p = new PrintWriter(System.out);
static int n;
static char a[];
public static void main(String[] args) throws Exception {
int test = Integer.parseInt(br.readLine());
// int test = 1;
long limit = 1000000000001l;
HashMap<Long, Integer> map = new HashMap<>();
ArrayList<Long> li = new ArrayList<>();
for (long i = 1, j = 1; j < limit; i = i + 1, j = j * i)
li.add(j);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
long val = li.get(i);
ArrayList<Long> temp = new ArrayList<Long>(map.keySet());
for (long x : temp) {
long next_val = x + val;
int step = map.get(x) + 1;
int exist_step = map.getOrDefault(next_val, Integer.MAX_VALUE);
exist_step = Math.min(exist_step, step);
map.put(next_val, exist_step);
}
map.put(val, 1);
}
map.put(0l,0);
// System.out.println(map.size());
me146: while (test-- > 0) {
long ans = Long.MAX_VALUE;
long question = Long.parseLong(br.readLine());
for(Map.Entry<Long,Integer> entry:map.entrySet()){
long val =entry.getKey();
int fre = entry.getValue();
if(question-val>=0){
ans = Math.min(ans,fre+Long.bitCount(question-val));
}
}
p.println(ans==Long.MAX_VALUE?-1:ans);
}
p.flush();
}
static int root(int union[],int i){
while(union[i]!=i){
union[i] = union[union[i]];
i=union[i];
}
return i;
}
static int[] join(int union[],int a,int b){
int rootA=root(union, a);
int rootB=root(union,b);
union[rootA] = union[rootB];
return union;
}
static boolean find(int union[],int a,int b){
if(root(union, a)==root(union,b))
return true;
else
return false;
}
static String formatString(String s) {
int len = s.length();
for (int i = 0; i < 19 - len; i++) {
s = "0" + s;
}
return s;
}
static String[] splitString() throws IOException {
return br.readLine().trim().split(" ");
}
}
class Obj implements Comparable<Obj>{
char type;
int count;
public Obj(char type, int count) {
this.type = type;
this.count = count;
}
@Override
public int compareTo(Obj o) {
// TODO Auto-generated method stub
return this.count-o.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 | fc9db32a03230c08c0585082fb81dfbb | 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
{
public static int howmany(long value)
{
String s = Long.toBinaryString(value);
// System.out.println(s);
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
count++;
}
}
return count;
}
public static void main(String args[]) throws java.lang.Exception
{
FastScanner input = new FastScanner();
ArrayList<Long> fact= new ArrayList<>();
long ad = 2;
for (long i = 3; i <=14; i++) {
fact.add(ad*i);
ad*=i;
}
// System.out.println(fact);
// System.out.println(Arrays.toString(fact));
int tc = input.nextInt();
work:
while (tc-- > 0) {
long n = input.nextLong();
int ans = Integer.MAX_VALUE;
//
// System.out.println(ans);
for (int i = 0; i <=((1<<12)-1); i++) {
String now = (Integer.toBinaryString(i));
while(now.length()<12)
{
now = "0"+now;
}
// System.out.println(now);
long nowsum=0;
int count=0;
for (int j = 0; j <12; j++) {
if(now.charAt(j)=='1')
{
count++;
nowsum+=fact.get(j);
}
}
if(nowsum<=n)
{
ans = Math.min(ans,count+howmany(n-nowsum) );
}
}
System.out.println(ans);
}
}
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());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.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 | 925816d6db9a967a63e3f8664a30c793 | 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 | 329314fd324d7e23ee58884ef7d2b8a3 | 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 int MAXN = (int) 1e5+5;
static long[] pNum = new long[MAXN];
static int p = 0;
static int ans = 100;
public static int get2K(long n){
int ret = 0;
while (n != 0) {
if ((n & 1) == 1) ret++;
n >>= 1;
}
return ret;
}
public static void genSum(int i, long sum, long n, int k) {
if (sum > n) return;
if (i == p) {
ans = Math.min(ans, get2K( n - sum) + k);
return;
}
genSum(i + 1, sum + pNum[i], n, k + 1); // 选
genSum(i + 1, sum, n, k); // 不选
}
public static void main(String[] args) {
long res = 1;
for (int i = 1; i < 16; i++) {
res *= i;
pNum[p++] = res;
}
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t != 0) {
t--;
long n = input.nextLong();
genSum(0, 0, n, 0);
System.out.println(ans);
ans = 100;
}
}
}
| 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 | 0a4c446ab6d6f8cce3e69e791fc64db3 | 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 brute_force;
import java.io.*;
import java.util.*;
public class factorials_and_powers_of_two {
static long n;
static long fact[];
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
fact = new long[16];
fact[0] = 1;
for (long i = 1; i < fact.length; i++) {
fact[(int) i] = i * fact[(int) i - 1];
}
while (t-- > 0) {
n = sc.nextLong();
pw.println(solve(0, 0, 0));
}
pw.flush();
}
public static int solve(int curr, int count, long acc) {
if (curr == fact.length) {
if (acc <= n) {
return count + count_ones(n - acc);
}
return 1000;
}
int take = solve(curr + 1, count + 1, acc + fact[curr]);
int leave = solve(curr + 1, count, acc);
return Math.min(take, leave);
}
public static int count_ones(long x) {
long mask = 1;
int count = 0;
while (mask <= x) {
if ((mask & x) != 0)
count++;
mask <<= 1;
}
return count;
}
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 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 | e3315ec1b0e690bb76afcc8ba5f91ee6 | 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 factorials {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
long[] facts = new long[15];
facts[3] = 6;
for(int i=4; i<15; i++) facts[i] = facts[i-1]*i;
for(int i=0; i<t; i++) {
long n = Long.parseLong(br.readLine());
int ans = 1000;
for(int j=0; j<(1<<15); j+=8) {
long sub = 0;
for(int k=0; k<15; k++)
if((j&(1<<k))==(1<<k))sub+=facts[k];
if(n-sub>=0)
ans = Math.min(ans, Integer.bitCount(j)+Long.bitCount(n-sub));
}
out.write(ans+"\n");
}
out.flush();
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 | 1cd64bea3640021278c5726ddab28ca9 | 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 {
static FastScanner sc = new FastScanner(System.in);
static FastPrintStream out = new FastPrintStream(System.out);
static final ArrayList<Long> facts = new ArrayList<>();
static final TreeSet<Long> pows = new TreeSet<>();
static final long MAX = 1000000000000L;
static {
long num = 1;
while (num <= MAX) {
pows.add(num);
num *= 2;
}
num = 1;
int counter = 1;
while (num <= MAX) {
facts.add(num);
num *= (++counter);
}
}
static int brute(long n, int pos) {
if (pos == facts.size()) {
return countPowsRepr(n);
}
int ans = brute(n, pos + 1);
if (n >= facts.get(pos)) {
ans = Math.min(ans, 1 + brute(n - facts.get(pos), pos + 1));
}
return ans;
}
private static int countPowsRepr(long n) {
int ans = 0;
while (n > 0) {
ans++;
n -= pows.floor(n);
}
return ans;
}
public static void main(String[] args) {
int t = sc.nextInt();
for (int tn = 0; tn < t; tn++) {
long n = sc.nextLong();
out.println(solve(n));
}
out.flush();
}
private static int solve(long n) {
return brute(n, 0);
}
static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
return a;
}
static int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
return a;
}
static int[] sorted(int[] array) {
ArrayList<Integer> list = new ArrayList<>(array.length);
for (int val : array) {
list.add(val);
}
list.sort(Comparator.naturalOrder());
return list.stream().mapToInt(val -> val).toArray();
}
static int[] sortedReverse(int[] array) {
ArrayList<Integer> list = new ArrayList<>(array.length);
for (int val : array) {
list.add(val);
}
list.sort(Comparator.reverseOrder());
return list.stream().mapToInt(val -> val).toArray();
}
static long[] sort(long[] array) {
ArrayList<Long> list = new ArrayList<>(array.length);
for (long val : array) {
list.add(val);
}
list.sort(Comparator.naturalOrder());
return list.stream().mapToLong(val -> val).toArray();
}
static long[] sortedReverse(long[] array) {
ArrayList<Long> list = new ArrayList<>(array.length);
for (long val : array) {
list.add(val);
}
list.sort(Comparator.reverseOrder());
return list.stream().mapToLong(val -> val).toArray();
}
private static class FastPrintStream {
private final BufferedWriter writer;
public FastPrintStream(PrintStream out) {
writer = new BufferedWriter(new OutputStreamWriter(out));
}
public void print(char c) {
try {
writer.write(Character.toString(c));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(int val) {
try {
writer.write(Integer.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(long val) {
try {
writer.write(Long.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(double val) {
try {
writer.write(Double.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(String val) {
try {
writer.write(val);
} catch (IOException e) {
e.printStackTrace();
}
}
public void println(char c) {
print(c + "\n");
}
public void println(int val) {
print(val + "\n");
}
public void println(long val) {
print(val + "\n");
}
public void println(double val) {
print(val + "\n");
}
public void println(String str) {
print(str + "\n");
}
public void println() {
print("\n");
}
public void flush() {
try {
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class FastScanner {
private final BufferedReader br;
private StringTokenizer st;
public FastScanner(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public 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());
}
public byte nextByte() {
return Byte.parseByte(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public 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 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 | 48f9bef7f24a6978d73317fa105bb669 | 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 FactorialsAndPowersOfTwo {
public static PrintWriter out;
public static void main(String[] args)throws IOException{
Scanner sc=new Scanner();
out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
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-=factorial(i+1);
}
}
if(m>=0){
ans=Math.min(ans,Long.bitCount(mask)+Long.bitCount(m));
}
}
out.println(ans);
}
out.close();
}
public static long factorial(int n) {
if(n==1) {
return 1;
}else if(n==0) {
return 1;
}
return n*factorial(n-1);
}
public static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
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 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 | dc07cd3fa37cc8344215feff1d0015be | 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 javax.management.Query;
import java.io.*;
public class Try {
// static int n,k;
//// static String t,s;
// static long[]memo;
static long[] arr;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
arr=new long[15];
arr[0]=1;
for (int i = 1; i <15 ; i++) {
arr[i]=i*arr[i-1];
}
int t=sc.nextInt();
while(t-->0) {
// for (int i = 0; i <arr.length ; i++) {
// pw.print(Long.bitCount(n));
// }
pw.println(func(3,sc.nextLong()));
}
pw.close();
}
public static long func(int idx1,long val){
if(idx1>=arr.length&&val>=0)return Long.bitCount(val);
if(val<0||idx1>=arr.length)
return Long.MAX_VALUE/2;
return Math.min(1+func(idx1+1,val-arr[idx1]),Math.min(func(idx1+1,val),Long.bitCount(val)));
}
// public static long dp(int idx) {
// if (idx >= n)
// return Long.MAX_VALUE/2;
// return Math.min(dp(idx+1),memo[idx]+dp(idx+k));
// }
// if(num==k)
// return dp(0,idx+1);
// if(memo[num][idx]!=-1)
// return memo[num][idx];
// long ret=0;
// if(num==0) {
// if(s.charAt(idx)=='a')
// ret= dp(1,idx+1);
// else if(s.charAt(idx)=='?') {
// ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) );
// }
// }
// else {
// if(num%2==0) {
// if(s.charAt(idx)=='a')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// else {
// if(s.charAt(idx)=='b')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// }
// }
public static int maxmin(int[]a,int[]b) {
int[] res=new int[a.length];
for (int i = 0; i < b.length; i++) {
res[i]=Math.max(a[i], b[i]);
}
int min=Integer.MAX_VALUE;
for (int i = 0; i < res.length; i++) {
min=Math.min(min, res[i]);
}
return min;
}
public static int[] swap(int[] a, int x,int y) {
int [] t=new int [a.length];
for (int i = 0; i < t.length; i++) {
if(a[i]==x)
t[i]=y;
else if(a[i]==y)
t[i]=x;
else
t[i]=a[i];
}
return t;
}
public static boolean palind(String s) {
for (int i = 0; i < s.length()/2; i++) {
if(s.charAt(i)!=s.charAt(s.length()-i-1))
return false;
}
return true;
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
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();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long 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 long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = 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 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 | d568e63423d3c14775963e1ac96cad1a | 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 2
static long [] fac;
static long n ;
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(new FileReader("input.in"));
// PrintWriter pw = new PrintWriter(new FileWriter(""));
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
// int t = 1;
long f=1;
fac= new long [14];
for(int i =0;i<14;i++){ //generate fac
fac[i]=f;
f*=(i+2);
}
int t = sc.nextInt();
while(t-->0){
n = sc.nextLong();
pw.println(gen(2,0));
}
pw.close();
}
static long gen(int i, long sum ){
if(i==14){
if(n-sum<0)return (long)1e5;
return count(n-sum);
}
long take = 1+gen(i+1, sum+fac[i]);
long leave = gen(i+1, sum);
return Math.min(take,leave);
}
static int count(long n){ // count the number of ones in the mask
int ans =0;
String binary = Long.toBinaryString(n);
for(int i =0;i<binary.length();i++){
ans+=binary.charAt(i)-'0';
}
return ans;
}
// -------------------------------------------------------Basics----------------------------------------------------
//-------------------------------------------------------- GRAPHS ----------------------------------------------------
static ArrayList<Integer> adj[];
static boolean vis[];
static void bfs (int node){
Queue<Integer> q= new LinkedList<>();
q.add(node);
vis[node]=true;
while(!q.isEmpty()){
int tmp= q.poll();
for(int i : adj[tmp]){
if(!vis[i]){
q.add(i);
vis[i]=true;
}
}
}
}
public static void dfs(int node ){
vis[node]=true;
for(int x: adj[node]){
if(!vis[x])
dfs(x);
}
}
//------------------------------------------------------ BINARYSEARCH ------------------------------------------------
// binary search // first occur // last occur
public static int binarySearch(long x, Long [] a){
int i =0;
int j = a.length-1;
int mid ;
while(i<=j){
mid = (i+j)/2;
if(a[mid]<=x){
i=mid+1;
}else{
j=mid-1;
}
}
return i;
}
// ------------------------------------------------------- MATH ----------------------------------------------------
private static int gcd(int a, int b) {
return (b == 0)? a : gcd(b, a % b);
}
private static long gcd(long a, long b) {
return (b == 0)? a : 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;
}
// ------------------------------------------------------ Objects --------------------------------------------------
static class Pair implements Comparable<Pair>{
long x ;
long y ;
Pair(long x , long y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair o) {
if(this.x==o.x)return 0;
if(this.x>o.x)return 1;
return -1;
}
@Override
public String toString() {
return x +" " + y ;
}
}
static class Tuple implements Comparable<Tuple>{
int x;
int y;
int z;
Tuple(int x, int y , int z){
this.x=x;
this.y=y;
this.z=z;
}
@Override
public int compareTo(Tuple o) {
if(this.x==o.x){
if(this.y==o.y)return this.z-o.z;
return this.y-o.y;
}
return this.x-o.x;
}
@Override
public String toString() {
return x +" " + y + " " + z ;
}
}
// -------------------------------------------------------Scanner---------------------------------------------------
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();
}
}
}
| 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 | 8b698aff6ebd12eeeb6e7100ade4b1a6 | 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 FactorialsAndPowersOfTwo {
static TreeSet<Long> tm = new TreeSet<>();
static ArrayList<Long> arr;
static long factorial(int n){
if(n == 0)
return 1;
return n * factorial(n-1);
}
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
arr = new ArrayList<>();
for(int i=1; i < 17 ;i++)
arr.add(factorial(i));
int t = sc.nextInt();
while(t-->0) {
long n = sc.nextLong();
int ans = 1000 , i;
for(i=0; i < 1<<arr.size() ;i++){
long tmp = n;
for(int j=0; j<arr.size() ;j++)
if((i & (1<<j)) != 0)
tmp -= arr.get(j);
if(tmp >= 0)
ans = Math.min(ans , Long.bitCount(i) + Long.bitCount(tmp));
}
pw.println(ans);
}
pw.flush();
}
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 | 72aebee8f77ada5631336ad87cdc299a | 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 factorials_and_powers_of_two {
static long n;
static long fact[];
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
fact = new long[16];
fact[0] = 1;
for (long i = 1; i < fact.length; i++) {
fact[(int) i] = i * fact[(int) i - 1];
}
while (t-- > 0) {
n = sc.nextLong();
pw.println(solve(0, 0, 0));
}
pw.flush();
}
public static int solve(int curr, int count, long acc) {
if (curr == fact.length) {
if (acc <= n) {
return count + count_ones(n - acc);
}
return 1000;
}
int take = solve(curr + 1, count + 1, acc + fact[curr]);
int leave = solve(curr + 1, count, acc);
return Math.min(take, leave);
}
public static int count_ones(long x) {
long mask = 1;
int count = 0;
while (mask <= x) {
if ((mask & x) != 0)
count++;
mask <<= 1;
}
return count;
}
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 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 | c12ba8034e82c2806e5ce1807618568a | 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 long fact[] = {1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800L,87178291200L,1307674368000L};
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-- > 0){
long val = scn.nextLong();
int min = recursion(val, 0);
System.out.println(min);
}
}
public static int recursion(long val, int idx){
if(fact[idx] > val){
return kernighans(val);
}
int temp1 = recursion(val, idx+1);
int temp2 = 1 + recursion(val - fact[idx], idx+1);
return Math.min(temp1, temp2);
}
public static int kernighans(long val){
int count = 0;
for(count = 0; val > 0; count++){
val = val - rsb(val);
}
return count;
}
public static long rsb(long val){
return val & (~val + 1);
}
} | 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 | e912bf9f461aecb2c4a79a554e1203f7 | 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.InputStream;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*/
public class Main {
static InputReader sc = new InputReader(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
private static void solve() {
long n=sc.nextLong();
Vector<Long> factors=new Vector<>();
long factor=6;
int num=4;
while(factor<=n){
factors.add(factor);
factor*=num;
num++;
}
Vector<Long[]> facGroup=new Vector();
Long l[]={0l,0l};
facGroup.add(l);
for(int i=1;i<(1<<factors.size());i++){
int FirstBit=getFirstBit(i);
Long pair[]=new Long[2];
//System.out.println(i+" "+(1<<(FirstBit-1)));
int pos=0;
int fb=FirstBit;
while(fb>0){
fb>>=1;
pos++;
}
//System.out.println(Integer.lowestOneBit(1)+" "+FirstBit);
pair[0]=facGroup.get(i-FirstBit)[0]+factors.get(pos-1);
pair[1]=(long)Integer.bitCount(i);
//System.out.println(i+" "+pair[0]+" "+pair[1]);
facGroup.add(pair);
}
int ans=Long.bitCount(n);
for(Long[] x:facGroup){
Long res=n-x[0];
if(n>=0){
long c=Long.bitCount(res)+x[1];
//System.out.println(x[0]+" "+x[1]);
ans=Math.min(ans,(int)c);
}
}
System.out.println(ans);
}
private static int getFirstBit(int i) {
return Integer.lowestOneBit(i);
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
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 = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
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 {
res *= 10;
res += c - '0';
c = read();
} 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 = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
} | 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 | bad5125eefff215bea56d0984ff6479f | 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.BigInteger;
public class code{
public static class Pair{
int a;
int b;
Pair(int i,int j){
a=i;
b=j;
}
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void shuffle(int a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
public static PrintWriter out = new PrintWriter(System.out);
//@SuppressWarnings("unchecked")
public static void main(String[] arg) throws IOException{
//Reader in=new Reader();
//PrintWriter out = new PrintWriter(System.out);
//Scanner in = new Scanner(System.in);
FastScanner in=new FastScanner();
int t=in.nextInt();
long[] fact=new long[12];
fact[0]=6;
for(int i=1;i<12;i++){
fact[i]=fact[i-1]*(i+3);
}
int size=1<<12;
//long k=1;
while(t-- > 0){
long n=in.nextLong();
int res=40;
for(int i=0;i<size;i++){
long sum=0;
int count=0;
for(int j=0;j<12;j++){
if(((1<<j)&i) >0){
sum+=fact[j];
count++;
}
}
if(sum<=n){
long m=n-sum;
while(m>0){
if((m & 1) >0){
count++;
}
m>>=1;
}
res=Math.min(res,count);
}
}
out.println(res);
}
out.flush();
}
}
class Fenwick{
int[] bit;
public Fenwick(int n){
bit=new int[n];
//int sum=0;
}
public void update(int index,int val){
index++;
for(;index<bit.length;index += index&(-index)) bit[index]+=val;
}
public int presum(int index){
int sum=0;
for(;index>0;index-=index&(-index)) sum+=bit[index];
return sum;
}
public int sum(int l,int r){
return presum(r+1)-presum(l);
}
}
class FastScanner {
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 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 | d8ce18ee81b414878eeedca189802e14 | 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.lang.*;
import java.util.*;
public class Main {
static long[] inv = new long[20];
static void init() {
// 全局变量初始化
inv[0] = inv[1] = 1;
for(int i = 2; i <= 15; i++) {
inv[i] = inv[i - 1] * i;
}
}
public static void main(String[] args) throws Exception {
init();
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while (t-- != 0) {
int ans = Integer.MAX_VALUE;
long n = fs.nextLong();
for (int i = 0; i < 1 << 15; i++) {
long v = n;
for (int j = 0; j < 15; j++) {
if (((i >> j) & 1) != 0) {
v -= inv[j];
}
}
if (v < 0) continue;
ans = Math.min(ans, Integer.bitCount(i) + Long.bitCount(v));
}
out.println(ans);
}
out.flush();
out.close();
}
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 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// Use this to input code since it is faster than a Scanner
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());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
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 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 | 1e7b2a2c690228525e929e758125ecb4 | 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 FactorialsAndPowersOfTwo {
public static PrintWriter out;
private static Scanner sc;
private static final long MOD = 1000000007;
public FactorialsAndPowersOfTwo(){
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new Scanner(System.in);
}
public static void main(String[] args) {
FactorialsAndPowersOfTwo ans = new FactorialsAndPowersOfTwo();
ans.solve();
out.close();
}
// hard work wins over skill
private void solve() {
int tests = sc.nextInt();
while(tests-->0) {
long N = sc.nextLong();
List<Long> factorials = new ArrayList<>();
long product = 1;
for(int i=2;product<=N;i++){
factorials.add(product);
product*=i;
}
long result = Long.MAX_VALUE;
for(int mask=0;mask<(1<<factorials.size());mask++){
long sum=N;
for(int i=0;i<factorials.size();i++){
if((mask&(1<<i))!=0) {
sum -= factorials.get(i);
}
}
if(sum<0) continue;
result = Math.min(result, Integer.bitCount(mask)+Long.bitCount(sum));
}
out.printf("%d\n",result);
}
}
private long log(long val, long base) {
return (long) (Math.log(val) / Math.log(base));
}
// dont forget to consider <0 case
private long add(long a) {
long sum = a;
if(sum<0) sum+=MOD;
else if(sum>=MOD) sum -= MOD;
return 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 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 | 297645dd0cb17fa6a22bb4c3569e5a09 | 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 FactorialsAndPowersOfTwo {
public static PrintWriter out;
private static Scanner sc;
private static final long MOD = 1000000007;
public FactorialsAndPowersOfTwo(){
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new Scanner(System.in);
}
public static void main(String[] args) {
FactorialsAndPowersOfTwo ans = new FactorialsAndPowersOfTwo();
ans.solve();
out.close();
}
// hard work wins over skill
private void solve() {
int tests = sc.nextInt();
while(tests-->0) {
long N = sc.nextLong();
List<Long> factorials = new ArrayList<>();
long product = 6;
for(int i=4;product<=N;i++){
factorials.add(product);
product*=i;
}
long result = Long.MAX_VALUE;
for(int mask=0;mask<(1<<factorials.size());mask++){
long sum=N;
for(int i=0;i<factorials.size();i++){
if((mask&(1<<i))!=0) {
sum -= factorials.get(i);
}
}
if(sum<0) continue;
result = Math.min(result, Integer.bitCount(mask)+Long.bitCount(sum));
}
out.printf("%d\n",result);
}
}
private long log(long val, long base) {
return (long) (Math.log(val) / Math.log(base));
}
// dont forget to consider <0 case
private long add(long a) {
long sum = a;
if(sum<0) sum+=MOD;
else if(sum>=MOD) sum -= MOD;
return 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 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 | 7eb025e12db708913fbf43fd242324be | 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.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static long mod = 1000000007 ;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=fs.nextInt();
outer:while(t-->0) {
long n=fs.nextLong();
long pow=1;
int i=0;
Set<Long> set=new HashSet<>();
long mul=1;
i=1;
while(mul*i<=n) {
mul*=i;
set.add(mul);
;
i++;
}
// System.out.println(mul+" "+i);
set.remove(1L);
set.remove(2L);
long arr[]=new long[set.size()];
i=0;
for(long ele:set) arr[i++]=ele;
sort(arr);
int ans= Long.bitCount(n);
ans= Math.min(recur(0,n,arr), ans);
out.println(ans);
}
out.close();
}
static int recur(int pos,long rem,long arr[]) {
if(pos==arr.length) return Long.bitCount(rem);
int ans= Long.bitCount(rem);
if(arr[pos]<=rem) {
ans= Math.min(ans, 1+recur(pos+1,rem-arr[pos],arr));
}
ans = Math.min(ans, recur(pos+1,rem,arr));
return ans;
}
static long pow(long a,long b) {
if(b<0) return 1;
long res=1;
while(b!=0) {
if((b&1)!=0) {
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b=b>>1;
}
return res;
}
static long gcd(long a,long b) {
if(b==0) return a;
return gcd(b,a%b);
}
static long nck(int n,int k) {
if(k>n) return 0;
long res=1;
res*=fact(n);
res%=mod;
res*=modInv(fact(k));
res%=mod;
res*=modInv(fact(n-k));
res%=mod;
return res;
}
static long fact(long n) {
// return fact[(int)n];
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static long modInv(long n) {
return pow(n,mod-2);
}
static void sort(int[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
static void sort(long[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
long temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
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());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
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 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 | 97c419686ef872a3246ebcfaa60c2957 | 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 F {
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
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 recur(long n, long hpow, long hfac, HashMap<Long, Long> dp) {
if (n == 0) return 0;
if (dp.get(n) != null) {
return dp.get(n);
}
long o = Long.MAX_VALUE / 2;
long mpow = (long)(Math.pow(2, (int)(Math.log(n) / Math.log(2))));
while (mpow >= hpow) {
mpow /= 2;
}
long mfac = 1;
for (int i = 2; i < 20; i++) {
if (mfac * i > n || mfac * i >= hfac) {
break;
}
mfac *= i;
}
// System.out.println(n + " " + mpow + " " + mfac);
o = Math.min(o, 1 + recur(n - mpow, mpow, hfac, dp));
o = Math.min(o, 1 + recur(n - mfac, hpow, mfac, dp));
dp.put(n, o);
return dp.get(n);
}
static long test(long n, ArrayList<Long> pows, ArrayList<Long> facs, int pi, int fi) {
if (n == 0) return 0;
else if (n == 1) return 1;
while (pi > -1 && pows.get(pi) > n) {
pi--;
}
while (fi > -1 && facs.get(fi) > n) {
fi--;
}
// System.out.println(n + " " + pi + " " + fi);
long o = Long.MAX_VALUE / 2;
if (pi < 0) {
o = Long.min(o, 1 + test(n - facs.get(fi), pows, facs, pi, fi - 1));
} else if (fi < 0) {
o = Long.min(o, 1 + test(n - pows.get(pi), pows, facs, pi - 1, fi));
} else if (pi > -1 && fi > -1) {
if (pows.get(pi) > facs.get(fi)) {
o = Long.min(o, 1 + test(n - pows.get(pi), pows, facs, pi - 1, fi));
} else {
o = Long.min(o, 1 + test(n - facs.get(fi), pows, facs, pi, fi - 1));
}
}
// dp
return o;
}
public static void main(String args[]) {
Reader r = new Reader();
int tcs = r.nextInt();
// HashMap<
for (int tc = 0; tc < tcs; tc++) {
long n = r.nextLong();
ArrayList<Long> pows = new ArrayList<Long>();
ArrayList<Long> facs = new ArrayList<Long>();
long p = 1;
long f = 1;
for (int i = 1; i <= 41; i++) {
pows.add(p);
p *= 2;
if (i < 20) {
facs.add(f);
f *= (i + 1);
}
}
// System.out.println(pows.get(pows.size() - 1) + " " + facs.get(facs.size() - 1));
// HashMap<Long, Long> dp = new HashMap<Long, Long>();
// long o = test(n, pows, facs, pows.size() - 1, facs.size() - 1);
int ppi = pows.size() - 1;
int ffi = facs.size() - 1;
int count = 0;
LinkedList<Long> list = new LinkedList<Long>();
list.add(n);
LinkedList<Integer> ind = new LinkedList<Integer>();
ind.add(pows.size() - 1);
ind.add(facs.size() - 1);
boolean found = false;
HashSet<Long> vis = new HashSet<Long>();
while (!list.isEmpty()) {
int s = list.size();
for (int i = 0; i < s; i++) {
long a = list.pop();
int pi = ind.pop();
int fi = ind.pop();
if (a == 0) {
found = true;
break;
}
if (!vis.add(a)) {
continue;
}
while (pi > -1 && pows.get(pi) > a) {
pi--;
}
while (fi > -1 && facs.get(fi) > a) {
fi--;
}
// System.out.println(a + " " + pi + " " + fi);
if (fi > -1) {
list.add(a - facs.get(fi));
ind.add(pi);
ind.add(fi - 1);
}
if (pi > -1) {
list.add(a - pows.get(pi));
ind.add(pi - 1);
ind.add(fi);
}
}
if (found) break;
count++;
}
// while (n > 0) {
// while (pi > -1 && pows.get(pi) > n) {
// pi--;
// }
// while (fi > -1 && facs.get(fi) > n) {
// fi--;
// }
// if (pi < 0 && fi < 0) break;
// if (pi < 0) {
// n -= facs.get(fi);
// fi--;
// } else if (fi < 0) {
// n -= pows.get(pi);
// pi--;
// } else if (pows.get(pi) > facs.get(fi)) {
// n -= pows.get(pi);
// pi--;
// } else {
// n -= facs.get(fi);
// fi--;
// }
// count++;
// }
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 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 | 6995d3be71c541b9fc0184f7bca9901b | 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 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
Solution solution = new Solution();
for (int i = 0; i < t; i++) {
System.out.println(solution.func(cin.nextLong()));
}
}
}
class Solution {
long[] fs;
long[] dp;
int m = 14;
public Solution() {
fs = new long[m];
fs[0] = 1;
for (int i = 1; i < m; i++) {
fs[i] = fs[i - 1] * (i + 1);
}
dp = new long[1 << m];
for (int i = 0; i < m; i++) {
dp[1 << i] = fs[i];
}
for (int i = 1; i < 1 << m; i++) {
int lb = i & -i;
dp[i] = dp[i - lb] + dp[lb];
}
}
public long func(long n) {
int k = 99;
for (int i = 0; i < 1 << m; i++) {
k = Math.min(k, Long.bitCount(n - dp[i]) + Integer.bitCount(i));
}
return k;
}
} | 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 | 87d012fba99bb3c4360a0a94e3d2463a | 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.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
long first, second,third;
public Tuple(long first, long second, long third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int count = 0;
int[] parent;
int[] rank;
public DSU(int n)
{
count = n;
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i)
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public void union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return;
if(rank[a] < rank[b])
{
parent[a] = b;
}
else if(rank[a] > rank[b])
{
parent[b] = a;
}
else
{
parent[b] = a;
rank[a] = 1 + rank[a];
}
count--;
}
public int countConnected()
{
return count;
}
}
static class Reader
{
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) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
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());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long powMod(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return powMod(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Long> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Long> l = new ArrayList<>();
for (long p = 2; p*p<=n; p++)
{
if (prime[(int)(p)] == true)
{
for(long i = p*p; i<=n; i += p)
{
prime[(int)(i)] = false;
}
}
}
for (long p = 2; p<=n; p++)
{
if (prime[(int)(p)] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
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;
}
public static int UpperBound(int a[], int x)
{
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 void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int x = 17;
long[] arr = new long[x];
arr[0] = 1;
for(int i = 1;i<=x-1;i++) arr[i] = i * arr[i-1];
int tt = sc.nextInt();
fr: while(tt-- > 0)
{
long n = sc.nextLong();
int len = 1 << (x-1);
long res = Long.MAX_VALUE;
for(int i = 0;i<=len;i++)
{
long curr = 0;
for(int j = 0;j<=x-1;j++)
{
if((i & (1 << j)) != 0)
{
curr += arr[j];
}
}
if(curr > n) continue;
long x1 = Long.bitCount(i), x2 = Long.bitCount((n - curr));
long tot = x1 + x2;
res = min(res,tot);
}
if(res == 0) res = 1;
fout.println((res == Long.MAX_VALUE) ? -1 : res);
}
fout.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 | fde179bd29ce0157432172d67953c639 | 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 C {
static List<Long> facts;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
facts = new ArrayList<>();
facts.add((long)1);
long prod = 1;
int i = 1;
while (prod <= (long)1e12){
prod *= i;
facts.add(prod);
i++;
}
int T= fs.nextInt();
for(int tt = 0; tt < T; ++tt){
long n = fs.nextLong();
System.out.println(min(n,0));
}
}
static int min(long x, int idx){
if(idx == facts.size() || facts.get(idx) > x){
return count(x);
}
int a1 = 1 + min(x - facts.get(idx), idx + 1);
int a2 = min(x, idx + 1);
return Math.min(a1, a2);
}
static int count(long x){
int cnt = 0;
for(int i = 63; i >= 0; --i) {
if (((x >> i) & 1) != 0) {
cnt++;
}
}
return cnt;
}
static final Random random = new Random();
static final int mod = 1_000_000_007;
static long add(long a, long b) {
return (a + b) % mod;
}
static long sub(long a, long b) {
return ((a - b) % mod + mod) % mod;
}
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 long mul(long a, long b) {
return (a * b) % mod;
}
static long exp(long base, long exp) {
if (exp == 0) return 1;
long half = exp(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials = new long[2_000_001];
static long[] invFactorials = new long[2_000_001];
static void precompFacts() {
factorials[0] = invFactorials[0] = 1;
for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i);
invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2);
for (int i = invFactorials.length - 2; i >= 0; i--)
invFactorials[i] = mul(invFactorials[i + 1], i + 1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k]));
}
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 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 | 7962e5d719a2933ba229234c76ccfa83 | 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;
public class Main {
static List<Long> fact = new ArrayList<>();
static {
long x = 1;
for (int i = 1 ; i < 20; i ++) {
x *= i;
if (x > 1_000_000_000_000L) {
break;
}
fact.add(x);
}
// System.out.println(fact);
}
static int best = 0;
private static int solve(long n) {
best = Long.bitCount(n);
solve(n , 0, new boolean[fact.size()]);
return best;
}
private static void solve(long n, int k, boolean[] used) {
if (k > best) {
return;
}
if (n == 0) {
best = k;
return;
}
solve(0, k + Long.bitCount(n), used);
for (int i = fact.size() - 1 ; i >= 0 ; i--) {
if (used[i]) {
continue;
}
long f = fact.get(i);
used[i] = true;
solve(n - f, k + 1, used);
used[i] = false;
}
}
private static int solveSmart(long n) {
int p = fact.size();
int best = Integer.MAX_VALUE;
for (int mask = 0 ; mask < 1 << p ; mask++) {
long factSum = getFactSum(fact, mask);
if (factSum > n) {
continue;
}
int k = Integer.bitCount(mask) + Long.bitCount(n - factSum);
best = Math.min(k, best);
}
return best;
}
private static long getFactSum(List<Long> fact, int mask) {
long sum = 0;
for (int i = 0 ; i < fact.size() ; i ++) {
if ((mask & (1<<i)) != 0) {
sum += fact.get(i);
}
}
return sum;
}
public static void main(String[] args) throws IOException {
// long start = System.currentTimeMillis();
// long max =1_000_000L;
// for (int i = 0 ; i < 100; i ++) {
// System.out.println(Long.bitCount(max - i));
// System.out.println(solveSmart(max - i));
// }
// System.out.println("Done: " + (System.currentTimeMillis() - start));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1024 * 1024 * 2);
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
long n = Long.parseLong(br.readLine());
sb.append(solveSmart(n)).append('\n');
}
System.out.print(sb.toString());
}
public static boolean isValid(int[] x) {
List<Integer> xxx = new ArrayList<>();
for (int a: x) {
xxx.add(a);
}
Collections.sort(xxx);
for (int i = 0 ; i < x.length ; i ++){
x[i] = xxx.get(i);
}
int bluePos = 0;
long blue = x[bluePos];
int blueCount = 1;
int redPos = x.length - 1;
long red = x[redPos];
int redCount = 1;
while (true) {
if (redPos <= bluePos) {
return false;
}
if (blue < red && redCount < blueCount) {
return true;
} else if (blue >= red) {
redPos--;
redCount++;
red += x[redPos];
} else if (redCount >= blueCount) {
bluePos ++;
blueCount ++;
blue += x[bluePos];
}
}
}
public static int[] readArrayLine(String line, int n) {
return readArrayLine(line, n, null, 0);
}
private static int[] readArrayLine(String line, int n, int array[], int pos) {
int[] ret = array == null ? new int[n] : array;
int start = 0;
int end = line.indexOf(' ', start);
for (int i = pos; i < pos + n; i++) {
if (end > 0)
ret[i] = Integer.parseInt(line.substring(start, end));
else
ret[i] = Integer.parseInt(line.substring(start));
start = end + 1;
end = line.indexOf(' ', start);
}
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 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 | 2cf2a8491fca3449d40d065ec9367d2d | 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.Collections;
import java.util.Random;
import java.util.StringTokenizer;
/*
Long.bitCount(n);
some factorials to look at
brute force which factorials are intersting
*/
public class C {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
long n=fs.nextLong();
ArrayList<Long> factorials=new ArrayList<>();
long prod=6;
for (int i=4; true; i++) {
if (prod>n) {
break;
}
factorials.add(prod);
prod*=i;
}
long ans=Long.MAX_VALUE;
for (int mask=0; mask<1<<factorials.size(); mask++) {
long sum=n;
for (int i=0; i<factorials.size(); i++) {
if ((mask&(1<<i))!=0) sum-=factorials.get(i);
}
if (sum<0) continue;
ans=Math.min(Integer.bitCount(mask)+Long.bitCount(sum), ans);
}
System.out.println(ans);
}
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
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 long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
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());
}
}
}
| 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 | 2db3fbbc2c57fdf8fbe1d1ce3ec7b85a | 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 kg.my_algorithms.Codeforces;
import java.util.*;
import java.io.*;
// If you know the reason for your existence, then act on it.
// "मैं दर्शक नहीं हूँ" -> I am not a spectator
public class Solution {
private static final FastReader fr = new FastReader();
private static final long mod = 1_000_000_007L;
private static final int INFINITY = 100;
public static void main(String[] args) throws IOException {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
TreeSet<Long> set = new TreeSet<>();
long t = 1L;
set.add(t);
while(t<=1_000_000_000_000L){
set.add(t);
t=t*2L;
}
t=1L;
long index = 2;
while(t<=1_000_000_000_000L){
set.add(t);
t=t*(index++);
}
long[] arr = new long[set.size()];
for(int i=0;i<arr.length;i++) arr[i] = set.pollFirst();
int n = arr.length;
long[] prefixSum = new long[n];
prefixSum[0] = arr[0];
for(int i=1;i<n;i++) prefixSum[i] = prefixSum[i-1]+arr[i];
int testCases = fr.nextInt();
for(int testCase=1;testCase<=testCases;testCase++){
long sum = fr.nextLong();
long res = fun(n-1,arr,sum, prefixSum);
if(res>55) sb.append("-1\n");
else sb.append(res).append("\n");
}
// System.out.println(fun(n-1,arr,7,prefixSum));
// System.out.println(fun(n-1,arr,11,prefixSum));
// System.out.println(fun(n-1,arr,240,prefixSum));
output.write(sb.toString());
output.flush();
}
private static int fun(int index, long[] arr, long rem_sum,long[] prefixSum){
// System.out.println("index= " + index + " rem_sum= " + rem_sum);
if(rem_sum == 0) return 0;
if(index == -1) return INFINITY;
if(rem_sum>prefixSum[index]) return INFINITY;
if(arr[index]>rem_sum) return fun(index-1,arr,rem_sum,prefixSum);
return Math.min(fun(index-1,arr,rem_sum-arr[index],prefixSum)+1,fun(index-1,arr,rem_sum,prefixSum));
}
}
//Fast Input
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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
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 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 | afaa568e6adb4cf8a163008609a46a60 | 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) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long n = sc.nextLong();
List<Long> list = new ArrayList<>();
for (long x = 1, y = 2; x <= n; x *= y, y++) {
list.add(x);
}
int res = dfs(list, 0, n);
System.out.println(res >= Integer.MAX_VALUE / 2 ? -1 : res);
}
}
private static int dfs(List<Long> list, int cur, long sum) {
if (cur == list.size()) return func(sum);
Long num = list.get(cur);
int res = dfs(list, cur + 1, sum);
return Math.min(res, 1 + dfs(list, cur + 1, sum - num));
}
private static int func(long sum) {
if (sum < 0) return Integer.MAX_VALUE / 2;
return Long.bitCount(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 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 |
Subsets and Splits