欧拉函数

 

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
int phi[maxn];

void euler() {
    for (int i = 2; i < maxn; i++) {
        if (!phi[i]) {
            for (int j = i; j < maxn; j += i) {
                if (!phi[j]) phi[j] = j;
                phi[j] = phi[j] / i * (i - 1);
            }
        }
    }
    phi[1] = 1;
}

int main() {
//    freopen("out", "w", stdout);
    euler();
    for (int i = 1; i < maxn; i++)
        cout << "Euler " << i << " is : " << phi[i] << endl;
    return 0;
}
View Code

 

这是一个欧拉函数的板子,小于或等于n的正整数中与n互质的数的数目

hdu2588 

GCD

http://acm.hdu.edu.cn/showproblem.php?pid=2588

#include<bits/stdc++.h>
#define int long long
using namespace std;
int Euler(int n){
    int res = n;
    for(int i = 2;i <= sqrt(n); i++){
        if(n % i == 0)
            res = res / i * (i - 1);
        while (n % i == 0)
            n /= i;
    }
    if(n > 1)
        res = res / n * (n - 1);
    return res;
}
int t,n,m,ans;
signed main(){
    ios::sync_with_stdio(0);
    cin >> t;
    while(t--){
        cin >> n >> m;
        ans = 0;
        for(int a = 1;a <= sqrt(n); a++){
            if(n % a == 0){
                if(a >= m)
                    ans += Euler(n / a);
                if(a * a != n && n / a >= m)
                    ans += Euler(a);
            }
        }
        cout << ans << endl;
    }
    return 0;
}
View Code

 

posted @ 2019-09-02 07:51  Hazelxcf  阅读(116)  评论(0编辑  收藏  举报