Han Xin and His Troops
扩展中国剩余定理模板题,但是会爆long long。
java大数代码
import java.math.BigInteger;
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class Main {
static BigInteger a[]=new BigInteger[100005];
static BigInteger m[]=new BigInteger[100005];
static int n;
static BigInteger[] exgcd(BigInteger a,BigInteger b) {
BigInteger res[]=new BigInteger[3];
if (b.compareTo(BigInteger.ZERO)==0) {
res[0]=a;
res[1]=BigInteger.ONE;
res[2]=BigInteger.ZERO;
return res;
}
BigInteger[] tmp=exgcd(b,a.mod(b));
res[0]=tmp[0];
res[1]=tmp[2];
res[2]=tmp[1].subtract(a.divide(b).multiply(tmp[2]));
return res;
}
static BigInteger excrt() {
BigInteger M=BigInteger.valueOf(1),A=BigInteger.valueOf(0);
for (int i=1;i<=n;i++) {
BigInteger[] rrr=exgcd(M,m[i]);
BigInteger d=rrr[0];
BigInteger x1=rrr[1];
BigInteger y1=rrr[2];
BigInteger mm=m[i].divide(d);
if (((a[i].subtract(A)).mod(d)).compareTo(BigInteger.ZERO)!=0 ) {return BigInteger.valueOf(-1);}
x1=x1.mod(mm);
BigInteger k=(((((a[i].subtract(A)).divide(d)).multiply(x1)).mod(mm))).mod(mm);
BigInteger nM=M.multiply(mm);
A=A.add((k.multiply(M).mod(nM)));
M=nM;
}
return A;
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
BigInteger m1;
m1=cin.nextBigInteger();
for(int i=1;i<=n;i++) {
m[i]=cin.nextBigInteger();
a[i]=cin.nextBigInteger();
}
BigInteger ans=excrt();
if(ans.compareTo(m1)==1)
System.out.println("he was probably lying");
else if(ans.compareTo(BigInteger.valueOf(-1))==0)
System.out.println("he was definitely lying");
else if(ans.compareTo(m1)==-1)
System.out.println(ans);
}
}