原题链接:https://www.luogu.com.cn/problem/CF687B
题意解读:已知x对c1、c2...cn取模的结果,问是否可以求得x%k。
解题思路:
推公式,根据已知条件看能得出什么。
设只有c1、c2的情况,
x = c1*t1 + b1,x = c2*t2 + b2
c1*t1 + b1 = c2*t2 + b2
c1*t1 - c2*t2 = b2 - b1,要使的t1,t2有解,必须满足gcd(c1,c2) | (b2 - b1)
上式两边同时除以d = gcd(c1,c2),得(c1/d)*t1 = (t2/d)*t2 + (b2-b1)/d
即(c1/d)*t1 ≡ (b2-b1)/d (mod c2/d)
有t1 ≡ (c1/d)-1*(b2-b1)/d (mod c2/d)
t1 = (c2/d)*t + (c1/d)-1*(b2-b1)/d,将此t1代入x = c1*t1 + b1
得到x = (c1c2/d)*t + (c1/d)*(c1/d)-1*(b2-b1) + b1
c1c2/d即LCM(c1,c2)
从x%c1,x%c2的值可以推出x%LCM(c1,c2)的值,只需要LCM(c1,c2)是k的倍数,就可以推出x%k的值
因此,推而广之,只要LCM(c1,c2...cn) % k = 0,则认为可以得到x % k的结果。
100分代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;const int N = 1000005;
int n, k;LL gcd(LL a, LL b)
{return b == 0 ? a : gcd(b, a % b);
} LL lcm(LL a, LL b)
{return a * b / gcd(a, b);
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cin >> n >> k;LL res = 1;for(int i = 1; i <= n; i++) {LL c;cin >> c;res = lcm(res, c);res %= k;}if(res == 0) cout << "Yes";else cout << "No";return 0;
}