ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

洛谷 P2260[清华集训2012]模积和(数论分块)

2021-05-16 20:33:25  阅读:150  来源: 互联网

标签:lfloor 洛谷 rfloor int sum P2260 long 模积 mod


题目连接

\(Description\)

\[\sum_{i=1}^n\sum_{j=1}^m(n\ mod\ i)(m\ mod\ i),i\neq j \]

答案对\(19940417\)取模。
\(n,m\leq10^9\)


\(Solution\)
如果不考虑\(i\neq j\)这个条件,根据和式的分配率答案就是

\[\sum_{i=1}^nn\ mod\ i\sum_{i=1}^mm\ mod\ i \]

其中\(\sum_{i=1}^nn\ mod\ i=\sum_{i=1}^nn-\lfloor\frac{n}{i}\rfloor\times i\),接下来就可以通过数论分块\(O(\sqrt n)\)计算答案了(详细可见这道题)。
考虑怎么在这两项的乘积当中减去\(i=j\)的部分。

\[\sum_{i=1}^{min(n,m)}(n-\lfloor\frac{n}{i}\rfloor\times i)\times (m-\lfloor\frac{m}{i}\rfloor\times i) \]

\[=\sum_{i=1}^{min(n,m)}nm-(n\lfloor\frac{m}{i}\rfloor+m\lfloor\frac{n}{i}\rfloor)\times i+\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{i}\rfloor i^2 \]

常数项可单独算,其余两项也都可用数论分块求得。

一个坑点是模数不是质数,在计算\(\sum_{i=l}^ri和\sum_{i=l}^ri^2\)时不能用费马小定理去乘2和6的逆元,但可以通过扩展欧几里得来求。可以,但没必要(手动滑稽)。

#include<complex>
#include<cstdio>
using namespace std;
const int mod=19940417;
int n,m,ans;
int Calc1(int x)
{
	long long res=0;
	for(int l=1,r;l<=x;l=r+1)
	{
		r=x/(x/l);
		res+=1ll*(x/l)*(r-l+1)*(l+r);
	}
	return (1ll*x*x-res/2)%mod;
}
int SqrSum(int x)
{
	int a=x+1,b=2*x+1,c=x;
	if(x&1)a>>=1;
	else c>>=1;
	if(x%3==1)b/=3;
	else if(x%3==2)a/=3;
	else c/=3;
	return 1ll*a*b%mod*c%mod;
}
int Calc2(long long x,long long y)
{
	long long res=0,lim=min(n,m);
	for(int l=1,r;l<=lim;l=r+1)
	{
		r=min(x/(x/l),y/(y/l));
		res+=(x/l)*(y/l)%mod*(SqrSum(r)-SqrSum(l-1))%mod;
		res-=1ll*(r-l+1)*(l+r)/2%mod*(x*(y/l)%mod+y*(x/l)%mod)%mod;
	}
	return (x*y%mod*lim+res)%mod;
}
int main()
{
	scanf("%d%d",&n,&m);
	printf("%d\n",(1ll*Calc1(n)*Calc1(m)-Calc2(n,m)+mod)%mod);
	return 0;
}

标签:lfloor,洛谷,rfloor,int,sum,P2260,long,模积,mod
来源: https://www.cnblogs.com/LeTri/p/14774704.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有