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

Remainders Game

2016-07-12 21:32 381 查看
D. Remainders Game

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

input
4 5
2 3 5 12


output
Yes


input
2 7
2 3


output
No


Note

In the first sample, Arya can understand 

 because 5 is
one of the ancient numbers.

In the second sample, Arya can't be sure what 

 is.
For example 1 and 7 have the same remainders after dividing
by 2 and 3, but they differ in remainders after dividing by 7.

题意是说如果能知道 x mod (c1,c2c3。。。cn)的值,能不能确定 x mod k 的值,也就是相当于要确定 C 数组整体的最小公倍数 lcm(c) 是否是 K 的倍数,如果是,则能确定输出 yes,否则输出 no,如果其中某个数已经是 K 的倍数则一定能确定。

最开始我的想法是把 C 数组的所有质数因子都乘起来在算是否为 K 的倍数,但是后来想了一下直接乘起来可能会数字特别大,而且也是错误的想法,所有质数因子乘起来不一定是 K 的倍数,例如 C 数组的所有质数因子2*3*5,K=4*6*5,最后的结果是 K 为 C 的倍数,但是这样并不能确定 x mod k 的值。

这个代码几乎是看的大神的,但是为了以后可以查看所以按照自己的理解记录下来。统计 C 数组的每个质数因子的最多的个数,如果比K的个数多则可以,少则不行

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=1e6+6;
int c[M],prime[1000],s[M];

bool isprime(int a)
{
for(int i=2;i*i<=a;i++)
{
if(a%i==0) return 0;
}
return 1;
}

int main()
{
int n,k,cnt=0;
for(int i=2;i<=1000;i++)
{
if(isprime(i))
prime[cnt++]=i;
}
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++) scanf("%d",&c[i]);
for(int i=0;i<n;i++)
{
if(c[i]%k==0)
{
puts("Yes");
return 0;
}
for(int j=0;j<cnt&&c[i]>1&&prime[j]*prime[j]<=c[i];j++)
{
int r=0;
while(c[i]%prime[j]==0)
{
r++;
c[i]/=prime[j];
}
s[prime[j]]=max(s[prime[j]],r);
}
if(c[i]>1) s[c[i]]=max(s[c[i]],1);
}
for(int j=0;j<cnt&&k>1&&prime[j]*prime[j]<=k;j++)
{
int r=0;
while(k%prime[j]==0)
{
r++;
k/=prime[j];
}
if(r>s[prime[j]])
{
puts("No");
return 0;
}
}
if(k>1)
if(s[k]>=1) puts("Yes");
else puts("No");
else puts("Yes");
return 0;
}
另外一个zeppelin_58#大神的更短的代码
#include <bits/stdc++.h>
using namespace std;

inline int gcd(int a, int b){
while(a > 0 && b > 0){
if(a > b){
a %= b;
}
else{
b %= a;
}
}
return a + b;
}

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

int main(){
int n, k, l;
scanf("%d %d", &n, &k);
l = 1;
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
int g = gcd(x, k);
l = lcm(g, l);
}
if(k == l){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lcm 素数