现在的位置: 首页 > 综合 > 正文

HDOJ-1787-GCD Again 解题报告

2018年04月21日 ⁄ 综合 ⁄ 共 497字 ⁄ 字号 评论关闭

       求欧拉函数单值,水题。题意:给你一个数n,求出大于0小于n的正整数中与n的最大公约数大于1的数的个数。


       我的解题思路:与n的公约数大于1的数的个数就是与n不互素的数的个数。这里有个坑,首先就是小于n,因此输出应该是n - 1 - phi(n)。


       我的解题代码:

#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>

using namespace std;

int Phi(int x);

int main()
{
    int x;
    while (~scanf("%d", &x))
    {
        if (x == 0) break;
        printf("%d\n", x - 1 - Phi(x));
    }
    return 0;
}

int Phi(int x)
{
    int ans = x;
    int cnt = (int)sqrt(x + 0.5) + 1;
    for (int i=2; i<cnt; ++i)
    {
        if (x % i == 0)
        {
            ans -= ans / i;
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) ans -= ans / x;
    return ans;
}

抱歉!评论已关闭.