0
点赞
收藏
分享

微信扫一扫

2019牛客暑期多校训练营(第十场)Han Xin and His Troops(高精度拓展中国剩余定理


题目来源

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

题意



如果不存在则输出:​​​he was definitely lying​​​存在但是则输出:​​​he was probably lying​​​否则输出

思路

二次剩余(高精度

C++

#include<bits/stdc++.h>
using namespace std;

#define
I read(){long long x;scanf("%lld",&x);return x;}
void exgcd(I a,I&x,I b,I&y,I c) { // assert(__gcd(a,b)==c)
if (b == 0) x = c / a, y = 0;
else exgcd(b, y, a % b, x, c), y -= a / b * x;
}

bool merge(I x1,I p1,I x2,I p2,I&x,I&p) {
I u, v, d = __gcd(p1, p2);
if ((x2 - x1) % d != 0) return false;
exgcd(p1, u, p2, v, x2 - x1);
p = p1 / d * p2;//lcm
I n = p2 / d;
u = ((u % n) + n) % n;
x = u * p1 + x1;
return true;
}

int main() {
I n = read(), m = read();
I x = 0, p = 1, flag = 0;
for (I i = 0; i < n; i++) {
I p1 = read(), x1 = read();
if (flag == 2) continue;
if (!merge(x, p, x1, p1, x, p)) flag = 2;
else if (x > m) flag = 1;
}
if (flag == 0) printf("%lld", x);
else if (flag == 1) puts("he was probably lying");
else puts("he was definitely lying");
return 0;
}

Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
def gcd(a,b):
return a if b==0 else gcd(b,a%b)

def exgcd(a,b):
if b==0:
return 1,0
y,x=exgcd(b,a%b)
y-=(a//b)*x
return x,y

def inv(a,b):
x,y=exgcd(a,b)
return (x%b+b)%b

def main():
n,M=map(lambda x:int(x),input().split(' '))
m=-1;c=-1
for i in range(n):
a,b=map(lambda x:int(x),input().split(' '))
if c==-1:
m=a;c=b
else:
g=gcd(m,a)
if (c-b)%g!=0:
print('he was definitely lying')
exit()
c+=(inv(m//g,a//g)*(b-c)//g)%(a//g)*m
m=m*a//g
if M>=c:
print(str(c))
else:
print('he was probably lying')

if __name__ == '__main__':
main()

Java

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
public static boolean vis = false;
public static BigInteger x;
public static BigInteger y;
public static BigInteger m[] = new BigInteger[200];
public static BigInteger r[] = new BigInteger[200];
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
BigInteger M = cin.nextBigInteger();
for(int i = 1;i <= n; ++i){
m[i] = cin.nextBigInteger();
r[i] = cin.nextBigInteger();
}
BigInteger ans = excrt(n);
if(vis) System.out.println("he was definitely lying");
else if(ans.compareTo(M)>0) System.out.println("he was probably lying");
else System.out.println(ans);
}
public static BigInteger exgcd(BigInteger a,BigInteger b){
if(b==BigInteger.ZERO){
x = BigInteger.ONE;
y = BigInteger.ZERO;
return a;
}
BigInteger d = exgcd(b,a.mod(b));
BigInteger t = x;
x = y;
y = t.subtract(a.divide(b).multiply(y));
return d;
}
public static BigInteger excrt(int n){
BigInteger a = m[1],rr=r[1];
for(int i=2;i<=n;++i){
BigInteger b = m[i];
BigInteger c = r[i].subtract(rr);
BigInteger d = exgcd(a,b);
if(c.mod(d) != BigInteger.ZERO){
vis = true;
return BigInteger.ZERO;
}
c = c.divide(d); b = b.divide(d);
x = (x.multiply(c).mod(b).add(b)).mod(b);
BigInteger lcm = a.multiply(b);
rr = (x.multiply(a).mod(lcm).add(rr)).mod(lcm);
a = lcm ;
}
return rr;
}
}


举报

相关推荐

0 条评论