CodeForces-668D:Remainders Game (中国剩余定理||理解)
Today Pari and Arya are playing a game called Remainders.
Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value . There are n ancient numbers c1, c2, ..., cn and Pari has to tell Arya if Arya wants. Given k and the ancient values, tell us if Arya has a winning strategy independent of value of x or not. Formally, is it true that Arya can understand the value for any positive integer x?
Note, that means the remainder of x after dividing it by y.
Input
The first line of the input contains two integers n and k (1 ≤ n, k ≤ 1 000 000) — the number of ancient integers and value k that is chosen by Pari.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 1 000 000).
Output
Print "Yes" (without quotes) if Arya has a winning strategy independent of value of x, or "No" (without quotes) otherwise.
Example
4 5
2 3 5 12
Yes
2 7
2 3
No
题意:给定N,K。输入N个ni,表示已知X%ni的值,有了N组这样的剩余系,问是否X%K的值唯一。
思路:求出Lcm(ni),若Lcm(ni)%K==0,则唯一。
原因:因为中国剩余定理:ans=Σ(Ai*Ni*Mi)%N,而N就是Lcm(ni)。如果N是K的倍数,那么先%N,再%K的结果是不变的。
#include<cmath> #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; #define ll long long ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } int main() { ll N,K,x,res=1; scanf("%lld%lld",&N,&K); for(int i=1;i<=N;i++){ scanf("%lld",&x); res=x/gcd(x,res)*res%K; } if(res%K==0) printf("Yes\n"); else printf("No\n"); return 0; }