Files
python/TangDou/AcWing/EulerFunction/873.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

65 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##[$AcWing$ $873$. 欧拉函数](https://www.acwing.com/problem/content/875/)
### 一、题目描述
给定 $n$ 个正整数 $a_i$,请你求出每个数的欧拉函数。
**欧拉函数的定义**
![](http://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/2023/03/b2d085820a56c1e0ae0e83186f241c91.png)
**输入格式**
第一行包含整数 $n$。
接下来 $n$ 行,每行包含一个正整数 $a_i$。
**输出格式**
输出共 $n$ 行,每行输出一个正整数 $a_i$ 的欧拉函数。
**数据范围**
$1≤n≤100,1≤a_i≤2×10^9$
**输入样例:**
```cpp {.line-numbers}
3
3
6
8
```
**输出样例:**
```cpp {.line-numbers}
2
2
4
```
$Code$
```cpp {.line-numbers}
#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;
}
```