欧拉函数 a[n] : 1~n中与n互质的数的个数 N=p1^a1+p2^a2+...+pk^ak ans=N-(1-1/p1)(1-1/p2)...(1-1/pk)
eg:a[6]=2
a[6]=6*(1-1/2)*(1-1/3)
容斥原理(证明欧拉函数):N-N/p1-N/p2.....N/pk+N/p1*p2+N/p1*p3+...+N/p2*p3+...==(N
-(1-1/p1)(1-1/p2)...(1-1/pk));
1. 从1~n中去掉p1、p2.....pk的所有倍数
2. 加上所有pi*pj的倍数
3. 减去所有pi*pj*pk的倍数
......
//定义法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n;
void solve(){
cin>>n;
while(n--){
int a;
cin>>a;
int res=a;
for(int i=2;i<a/i;i++){
if(a%i==0){
res=res/i*(i-1);
while(a%i==0)a/=i;
}
}
if(a>1)res=res/a*(a-1);
cout<<res<<endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t=1;
//cin>>t;
while(t--){
solve();
}
}
360




被折叠的 条评论
为什么被折叠?



