2017年上海金马五校程序设计竞赛 I : Frog's Jumping 找规律


Description

There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., n-1 respectively. The leaf 0 and n-1 are adjacent.

The frog king wants to play a jumping game. He stands at the leaf 0 initially. For each move, he jumps k (0 < k < n) steps forward. More specifically, if he is standing at the leaf x, the next position will be the leaf (x + k) % n.

After n jumps, he wants to go through all leaves on the lake and go back to the leaf 0 finally. He can not figure out how many different k can be chosen to finish the game, so he asks you for help.
 


 

Input

There are several test cases (no more than 25).
For each test case, there is a single line containing an integer n (3 ≤ n ≤ 1,000,000), denoting the number of lotus leaves.

 

Output

For each test case, output exactly one line containing an integer denoting the answer of the question above.

 

Sample Input

4
5

 

Sample Output

2
4



n个点,跳n次,每个点都要遍历一遍,说明啥,就是没跳一个点都是没跳过的,不能重复跳一个点,如果从一到n一个个去循环一遍的话,不算while(n--)的那层循环就是n平方的时间复杂度,1000000的n方已经吃不消了,肯定超时,这时候就要找一下规律了。

从一开始的想法出发,如果出现重复的话肯定是从第0个点开始重复的,如果没有到最后一次跳就跳到了第0个点的话,这个k的倍数一定也是n的整数倍。于是我就找了n的的约数,然后把约数的所有整数倍x都规定为会出现重复排除掉(x<n),额,这个想法呢属于突发奇想,不过他过了。


#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstring>
using namespace std;
int X[1000000];
int main()
{
	int n;
	while(cin>>n)
	{
		int num=0;
		memset(X,0,sizeof(X));
		for(int i=2;i<=n/2;i++)
 	    {
		     if(n%i==0)
		     {
		     	for(int j=1;j<n;j++)
		     	{
		     		if(j*i<n) X[i*j]=1;
		     		else break;
				 }
			 }
    	}
    	for(int i=1;i<n;i++)
    	{
    		if(X[i]==0) num++;
		}
		cout<<num<<endl;
	}
	
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_我走路带风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值