• Han Xin and His Troops


    Han Xin and His Troops

    中国剩余定理

    JAVA板子

    /*中国剩余定理,根据公式需要求取大数的逆元*/
    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class Main {
     
            static BigInteger m[]=new BigInteger[105];
            static BigInteger c[]=new BigInteger[105];
            static int  n;
            static BigInteger _m;
            static BigInteger x,y;
            public  static BigInteger[] ex_gcd(BigInteger a,BigInteger b){ 
                BigInteger ans; 
                BigInteger[] result=new BigInteger[3]; 
                if(b.equals(BigInteger.ZERO)) 
                { 
                    result[0]=a; 
                    result[1]=BigInteger.ONE; 
                    result[2]=BigInteger.ZERO; 
                    return result; 
                } 
                BigInteger [] temp=ex_gcd(b,a.mod(b)); 
                ans = temp[0]; 
                result[0]=ans; 
                result[1]=temp[2]; 
                result[2]=temp[1].subtract((a.divide(b)).multiply(temp[2]));
                //System.out.println(result[0]+" "+result[1]+" "+result[2]);
                return result; 
            }
            static BigInteger getInv(BigInteger a,BigInteger md)
            {
                //x=BigInteger.ZERO;y=BigInteger.ZERO;
                BigInteger d[]=ex_gcd(a,md);
                //System.out.println(a+" "+md);
                //System.out.println(x+" "+y);
                //System.out.println(d[1]);
                return (d[0].equals(BigInteger.ONE))?(d[1].add(md)).mod(md):BigInteger.valueOf(-1);
            }
            static BigInteger exCRT(int n)
            {
                BigInteger m1,m2,c1,c2,d;
                for(int i=2;i<=n;i++)
                {
                    m1=m[i-1];m2=m[i];c1=c[i-1];c2=c[i];
                    d=m1.gcd(m2);
                    if(!(c2.subtract(c1)).mod(d).equals(BigInteger.ZERO))return BigInteger.valueOf(-1);//此时无法合并
                    m[i] = m[i-1] .multiply(m[i]).divide(d) ;
                    BigInteger t=(c2.subtract(c1)).divide(d);
                    //System.out.println(t);
                    c[i] = t.multiply( getInv(m1.divide(d),m2.divide(d))) .mod ( m2.divide(d) ).multiply(m1) .add(c1) ;
                    //System.out.println(c[i]);
                   // System.out.println(getInv(m1.divide(d),m2.divide(d)));
                    c[i] = ( (c[i] .mod(m[i])) .add(m[i]) ) .mod( m[i]);
                   // System.out.println(m[i]);
                }
                return c[n];
            }
         public static void main(String[] args) {
             
                Scanner sc=new Scanner(System.in);
                n=sc.nextInt();
                _m=sc.nextBigInteger();
               // int k=0;
                for(int i=1;i<=n;i++){
                    m[i]=sc.nextBigInteger();
                    c[i]=sc.nextBigInteger();
     
                }
     
                BigInteger ans=exCRT(n);
                //System.out.println(ans);
                if(ans.compareTo(_m)>0){
                    System.out.println("he was probably lying");
                }else if(ans.equals(BigInteger.valueOf(-1))){
                    System.out.println("he was definitely lying");
                }else System.out.println(ans);
     
            }
     
    }
  • 相关阅读:
    查询SystemFeature的方法
    【HTML5游戏开发小技巧】RPG情景对话中,令文本逐字输出
    BFS寻路的AS3实现
    超级坑人的Couchbase数据库问题!!!
    java--函数练习
    CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方
    2017第27周六努力与积累
    2017第27周五
    丢掉生活中的90%,你会收获更多
    《时间简史》笔记摘录
  • 原文地址:https://www.cnblogs.com/liulex/p/11370275.html
Copyright © 2020-2023  润新知