HDOJ-1787-GCD Again 解题报告

来源:互联网 发布:淘宝特惠囤 编辑:程序博客网 时间:2024/06/01 20:46

       求欧拉函数单值,水题。题意:给你一个数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;}


0 0
原创粉丝点击