zoukankan      html  css  js  c++  java
  • 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
    ");
    49     else if(ans==-1)printf("he was definitely lying
    ");
    50     else printf("%lld
    ",(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 }
  • 相关阅读:
    xshell的优化和连接
    系统安装后的linux和vmware的网络配置
    CentOS安装系统安装完成
    最快让你上手ReactiveCocoa之进阶篇
    最快让你上手ReactiveCocoa之基础篇
    提升自己逼格的编程之美之代码规范
    告别2016迎接2017,分享一些第三方插件
    Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性
    ReactiveCocoa 5.0 初窥:可能是最痛的一次升级
    react+antd+select+lodash模糊搜索防抖
  • 原文地址:https://www.cnblogs.com/ccsu-kid/p/11372061.html
Copyright © 2011-2022 走看看