分析
首先看第一个式子,根据莫比乌斯函数的性质可知,当i=1时,μ(i)=1,其它都是0.所以输出1
然后看第二个式子。根据欧拉函数的性质,平方的部分多出来的质因数已经出现过,所以式子变成∑ni=1iϕ(i)。这个用杜教筛来求。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e6+5,mo=1e9+7,Inv=166666668;
typedef long long LL;
int n,tot,p[N],phi[N],s[N];
bool bz[N];
map <int,int> h;
int calc(int n)
{
if (n<N) return s[n];
if (h[n]>0) return h[n];
int ans=(LL)n*(n+1)%mo*(2*n+1)%mo*Inv%mo,la=1;
for (int i=2,j;i<=n;i=j+1)
{
j=n/(n/i);
int x=(LL)(j+1)*j/2%mo;
ans=(ans-(LL)(x-la)*calc(n/i))%mo;
la=x;
}
if (ans<0) ans+=mo;
return h[n]=ans;
}
int main()
{
phi[1]=s[1]=1;
for (int i=2;i<N;i++)
{
if (!bz[i]) p[tot++]=i,phi[i]=i-1;
s[i]=(s[i-1]+(LL)i*phi[i])%mo;
for (int j=0;j<tot && i*p[j]<N;j++)
{
bz[i*p[j]]=1;
if (i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
scanf("%d",&n);
printf("1\n%d\n",calc(n));
return 0;
}