AcWing 873. 欧拉函数

题目传送门
欧拉函数专题

#include <bits/stdc++.h>

using namespace std;

/**
 * 功能:求单个数字的欧拉函数
 * @param x
 * @return
 */
int phi(int x) {
    int res = x;
    for (int i = 2; i <= x / i; i++)
        if (x % i == 0) {
            res = res / i * (i - 1);
            while (x % i == 0) x /= i;
        }
    if (x > 1) res = res / x * (x - 1); //公式内容
    return res;
}

int main() {
    //优化读入
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        cout << phi(x) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15341544.html