[Luogu3768]简单的数学题
题面戳我
题意:求
\[\sum_{i=1}^{n}\sum_{j=1}^{n}ij\gcd(i,j)
\]
\(n\le10^{10}\)
sol
\[ans=\sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=1}^{n}ij[gcd(i,j)==d]\\=\sum_{d=1}^{n}d^3\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}ij[gcd(i,j)==1]\\=\sum_{d=1}^{n}d^3\sum_{i=1}^{n/d}\mu(i)i^2(\frac{\lfloor\frac{n}{id}\rfloor(\lfloor\frac{n}{id}\rfloor+1)}{2})^2\\=\sum_{T=1}^{n}(\frac{\lfloor\frac{n}{T}\rfloor(\lfloor\frac{n}{T}\rfloor+1)}{2})^2\sum_{d|T}d^3\mu(\frac Td)(\frac Td)^2\\=\sum_{T=1}^{n}(\frac{\lfloor\frac{n}{T}\rfloor(\lfloor\frac{n}{T}\rfloor+1)}{2})^2\sum_{d|T}d\mu(\frac Td)T^2
\]
然后有一个东西
\[\sum_{d|i}d\mu(\frac id)=\varphi(i)
\]
所以我们需要处理出\(\varphi(i)i^2\)的前缀和就行了。前缀和由杜教筛负责。
分块前面即可
code
读入优化记得开long long
以下代码使用了Gay神讲到的小trick
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 10000000;
ll gi()
{
ll x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
ll n;
int mod,inv2,inv6,maxN,pri[N+5],tot,zhi[N+5];
ll phi[N+5],F[100000];
int fastpow(int a,int b)
{
int res=1;
while (b) {if (b&1) res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}
return res;
}
void Mobius()
{
zhi[1]=phi[1]=1;
for (int i=2;i<=maxN;i++)
{
if (!zhi[i]) pri[++tot]=i,phi[i]=i-1;
for (int j=1;j<=tot&&i*pri[j]<=maxN;j++)
{
zhi[i*pri[j]]=1;
if (i%pri[j]) phi[i*pri[j]]=phi[i]*phi[pri[j]];
else {phi[i*pri[j]]=phi[i]*pri[j];break;}
}
}
for (int i=1;i<=maxN;i++) phi[i]=(phi[i-1]+phi[i]*i%mod*i%mod)%mod;
}
ll Sum(ll x){x%=mod;return x*(x+1)%mod*inv2%mod;}
ll Sqr(ll x){x%=mod;return x*(x+1)%mod*(x+x+1)%mod*inv6%mod;}
ll Phi(ll x)
{
if (x<=maxN) return phi[x];
if (F[n/x]) return F[n/x];
ll res=Sum(x);res=res*res%mod;
ll i=2,j;
while (i<=x)
{
j=x/(x/i);
res=(res-(Sqr(j)-Sqr(i-1)+mod)%mod*Phi(x/i)%mod+mod)%mod;
i=j+1;
}
return F[n/x]=res;
}
int main()
{
mod=gi();n=gi();
maxN=min(n,(ll)N);
Mobius();
inv2=fastpow(2,mod-2);
inv6=fastpow(6,mod-2);
ll i=1,j,ans=0,yyb;
while (i<=n)
{
j=n/(n/i);
yyb=Sum(n/i);yyb=yyb*yyb%mod;
ans=(ans+(Phi(j)-Phi(i-1)+mod)%mod*yyb%mod)%mod;
i=j+1;
}
printf("%lld\n",ans);
return 0;
}