zoukankan      html  css  js  c++  java
  • poj2407

    题意:给出n,求欧拉函数,欧拉函数euler(n)表示小于等于n的与n互质的数的个数,在欧拉函数,认为如果两数最大公约数为1,则两数互质。所以,n与1也互质,且euler(1)=1。

    分析:计算公式为:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有不重复的质因数,x是不为0的整数。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    usingnamespace std;

    unsigned euler(unsigned x)
    {
    // 就是公式
    unsigned i, res = x;
    for (i =2; i < (int) sqrt(x *1.0) +1; i++)
    if (x % i ==0)
    {
    res
    = res / i * (i -1);
    while (x % i ==0)
    x
    /= i; // 保证i一定是素数
    }
    if (x >1)
    res
    = res / x * (x -1);
    return res;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int n;
    while (scanf("%d", &n), n !=0)
    {
    printf(
    "%d\n", euler(n));
    }
    return0;
    }
  • 相关阅读:
    第一次热身赛和正式比赛感想
    简明解释算法中的大O符号
    poj 3045
    poj 3104
    poj 3273
    poj 3258
    poj 2456
    二分法小结
    Poj 2718 Smallest Difference
    GCJ——Crazy Rows (2009 Round 2 A)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2052132.html
Copyright © 2011-2022 走看看