我尝试在Java中使用long类型,效果很好。但是,我想处理大于长值的值,所以我搬到BigInteger,但最后遇到类型不兼容(BigInteger和int)的错误。预先感谢。
import java.io.*;
import java.util.*;
import java.math.BigInteger;
class Main
{
public static BigInteger aryaStarkToWin(int p, int r,BigInteger[][]req_Array) {
BigInteger a=new BigInteger("1000000007");
BigInteger b=new BigInteger("0");
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
BigInteger returnValue= req_Array[p][0];
returnValue=returnValue.mod(a);
return returnValue ;
}
public static BigInteger wins(int aryaCounter, int sansaCounter, BigInteger chances, final int p, final int r,BigInteger[][]req_Array) {
BigInteger a=new BigInteger("1000000007");
BigInteger b=new BigInteger("0");
BigInteger c=new BigInteger("1");
BigInteger d=new BigInteger("-1");
if(req_Array[aryaCounter][sansaCounter]!=d) {
return req_Array[aryaCounter][sansaCounter];
}
if (aryaCounter < sansaCounter * p) {
return b;
}
if (aryaCounter + sansaCounter == r) {
return c;
}
req_Array[aryaCounter][sansaCounter]=(wins(aryaCounter + 1, sansaCounter, chances, p, r,req_Array).mod(a).add(wins(aryaCounter, sansaCounter + 1, chances, p, r,req_Array).mod(a))).mod(a);
return req_Array[aryaCounter][sansaCounter];
}
public static void main(String[] args) {
int T_cases=0;
Scanner scany= new Scanner(System.in);
T_cases=scany.nextInt();
BigInteger d=new BigInteger("-1");
while(T_cases-->0) {
int no_of_rounds = scany.nextInt();
BigInteger[][] req_Array = new BigInteger[no_of_rounds+1][no_of_rounds+1];
int ptimes=scany.nextInt();
for(int i=0;i<=no_of_rounds;i++) {
for(int j=0;j<=no_of_rounds;j++) {
req_Array[i][j]=d;
}
}
System.out.println(aryaStarkToWin(ptimes,no_of_rounds,req_Array));
}
scany.close();
}
}
输入是
2
4 2
2 2
输出必须是
3
1
但是,我收到错误消息,例如
Main.java:9: error: incompatible types: int cannot be converted to BigInteger
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
1 error
req_Array[p][0]=wins(p, 0, 0, p, r,req_Array) ;
^
并且方法签名为:
public static BigInteger wins(int aryaCounter, int sansaCounter, BigInteger chances, final int p, final int r,BigInteger[][]req_Array) {
^^^^^^^^
您可以按如下方式传递的第三个参数:
req_Array[p][0]=wins(p, 0, BigInteger.ZERO, p, r,req_Array) ;
或将方法签名更改为:
public static BigInteger wins(int aryaCounter, int sansaCounter, int chances, final int p, final int r,BigInteger[][]req_Array) {
话虽如此,您没有在方法中利用机会,因此最好摆脱这种机会。