题目大意
求 ∑ i = L R φ ( i ) \sum_{i=L}^R \varphi(i) ∑i=LRφ(i)
解题分析
水题,前缀和构造,注意空间不要爆
示例代码
#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;
}