2019牛客暑期多校训练营(第十场)D Han Xin and His Troops(拓展中国剩余定理C++和JAVA版本)

https://ac.nowcoder.com/acm/contest/890/D

板子题套个好板子即可ac

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll __int128
 4 #define LL long long
 5 void exgcd(ll a,ll b,ll &g,ll &x,ll &y){
 6     if(!b){
 7         g=a;
 8         x=1;
 9         y=0;
10         return;
11     }
12     exgcd(b,a%b,g,y,x);
13     y-=(a/b)*x;
14 }
15 bool flag=false;
16 ll a1,a2,n1,n2;
17 void china(){
18     ll d=a2-a1;
19     ll g,x,y;
20     exgcd(n1,n2,g,x,y);
21     if(d%g==0){
22         x=((x*d/g)%(n2/g)+(n2/g))%(n2/g);
23         a1=x*n1+a1;
24         n1=(n1*n2)/g;
25     }
26     else flag=true;
27 }
28 LL mo[1005],a[1005];
29 ll realchina(int n){  
30     a1=a[1];
31     n1=mo[1];
32     for(int i=2;i<=n;i++){
33         a2=a[i];
34         n2=mo[i];
35         china();
36         if(flag)return -1;
37     }
38     return a1;
39 }
40 int n;
41 LL m;
42 int main(){
43     scanf("%d%lld",&n,&m);
44     for(int i=1;i<=n;i++){
45         scanf("%lld%lld",&mo[i],&a[i]);
46     }
47     ll ans=realchina(n);
48     if(ans>m)printf("he was probably lying\n");
49     else if(ans==-1)printf("he was definitely lying\n");
50     else printf("%lld\n",(LL)ans);
51 }

 

 1 //package 实验;
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 import java.util.*;
 5 import java.io.*;
 6 public class Main {
 7     static BigInteger m[]=new BigInteger[105];
 8     static BigInteger a[]=new BigInteger[105];
 9     static BigInteger tmp1;
10     static BigInteger tmp2,d,ans,t,x,y;
11     static BigInteger z0=BigInteger.ZERO;
12     static BigInteger z1=BigInteger.ONE;
13     static BigInteger z2=z1.negate();
14     boolean flag;
15     static BigInteger exgcd(BigInteger a1,BigInteger b1) {
16         if(b1.equals(z0)) {
17             x=z1;
18             y=z0;
19             return a1;
20         }
21         BigInteger d=exgcd(b1,a1.mod(b1));
22         BigInteger t2=x;
23         x=y;
24         BigInteger t1=a1.divide(b1);
25         t1=t1.multiply(y);
26         y=t2.subtract(t1);
27         return d;
28     }
29     static BigInteger EXCRT(int n){
30         BigInteger M=m[1],A=a[1];
31         for(int i=2;i<=n;i++){
32             BigInteger d=exgcd(M,m[i]);
33             BigInteger c=a[i].subtract(A);
34             if(c.mod(d) != BigInteger.valueOf(0)) return BigInteger.valueOf(-1);
35             BigInteger mul=m[i].divide(d);
36             x=(c.divide(d).multiply(x).mod(mul).add(mul)).mod(mul);
37             A=A.add(M.multiply(x));
38             M=M.multiply(mul);
39             A=A.mod(M);
40         }
41         return (A.add(M)).mod(M);
42     }
43     public static void main(String [] args){
44         Scanner cin = new Scanner(System.in);
45         int n;
46         BigInteger T;
47         n=cin.nextInt();
48         T=cin.nextBigInteger();
49         for(int i=1;i<=n;i++) {
50             m[i]=cin.nextBigInteger();
51             a[i]=cin.nextBigInteger();
52         }
53         BigInteger ans=EXCRT(n);
54         if(ans.equals(z2)) {
55             System.out.println("he was definitely lying");
56         }
57         else if(ans.compareTo(T)>0) {
58             System.out.println("he was probably lying");
59         }
60         else {
61             System.out.println(ans);
62         }
63     }
64 }

 

posted @ 2019-08-18 12:54  Venux  阅读(227)  评论(0编辑  收藏  举报