跳到正文

题解

题解 P5887 【Ringed Genesis】

gyh20约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月14日
篇幅
约 2 分钟
获赞
1
题目
P5887

版本

  1. v1当前

在一个长度为nn的环上,一次走kk步能到达的点就是只走一圈每一步走gcd(k,n)gcd(k,n)步所到达的位置。

于是可以得出方法。

令kk为gcd(k,n)gcd(k,n)。

对于每一个p[i]p[i],令v[p[i]%k]v[p[i]\% k]为1。

最后检查11~nn的每一个数,如果!v[i%k]v[i\% k]则将答案+1+1。

代码如下:

C++
#include<cstdio>
using namespace std;
#define re register
const int Mxdt=500;
inline char gc(){
	static char buf[Mxdt],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read()
{
	int res=0;char ch=gc();
	while(ch<'0'||ch>'9')ch=gc();
	while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+(ch^48),ch=gc();
	return res;
}
int n,m,a[1000002],p,b,ans;
bool v[1000002];
inline int gcd(re int x,re int y){
	return y?gcd(y,x%y):x;
}
int main(){
	n=read();
	m=read(); 
	p=read();
	p=gcd(p,n);
	for(re int i=1;i<=m;++i)v[read()%p]=1;
	for(re int i=1;i<=n;++i)if(!v[i%p])++ans;
	printf("%d",ans);
}
原文