用定义求欧拉函数
互质是公约数只有1的两个整数,叫做互质整数
φ(6)= 2。1 2 3 4 5 6中和6互质的数是1和5
求一个数的欧拉函数的话,需要对这个数先分解质因数
证明方法:容斥原理
时间复杂度O(sqrt(n))
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4 int n; 5 cin >> n; 6 while (n--) { 7 int a; 8 cin >> a; 9 int res = a; 10 for (int i = 2; i <= a / i; i++) { //分解质因数 11 if (a % i == 0) { 12 res = res / i * (i - 1); //公式,避免小数,交换顺序 13 while (a % i == 0) { 14 a /= i; 15 } 16 } 17 } 18 if (a > 1) { 19 res = res / a * (a - 1); 20 } 21 cout << res << endl; 22 } 23 return 0; 24 }