我正在使用以下代码回答比赛问题:import java.io.DataInputStream;import java.io.FileInputStream;import java.io.IOException;import java.util.Scanner;import java.math.BigInteger;//ans is modulu 998244353public 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 方法很慢?我需要知道尝试修复它的原因
1 回答

慕仙森
TA贡献1827条经验 获得超8个赞
如果您不必使用 java.math.BigInteger 最好使用原始 long 代替,使用不可变对象有利于可扩展性,但由于您运行的线程不超过一个,因此不会从使用它中受益,但会得到GC 的缺点是,如果您使用对象比使用原语更多,则 GC 会更频繁地工作。
添加回答
举报
0/150
提交
取消