'commit'
This commit is contained in:
26
TangDou/AcWing/EulerFunction/873.cpp
Normal file
26
TangDou/AcWing/EulerFunction/873.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user