您的位置:首页 > 其它

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

2017-06-03 21:43 351 查看


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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐