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~n-1,有一只青蛙初始再0位置,它每次可以跳过K个位置(0<K<n),最终跳n次回到0.求1~n-1中有多少个

K值满足在n此次跃中可以把每片荷叶就跳一次。

解题思路:

简单题,i 从 2~n-1 . 如果  n%i 取余等于0.则 i 和 i 的倍数全都不满足要求。就是n值有点大,怪吓人的,但是暴力2层for循环就过了。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
 
const int maxn = 1000000;
int vis[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        long long ans = n-1;
        memset(vis,0,sizeof(vis));
        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;
                        ans--;
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值