ax+by=c 且X∈[x1,x2] Y∈[y1,y2] 可行解组数 exgcd



#include <cstdio>
#include <algorithm>
#include <map>
#define rep(i,l,r) for (int i=l;i<=r;++i)
typedef long long LL;
const int INF=~0U>>2;
typedef std::pair<int,int> PII;
int exgcd(int a,int b,int &x,int &y){
	if (!b){x=1,y=0;return a;}
	int g=exgcd(b,a%b,x,y),t=x;
	x=y;y=t-a/b*y;
	return g;
}
PII range(int A,int B,int C){
	if (B==0) return PII(-INF,INF);
	if (B<0) A=-A,B=-B,C=-C,std::swap(A,C);
	return PII((A-1)/B+1,C/B);
}
int calc(int A,int B,int C,int x1,int x2,int y1,int y2){
	int x0,y0;
	int G=exgcd(A,B,x0,y0);
	if (C%G) return 0;
	x0=C/G*x0,y0=C/G*y0;
	A/=G,B/=G,C/=G;
	PII X=range(x1-x0,B,x2-x0);
	PII Y=range(y1-y0,-A,y2-y0);
	if (X.first>X.second||Y.first>Y.second) return 0;
	if (X.second<Y.second) std::swap(X,Y);
	if (Y.second<X.first) return 0;
	if (Y.first>X.first) return Y.second-Y.first+1;
	else return Y.second-X.first+1;
}
int n,m;
int main(){
	freopen("diamond.in","r",stdin);
	freopen("diamond.out","w",stdout);
	scanf("%d%d",&n,&m);
	LL res=0;
	rep(x,1,n) rep(y,1,m){
		int tp=0;
		int i=x,j=y;
		if (i>j) std::swap(i,j);
		tp+=calc(2*j,-2*i,j*j-i*i,1,j-1,1,i-1);
		tp+=calc(2*j,-2*i,j*j-i*i,0,(j-1)/2,0,(i-1)/2)*2;
		if (i==j) tp--;
		res+=(LL)(n-x+1)*(m-y+1)*(tp);
		}
	printf("%I64d\n",res);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值