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

题意:


题解:



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值