[尤拉函式]HDU 2824 The Euler function 題解
阿新 • • 發佈:2018-12-15
題目大意
求
解題分析
水題,字首和構造,注意空間不要爆
示例程式碼
#include<cstdio> using namespace std; typedef long long LL; const int maxn=3000005; int s,t,p[maxn]; bool vs[maxn]; LL phi[maxn]; void makep(){ vs[0]=vs[1]=0; p[0]=0; phi[1]=1; for (int i=2;i<=3e6;i++){ if (!vs[i]) {p[++p[0]]=i; phi[i]=i-1;} for (int j=1;j<=p[0]&&i*p[j]<=3e6;j++){ vs[i*p[j]]=1; if (i%p[j]) phi[i*p[j]]=phi[i]*phi[p[j]]; else {phi[i*p[j]]=phi[i]*p[j]; break;} } } for (int i=2;i<=3e6;i++) phi[i]+=phi[i-1]; } int main() { freopen("euler.in","r",stdin); freopen("euler.out","w",stdout); makep(); while (scanf("%d%d",&s,&t)==2) printf("%lld\n",phi[t]-phi[s-1]); return 0; }