zoukankan      html  css  js  c++  java
  • 2017年上海金马五校程序设计竞赛:Problem 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
    

    分析:

    一些荷叶漂浮在湖面上其编号是0~n-1,有一只青蛙初始再0位置,它每次可以跳过K个位置(0<K<n),最终跳n次回到0.求1~n-1中有多少个K值满足在n此次跃中可以把每片荷叶就跳一次。

    i 从 2~n-1 . 如果 n%i 取余等于0.则 i 和 i 的倍数全都不满足要求。

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<map>
    using namespace std;
    int vis[1000000];
    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
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    GISer面对创业的困惑
    近期微博吐槽言论存档,涉及“性能优化”、C++陋习等
    HDU 2825 Wireless Password【AC自动机+DP】
    20130809, 微软八月安全补丁提前通知
    终于把3DMAX的MSE搞定了!
    UVA 11464 Even Parity (独特思路)
    [置顶] hdu 4418 高斯消元解方程求期望
    UVA 10652 Board Wrapping
    少儿编程-教育:少儿编程教育
    少儿编程:目录
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6941249.html
Copyright © 2011-2022 走看看