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);

        }

}

原文地址:https://www.cnblogs.com/liulex/p/11370275.html

时间: 2024-11-26 11:40:20

Han Xin and His Troops的相关文章

2019nc#10

题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A Blackjack 点击查看 进入讨论 32/109 未通过 B Coffee Chicken 点击查看 进入讨论 738/2992  通过 C Gifted Composer 点击查看 进入讨论 12/64 未通过 D Han Xin and His Troops 点击查看 进入讨论 602/4072  通过 E Hilbert Sort 点击查看 进入讨论 508/1681  通过 F Popping Balloons 点击查看 进

二维码(QR code)基本结构及生成原理

什么是二维码 二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用的码制有:Data Matrix, Maxi Code, Aztec, QR Code, Vericode, PDF417, Ultracode, Code 49, Code 16K等. 二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码. 1.堆叠式/行排式二维条码,堆叠式/行排式二维条码又

变化太大-逗牙http://mvxz.idouya.xin/bencandy.php?fid=32&amp;id=14900

——约翰,好久不见了,你变化真大,头发少了,眼睛也不如以前明亮了. ——对不起,我叫汤姆. ——什么?你连名字也变了? 转载自逗牙-爱逗牙,逗掉你牙http://mvxz.idouya.xin/bencandy.php?fid=32&id=14900

逗牙搞笑网http://www.idouya.xin//html/32-14/14901.htm

和谁比赛 两个朋友一起来到森林,远远听到虎啸. 一个人急忙脱掉大皮靴.另一个也急了说:“脱了靴子你也跑不过老虎呀!” 脱靴者说:“我不和老虎赛跑,能跑过你就没事了.” 转载自:逗牙搞笑网http://www.idouya.xin//html/32-14/14901.htm

Codeforces Round #291 (Div. 2)——B&lt;set&gt;—— Han Solo and Lazer Gun

There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtro

ListView中使用自定义Adapter及时更xin

在项目中,遇到不能ListView及时更新的问题.写了一个demo,其中也遇到一些问题,一并写出来.好吧,上代码: public class PersonAdapter extends BaseAdapter { private ArrayList<PersonBean> mList; private Context mContext; public PersonAdapter(ArrayList<PersonBean> list, Context context) { mList

WHU1609 Han Move 思维

Description Cyy and Fzz are Han Move lovers. One day, they gather together to run. They choose a circular track whose length is L m. Cyy’s speed is A m/s and Fzz’s speed is B m/s. They may choose the direction separately, i.e. they may start with the

Australian troops to the fight against Islamic State militants.

He arrived in Arnhem Land on Sunday, honouring an election promise to spend a week every year in an indigenous area. Mr Abbott says he wants to hear from local people about community needs. His visit comes a day after he committed Australian troops t

写给妹子(han zi)的含蓄表白语

转载请注明: http://blog.csdn.net/u011495472/article/details/38347441 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <algorithm> #include <vecto