BZOJ 2956 模積和

題目連接:模積和php

  這種有模運算的題通常都要把取模運算給去掉,改爲除法取下整的模式。即\(a\bmod b=a-\lfloor \frac{a}{b} \rfloor b\)blog

  而後咱們先把\(i=j\)的也統計進答案,最後再減去便可。接下來就是推式子時間(\(n \le m\)):get

\begin{aligned}
&\sum_{i=1}^n\sum_{j=1}^m(n \bmod i)(m \bmod j) \\
=&\sum_{i=1}^n(n \bmod i)\sum_{j=1}^m(m \bmod j) \\
=&\sum_{i=1}^n(n-i\lfloor \frac{n}{i} \rfloor)\sum_{j=1}^m(m-j\lfloor \frac{m}{j} \rfloor) \\
=&(n^2-\sum_{i=1}^ni\lfloor \frac{n}{i} \rfloor)(m^2-\sum_{i=1}^mi\lfloor \frac{m}{i} \rfloor)
\end{aligned}io

\begin{aligned}
&\sum_{i=1}^n(n \bmod i)(m \bmod i) \\
=&\sum_{i=1}^n(n-i\lfloor \frac{n}{i} \rfloor)(m-i\lfloor \frac{m}{i} \rfloor) \\
=&n^2m-m\sum_{i=1}^ni\lfloor \frac{n}{i} \rfloor-n\sum_{i=1}^ni\lfloor \frac{m}{i} \rfloor+\sum_{i=1}^n\lfloor \frac{n}{i} \rfloor\lfloor \frac{m}{i} \rfloor i^2
\end{aligned}class

  而後分塊計算便可。上下兩個式子一減就是答案。統計

  下面貼代碼:di

#include<cstdio>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define mod 19940417
#define llg long long
#define min(a,b) (a<b?a:b)

const int n6=7776;
int n,m,a[3];

llg S(int x){return (1ll*x*(x+1)>>1)%mod;}
llg S2(int x){
	a[0]=x,a[1]=x+1,a[2]=2*x+1;
	for(int i=0;i<3;i++) if(a[i]%2==0){a[i]/=2;break;}
	for(int i=0;i<3;i++) if(a[i]%3==0){a[i]/=3;break;}
	return 1ll*a[0]*a[1]%mod*a[2]%mod;
}

llg work(int x,int y){
	llg now=0;
	for(int i=1,nt;i<=x;i=nt+1)
		nt=min(y/(y/i),x),now+=1ll*(y/i)*(S(nt)-S(i-1));
	return now%mod;
}

int main(){
	File("a");
	scanf("%d %d",&n,&m);
	if(n>m) n^=m^=n^=m;
	llg s1=work(n,n),ans;
	ans=(1ll*n*n-s1)%mod*(1ll*m*m%mod-work(m,m))%mod;
	ans+=s1*m+work(n,m)*n; ans%=mod;
	ans-=1ll*n*n%mod*m; ans%=mod;
	for(int i=1,nt;i<=n;i=nt+1){
		nt=min(n/(n/i),m/(m/i));
		ans-=(S2(nt)-S2(i-1))*(m/i)%mod*(n/i);
		ans%=mod;
	}
	printf("%lld",(ans+mod)%mod);
	return 0;
}
相關文章
相關標籤/搜索