首页 技术 正文
技术 2022年11月11日
0 收藏 587 点赞 2,780 浏览 1967 个字

题面戳我

题意:求

\[\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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,824
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906