You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
671 B
26 lines
671 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
// 求单个数的欧拉函数值
|
|
int phi(int x) {
|
|
int res = x;
|
|
// 不要写在i*i<=x, 可能会造成爆int
|
|
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() {
|
|
int n;
|
|
cin >> n;
|
|
while (n--) {
|
|
int x;
|
|
cin >> x;
|
|
printf("%d\n", phi(x));
|
|
}
|
|
return 0;
|
|
} |