我正在使用以下代码回答比赛问题:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.math.BigInteger;
//ans is modulu 998244353
public class ShiftAndAdd {
private static long mod = 998244353;
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)throws IOException {
BigInteger ans=new BigInteger("0");
int n,m;
BigInteger numb_a,numb_b;
n= input.nextInt();
m=input.nextInt();
numb_a=input.nextBigInteger();
numb_b=input.nextBigInteger();
long[] a = new long[n];
long[] b = new long[m];
long[] a1 = new long[n];//will contain indices of cells of "a" containing 1's
long[] b1 = new long[m];
int ka1=0;//will be actual length of a1
int kb1=0;//will be actual length of b1
for(int i=0;i<n;i++) {
a[n-1-i]=numb_a.longValue()%10;
numb_a=numb_a.divide(new BigInteger("10"));
}
for(int i=0;i<m;i++) {
b[m-1-i]=numb_b.longValue()%10;
numb_b=numb_b.divide(new BigInteger("10"));
}
int a1start=(m>=n)?m-n:0;
ka1=a1start;
for(int i=0;i<n;i++)
if(a[i]==1)
a1[ka1++]=i;
int counter=0;
for(int i=0;i<m;i++)
if(b[i]==1)
b1[kb1++]+=++counter;
else b1[kb1++]=counter;
//answer:
for(int i=a1start;i<ka1;i++) {
ans=ans.add(BigInteger.valueOf((fastExp((long)2,(n-1-a1[i]))%mod *b1[(int)(a1[i]+a1start)] %mod)%mod));
}
print(ans.longValue());
}//end main
但是,在线编译器给了我超出某些输入的时间限制(它向变量 numb_a 和 numb_b 输入非常大的整数)我的问题是我不知道时间限制在哪里超出,是在我阅读整数是因为类 BigInteger 的方法很慢?还是因为这个类的 valueOf 和 add 方法很慢?我需要知道尝试修复它的原因
慕仙森
相关分类