//欧拉函数 小于等于 n 且与n互质的正整数个数 #include <bits/stdc++.h> using namespace std; const int N = 100001; int n,p; int prime[N],phi[N],mark[N]; int main(){ cin >> n; phi[1] = 1; for(int i = 2; i <= n; ++i){ if(!mark[i]){ prime[++p] = i; phi[i] = i - 1; } for(int j = 1; j <= p; ++j){ if(i * prime[j] > n) break; mark[i * prime[j]] = 1; if(i % prime[j] == 0){ phi[i * prime[j]] = prime[j] * phi[i]; //欧拉积性 if(a|b) phi(ab) = a * phi(b); break; } else phi[i * prime[j]] = phi[i] * phi[prime[j]];//欧拉积性 if(a互质b) phi(ab) = phi(a) * phi(b); } } for(int i = 1; i <= p; ++i) cout << prime[i] << " "; cout << endl; for(int i = 1; i <= n; ++i) cout << phi[i] << " "; return 0; }
原文:https://www.cnblogs.com/Adventurer-H/p/10884492.html