天天看点

HDU 2824 The Euler function (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=3e6+5;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

int tot,prim[maxn/10];
bool vis[maxn];///优化一下内存
ll phi[maxn];
void sieve(){
    mst(phi,0),phi[1]=1;
    for(int i=2;i<maxn;i++){
        if(!vis[i]) prim[tot++]=i,phi[i]=i-1;
        for(int j=0;j<tot;j++){
            if(1LL*i*prim[j]>=maxn) break;
            int tmp=i*prim[j];vis[tmp]=true;
            if(i%prim[j]==0) phi[tmp]=phi[i]*prim[j];
            else phi[tmp]=phi[i]*(prim[j]-1);
        }
    }
    rep(i,1,maxn) phi[i]+=phi[i-1];
}
int a,b;
int main(){
    sieve();
    while(scanf("%d%d",&a,&b)!=EOF){
        printf("%lld\n",phi[b]-phi[a-1]);
    }
    return 0;
}
           

继续阅读