学到了好多东西啊这题。。。
https://blog.csdn.net/sdfzyhx/article/details/72968468
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e6+10,mod=1e9+7; 4 typedef long long ll; 5 ll miu[N],v[N],p[N],cnt,sum1[N],sum2[N],mi1[N],mi2[N],h[N],g[N],f[N],T,n; 6 ll inc(ll a,ll b) 7 { 8 a=a+b; 9 if(a>mod)a%=mod; 10 return a; 11 } 12 ll dec(ll a,ll b) 13 { 14 a=a-b; 15 if(a<0)a+=mod; 16 return a%mod; 17 } 18 ll qmod(ll x,ll y) 19 { 20 int ans=1; 21 while(y) 22 { 23 if(y&1)ans=1ll*ans*x%mod; 24 x=1ll*x*x%mod;y>>=1; 25 } 26 return ans; 27 } 28 void init() 29 { 30 miu[1]=sum1[1]=sum2[1]=mi1[1]=mi2[1]=1; 31 for(int i=2;i<=1e6;++i) 32 { 33 if(!v[i]) 34 { 35 p[++cnt]=i; 36 miu[i]=-1; 37 sum1[i]=i+1; 38 sum2[i]=inc(1ll*i*i%mod+i,1); 39 mi1[i]=mi2[i]=i; 40 } 41 for(int j=1;j<=cnt&&i*p[j]<=1e6;++j) 42 { 43 int x=i*p[j];v[x]=1; 44 if(i%p[j]) 45 { 46 mi1[x]=mi2[x]=p[j]; 47 miu[x]=-miu[i]; 48 sum1[x]=1ll*sum1[i]*sum1[p[j]]%mod; 49 sum2[x]=1ll*sum2[i]*sum2[p[j]]%mod; 50 } 51 else 52 { 53 mi1[x]=p[j]; 54 mi2[x]=1ll*mi2[i]*p[j]%mod; 55 sum1[x]=inc(sum1[i],1ll*p[j]*mi2[i]%mod*sum1[i/mi2[i]]%mod);//求约数和 56 sum2[x]=inc(sum2[i],1ll*inc(1ll*mi2[i]*mi2[i]%mod*mi1[i]%mod,1ll*mi2[i]*mi2[i]%mod*mi1[i]%mod*mi1[i]%mod)*sum2[i/mi2[i]]%mod);//求平方约数和 57 break; 58 } 59 } 60 } 61 for(int i=1;i<=1e6;++i) 62 { 63 h[i]=inc(h[i-1],sum1[i]); 64 g[i]=1ll*i*sum1[i]%mod*h[i]%mod; 65 sum2[i]=1ll*sum2[i]*i%mod; 66 sum2[i]=inc(sum2[i],sum2[i-1]); 67 } 68 for(int i=1;i<=1e6;++i) 69 { 70 71 for(int j=i,k=1;j<=1e6;j+=i,k++) 72 f[j]=inc(f[j],1ll*miu[i]%mod*i%mod*i%mod*g[k]%mod); 73 f[i]=inc(f[i],f[i-1]); 74 } 75 return; 76 } 77 int main() 78 { 79 init();scanf("%d",&T); 80 for(int i=1;i<=T;++i) 81 { 82 scanf("%d",&n); 83 printf("Case #%d: %lld ",i,(dec(2ll*f[n]%mod,sum2[n])+mod)%mod); 84 } 85 return 0; 86 }