我正在尝试将一个非常大的输入读取为字符串,然后将其转换long
为如下所示:[该程序适用于短输入]
输入是两个int
用空格隔开,例如:"1248614876148768372675689568324619856329856295619253291561358926935829358293587932857923857934572895729511 413241"
我的代码:
import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
public static void main(String args[] ) throws Exception {
Solution obj = new Solution();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
String input[]=new String[T];
for (int i=0;i<T;i++) {
input[i] = br.readLine();
}
for (int i=0;i<T;i++){
StringTokenizer st = new StringTokenizer(input[i]," ");
BigInteger N = new BigInteger(st.nextToken());
BigInteger P = new BigInteger(st.nextToken());
System.out.println(obj.result(N,P));
}
}
}
public BigInteger result(BigInteger N, BigInteger P){
BigInteger temp=1;
BigInteger c=0;
for (BigInteger i=0;i<=N;i++){
//System.out.println(nck(N,i));
if ((nck(N,i)%P) ==0)
c++;
}
return c;
}
public BigInteger nck(BigInteger N, BigInteger k){
if (k==0)
return 1;
else {
BigInteger temp=1;
BigInteger y=1;
BigInteger z=N;
while(k>=1){
temp=temp*z/y;
y++;
z--;
k--;
}
return temp;
}
}
}
我得到一个java.lang.NumberFormatException