输入包括两个数a, b,中间用空格分隔(2 <= a <= b <= 10^10)Output
输出S(a, b)。Input示例
3 10Output示例
-1相关问题 杜教筛裸题 $\sum_{i=1}^{n}\mu(i) = 1 - \sum_{d=2}^{n}\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\mu(i)$
#include<cstdio> #include<map> #define LL long long using namespace std; const int MAXN=5000030; int limit=5000000,tot=0,vis[MAXN],prime[MAXN]; LL N,mu[MAXN]; void GetMu() { vis[1]=1;mu[1]=1; for(int i=1;i<=limit;i++) { if(!vis[i]) prime[++tot]=i,mu[i]=-1; for(int j=1;j<=tot&&i*prime[j]<=limit;j++) { vis[i*prime[j]]=true; if(i%prime[j]==0) {mu[i*prime[j]]=0;break;} else mu[i*prime[j]]=-mu[i]; } } for(int i=1;i<=limit;i++) mu[i]+=mu[i-1]; } map<LL,LL>Amu; LL SolveMu(LL n) { if(n<=limit) return mu[n]; if(Amu.count(n)) return Amu[n]; LL tmp=1,nxt; for(LL i=2;i<=n;i=nxt+1) { nxt=n/(n/i); tmp-=(nxt-i+1)*SolveMu(n/i); } return Amu[n]=tmp; } int main() { GetMu(); LL a,b; scanf("%lld%lld",&a,&b); printf("%lld",SolveMu(b)-SolveMu(a-1)); return 0; }