2017年上海金馬五校程序設計競賽:Problem I : Frog's Jumping

Problem I : Frog's Jumping


 (Out of Contest)

Time Limit: 1 s

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

題目意思:有0,1,2,3,,,,n-1個荷葉,然後青蛙從0開始每次走(k+x)%n步數,x爲當前荷葉的下標,然後走n次,並且不能走到原來的走過的地方,問,輸入一個n,有多少種小於n符合題目要求的N的個數。

然後也就是找規律,就是如果這個數是n的約數的,一定會走到曾經走過的地方,因爲如果是約數,那麼這個數乘一個小於N的數肯定能等於n的倍數。所以在走n次之內一定會走原來的地方,所以把所有次數找出來減去這個就是所求答案。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int vis[1000005];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        int s=0;
        for(int i=2;i<=n/2;i++)
        {
               if(n%i==0)
                {
                    for(int j=i;j<n;j+=i)
                    {
                        if(vis[j]==0)
                        {
                            vis[j]=1;
                            s++;
                        }
                    }
                }
        }
        if(vis[n]==0)
            s++;
        cout<<n-s<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章