B. Remainders Game(扩展中国剩余定理)

https://codeforces.com/problemset/problem/687/B


给一堆数字,若已知xmod(这些数字中每一个),问能不能知道xmodk.知道就是yes,不知道就是no.

思路:扩展中国剩余定理的性质应用。

扩展中国剩余定理讲解部分来自:https://www.luogu.com.cn/blog/ShadderLeave/5days-equiv-from-beginner-to-killer

根据EXCRT可得,有解的情况只能是x是这些数lcm的因数。所以判x能否被lcm整除就可以了。

由于数太大,代码上注意求出lcm后%k,同时如果已经是因数了,直接break;

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+100;
typedef long long LL;
LL c[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,k;cin>>n>>k;
  for(LL i=1;i<=n;i++) cin>>c[i];
  LL ans=c[1];
  for(LL i=1;i<=n-1;i++)
  {
  	if(ans%k==0) 
	{
		ans=ans%k;
		break;
	}
  	LL tmp=c[i]/__gcd(c[i],c[i+1])*c[i+1]%k;//注意modk 
  	ans=tmp;
	c[i+1]=tmp;
  }
  if(ans%k==0) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  
return 0;
}
 

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108421517
今日推荐