​题目看这里​

最近才学的杜教筛,顺便就写一写模板题吧

杜教筛简易教程

直接上代码了

#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 10000010
#define LL long long
#define M 1000000007
using namespace std;
int w[N>>3],t; map<LL,LL> s; LL p[N]={0},n;
inline LL cal(LL n){
if(n<=10000000) return p[n];
if(s.count(n)) return s[n];
LL S=((__int128)n*(n+1)>>1)%M;
for(LL i=2,j;i<=n;i=j+1){
j=n/(n/i);
S=(S-cal(n/i)*(j-i+1)%M+M)%M;
}
return s[n]=S;
}
int main(){
p[1]=1;
for(int i=2;i<=10000000;++i){
if(!p[i]) p[w[++t]=i]=i-1;
for(int j=1,k;j<=t&&(k=i*w[j])<=10000000;++j){
if(i%w[j]==0){ p[i*w[j]]=p[i]*w[j]; break; }
else p[i*w[j]]=p[i]*(w[j]-1);
} p[i]=(p[i]+p[i-1])%M;
}
scanf("%lld",&n);
printf("%lld\n",cal(n));
}