题目链接:https://vjudge.net/contest/373416#problem/G
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 c 1, c 2, …, c n 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 c 1, c 2, …, c n (1 ≤ c i ≤ 1 000 000).
Output
Print “Yes” (without quotes) if Arya has a winning strategy independent of value of x, or “No” (without quotes) otherwise.
Input
4 5
2 3 5 12
Output
Yes
Input
2 7
2 3
Output
No
翻译:
输入n和k,第二行有n个数
c
i
c_i
ci,有一个未知数x,知道x%
c
i
c_i
ci的值。问x%k是否有唯一解?
反证法:
假设解不唯一,有两个解
x
1
x_1
x1,
x
2
x_2
x2,那么
x
1
x_1
x1,
x
2
x_2
x2满足:
x
1
x_1
x1%
c
i
c_i
ci=
x
2
x_2
x2%
c
i
c_i
ci且
x
1
x_1
x1%k!=
x
2
x_2
x2%k
由上述式子可以得出:
(
x
1
x_1
x1-
x
2
x_2
x2)%
c
i
c_i
ci=0,(
x
1
x_1
x1-
x
2
x_2
x2)%
k
k
k!=0
即 ( x 1 x_1 x1- x 2 x_2 x2)为 c i c_i ci的若干倍, c i c_i ci的若干倍不能整除k
易得
lcm(
c
1
c_1
c1,
c
2
c_2
c2,
c
n
c_n
cn) %
c
i
c_i
ci = 0
即lcm为
c
i
c_i
ci的若干倍
所以lcm % k != 0
得到命题: 如果解不唯一,那么lcm % k != 0
逆否命题为: 若lcm % k == 0 那么 解唯一
只需要判断lcm是否k 的整数倍
是的话就是yes,否则就是no
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL x,LL y)
{
if(y==0)
return x;
return gcd(y,x%y);
}
LL lcm(LL x,LL y)
{
return x*y/gcd(x,y);
}
int main()
{
LL n,k,ans=1;
scanf("%lld%lld",&n,&k);
if(n==1)
{
LL p;
scanf("%lld",&p);
if(p%k==0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
while(n--)
{
LL p;
scanf("%lld",&p);
ans=lcm(p,ans);
ans%=k;
}
if(ans==0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
后来遇见她