[数论] ACM 2015 Changchun B Count a*b

题意:

[数论] ACM 2015 Changchun B Count a*b_第1张图片

[数论] ACM 2015 Changchun B Count a*b_第2张图片

题解:

[数论] ACM 2015 Changchun B Count a*b_第3张图片

[数论] ACM 2015 Changchun B Count a*b_第4张图片

#include
#include
#include
#include
#include
#include
#define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++)
#define per(i,j,k) for(int i=(int)j;i>=(int)k;i--)
using namespace std;
typedef long long LL;
typedef double db;
int n,P;
inline int S1(int x){
	return (x*1ll*(x+1)/2)%P;
}
inline int S2(int x){
	int x1=x;
	int x2=x+1;
	int x3=2*x+1;
	if(x1%2==0)x1/=2;else x2/=2;
	if(x1%3==0)x1/=3;
	else if(x2%3==0)x2/=3;
	else x3/=3;
	return (x1*1ll*x2%P)*1ll*x3%P;
}
void Main(){
	int ans=0;
	int ans2=0;
	int pre1,pre2;
	pre1=pre2=0;
	for(int i=1;i<=n;i++){
		int j=n/(n/i);
		int ss2=S2(j);
		int ss1=S1(j);
		ans2=(ans2+(ss2+P-pre2)*1ll*(n/i))%P;
		ans=(ans+S1(n/i)*1ll*(ss1+P-pre1))%P;
		pre1=ss1;
		pre2=ss2;
		i=j;
	}
	printf("%d\n",(ans2+P-ans)%P);
}
int main(){
	freopen("phy.in","r",stdin);
	freopen("phy.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&P);
		Main();
	}
	return 0;
}


你可能感兴趣的:(数论,莫比乌斯反演&杜教筛)