zoukankan      html  css  js  c++  java
  • 欧拉函数模版

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <vector>
    #include <set>
    using namespace std;
    typedef long long LL;
    const int maxn = 1e6+5;
    LL n;
    LL get_Euler(LL x){
        LL res = x; ///初始值
        for(LL i = 2LL; i * i <= x; ++i) {
            if(x % i == 0) {
                res = res / i * (i - 1); ///先除后乘,避免数据过大
                while(x % i == 0) x /= i;
            }
        }
        if(x > 1LL) res = res / x * (x - 1); ///若x大于1,则剩下的x必为素因子
        return res;
    }
    
    int main(){
        while(cin >> n) {
            cout << get_Euler(n) << endl; ///求n的互质数的个数
            cout << n * get_Euler(n) / 2 << endl; ///求n的所有互质数之和
        }
        return 0;
    }
    

     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <vector>
    #include <set>
    using namespace std;
    typedef long long LL;
    const int maxn = 1e6+5;
    int n, phi[maxn];
    void phi_table() {
        phi[0] = 0, phi[1] = 1; ///1的欧拉函数值为1,唯一与1互质的数
        for(int i = 2; i < maxn; ++i) phi[i] = i; ///先初始化为其本身
        for(int i = 2; i < maxn; ++i) {
            if(phi[i] == i) { ///如果欧拉函数值仍为其本身,说明i为素数
                for(int j = i; j < maxn; j += i) ///把i的欧拉函数值改变,同时也把能被素因子i整除的数的欧拉函数值改变
                    phi[j] = phi[j] / i * (i - 1);
            }
        }
    }
    int main(){
        phi_table(); ///预处理打表
        while(cin >> n) {
            cout << phi[n] << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    Mockito 简明教程
    说说初用 Mock 工具测试碰到的坑
    java的mock测试框架
    使用模拟对象(Mock Object)技术进行测试驱动开发
    微信小程序
    微信小程序
    uniapp
    微信小程序
    微信小程序
    微信小程序
  • 原文地址:https://www.cnblogs.com/wuliking/p/11162040.html
Copyright © 2011-2022 走看看