【循环数组】#51 A. Flea travel

A. Flea travel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A flea is sitting at one of the n hassocks, arranged in a circle, at the moment. After minute number k the flea jumps through k - 1 hassoсks (clockwise). For example, after the first minute the flea jumps to the neighboring hassock. You should answer: will the flea visit all the hassocks or not. We assume that flea has infinitely much time for this jumping.

Input

The only line contains single integer: 1 ≤ n ≤ 1000 — number of hassocks.

Output

Output "YES" if all the hassocks will be visited and "NO" otherwise.

Sample test(s)
input
1
output
YES
input
3
output
NO


枚举题意和调试累死我了……Q^Q 居然memset和init写反了……

有一个跳蚤在一圈草丛中的一个,顺时针跳,第1分钟跳1个,第二分钟俩,第三分钟仨~

问这么无限下去能不能有朝一日给它遍历完。

其实不是无限啦~ 既然是一圈的话,跳N个不就是跳N-N%n个嘛~

那么我们记录每一个草丛第一次经过的时候跳跃力mod(n)是step,下一次又用这个跳跃力mod(n)到达这里之后不就是无限循环了嘛~

Code:

#include <cstdio>
#include <memory>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;	cin>>n;
	if(n==1)
	{
		cout<<"YES"<<endl;
		return 0;
	}
	int steps=0,mrk[1001];
	int now=0,cnt=0,flag=0,step=0;
	memset(mrk,-1,sizeof mrk);
	mrk[0]=0,cnt=1,step=0,now=0;
	while(1)
	{
		if(cnt==n)
		{
			flag=1;
			break;
		}
		
		step= (step+1)%n;
		now= (now+step)%n;
		
		if(mrk[now]==step)	break;
		if(mrk[now]==-1)
		{
			cnt++;
			mrk[now]=step;	
		}
		//cout<<now<<":"<<mrk[now]<<"  "<<cnt<<endl;
	} 
	if(flag)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值