zoukankan      html  css  js  c++  java
  • hdu1286 寻找新朋友 (欧拉功能)

    原标题:点击打开链接

    关于欧拉函数的算法具体解说:点击打开链接

    欧拉函数

    1.欧拉函数是不全然积性函数。

    2.欧拉函数p(x) = x * (p1 - 1) / p1 * (p2 - 1)/p2 * .....(pn - 1)/ pn;


    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    using namespace std;
    
    int euler(int x)//Euler 模板
    {
        int 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()
    {
        int x;
        int Case;
        cin >> Case;
        while(Case--)
        {
            while(cin >> x)
            {
                cout <<euler(x) << endl;
            }
        }
        return 0;
    }
    
    
    


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    ssh 远程命令
    POJ 2287
    POJ 2376
    hihoCoder1488
    POJ1854
    HDU 5510
    HDU 4352
    CodeForces 55D
    HDU 1517
    CodeForces 1200F
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4732004.html
Copyright © 2011-2022 走看看