您的位置:首页 > 大数据 > 人工智能

CodeForces 687B - Remainders Game(中国剩余定理)

2016-07-06 08:54 441 查看
  

题意:给定n个数(a1,a2,a3…… )和k,对于未知数x,假如你已知x mod ai(1 <= i <= n),能否求出x mod k的值?

中国剩余定理:

若m1,m2,m3……mn两两互质,有方程组

       k ≡ a1(mod m1)

       k
≡ a2(mod m2)

 
     k ≡ a3(mod m3)

 
     ……

 
     k ≡ an(mod mn)

则能求出解k。

在此题中:已知的n个数即是以上的m1~mn,而以上的a1~an均为此题中的x,

根据中国剩余定理可得:若这n个数的最小公倍数是k的倍数,则能求出x mod k

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int MAXN = 100 + 10;
const int MAXT = 1000000 + 10;
const int INF = 0x7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
using namespace std;

int n;
llu k, a[MAXT];

llu gcd(llu a, llu b){
return b == 0 ? a : gcd(b, a % b);
}

llu lcm(llu a, llu b){
return a * b / gcd(a, b);
}

int main(){
scanf("%d%I64u", &n, &k);
for(int i = 0; i < n; ++i)  scanf("%I64u", a + i);
llu tmp = 1;
for(int i = 0; i < n; ++i){
tmp = lcm(tmp, a[i]);
tmp %= k;   //防止爆llu类型
}
if(tmp % k == 0)  printf("Yes\n");
else  printf("No\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 数论