欧拉函数表示中与互质的数的个数

首先将经行质因数分解:,其中不同的质因子
那么与互质的数不能被整除,从一共有个数,那么:
第一步:减去能被单个质因子整除的数的个数
能被整除的数有能被p_{2}整除的数有\frac{x}{p_{2}},...,能被p_{n}整除的数有\frac{x}{p_{n}}
所以第一步要减去
第二步:加上能被两个质因子整除的数的个数
能被整除的数有个,能被p_{1} \times p_{3}整除的数有\frac{x}{p_{1} \times p_{3}}个....
所以第二步要加上
第三步:减去能被三个质因子整除的数的个数
第四步:加上能被四个质因子整除的数的个数
.....
直至处理完个质因子
那么
这个复杂的式子有一个非常优美的乘积形式:
,即
总代码:
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HelloWorld IOS;



int work(int x){
    int ans = x;
    for(int i = 2; i * i <= x; i ++){
        if(x % i == 0){
            ans = ans / i * (i - 1);
            while(x % i == 0) x /= i;
        }
    }
    if(x > 1) ans = ans / x * (x - 1);
    return ans;
}
signed main(){
    HelloWorld;
    
    int q; cin >> q;
    while(q --){
        int x; cin >> x;
        cout << work(x) << endl;
    }
    return 0;
}
解释一下这行代码:
ans = ans / i * (i - 1);
这一步就是
初始化的质因子,所以对于每一个一定能够整除