JAVA求扩展中国剩余定理

大数记得试一下int__128;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
  
 
public class Main {
    public static void main (String[] args){
        Scanner scan = new Scanner(System.in);
        BigInteger []a=new BigInteger[105];
        BigInteger []b=new BigInteger[105];
            int n;
            n=scan.nextInt();
            BigInteger m=scan.nextBigInteger();
            for(int i=0;i<n;i++) {
                a[i]=scan.nextBigInteger();
                b[i]=scan.nextBigInteger();
            }
            boolean flag=true;
            for(int i=1;i<n;i++) {
                BigInteger m1=a[i-1],m2=a[i];
                BigInteger c1=b[i-1],c2=b[i];
                BigInteger g = gcd(m1,m2);
                if((c2.subtract(c1).mod(g)).compareTo(BigInteger .ZERO)!=0) {
                    flag=false;break;
                }
                a[i]=m1.divide(g).multiply(m2);
                BigInteger inv=exgcd(m1.divide(g),m2.divide(g));
                if(inv.equals(BigInteger.valueOf(-1))) {
                    flag=false;
                    break;
                }
                b[i]=c2.subtract(c1).divide(g).mod(m2.divide(g)).multiply(m1).multiply(inv).add(c1);
                b[i]=(b[i].mod(a[i]).add(a[i])).mod(a[i]);
            }
            BigInteger ans=b[n-1];
            if(!flag) {
                System.out.println("he was definitely lying");
            }
            else if(ans.compareTo(m)==1) {
                System.out.println("he was probably lying");
            }
            else if(ans.compareTo(BigInteger.valueOf(0))==-1) {
                System.out.println("he was definitely lying");
            }
            else
                System.out.println(ans);
    }
    static BigInteger gcd(BigInteger a,BigInteger b) {
        if(b.equals(BigInteger.ZERO)){
            return a;
        }
        else {
            return gcd(b,a.mod(b));
        }
    }
    //java求Ex_gcd
    static BigInteger exgcd(BigInteger M, BigInteger m){
            ArrayList<BigInteger> List_Q = new ArrayList<BigInteger>();
            ArrayList<BigInteger> List_S = new ArrayList<BigInteger>();
            BigInteger temp, res,m_rec = m;
            //通过辗转相除获得List_Q
            while (!m.equals(BigInteger.ZERO)){
                temp = m;
                List_Q.add(M.divide(m));
                m = M.remainder(m);
                M = temp;
            }
                     
            //根据递推公式获得List_S
            List_S.add(BigInteger.ONE);
            List_S.add(BigInteger.ZERO);
            for (int i = 2 ; i < 1 + List_Q.size() ; i++){
                List_S.add(List_S.get(i - 2).subtract(List_Q.get(i - 2).multiply(List_S.get(i - 1))));
            }
            res = List_S.get(List_S.size() - 1);
            while (res.compareTo(BigInteger.ZERO) < 0){
                res = res.add(m_rec);
            }
            return res.mod(m_rec);
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值