hdu 3579(中国剩余定理)

点击打开链接


题意:

给你n组线型方程x%ai=ri,求x,x必须为正数


因为x必须为正,所以WA了一次


#include"stdio.h"
#include"string.h"
#define N 8
typedef __int64 LL;
LL x,y,d;

void exgcd(LL a,LL b)
{
	if(b==0)
	{
		d=a;x=1;y=0;
		return ;
	}
	else
	{
		exgcd(b,a%b);
		LL t=x;
		x=y;
		y=t-(a/b)*y;
	}
}

int main()
{
	LL T;
	LL n;
	LL i;
	LL t=1;
	LL A[N];
	LL R[N];

	scanf("%I64d",&T);
	while(T--)
	{
		scanf("%I64d",&n);
		for(i=0;i<n;i++)
			scanf("%I64d",&A[i]);
		for(i=0;i<n;i++)
			scanf("%I64d",&R[i]);

		for(i=1;i<n;i++)
		{
			exgcd(A[i-1],A[i]);
			LL c=R[i]-R[i-1];
			if(c%d!=0)break;
			LL t=A[i]/d;
			x=((c/d*x)%t+t)%t;
			R[i]=A[i-1]*x+R[i-1];
			A[i]=A[i]*A[i-1]/d;
		}

		printf("Case %I64d: ",t++);
		if(i>=n)printf("%I64d\n",R[n-1]==0?A[n-1]:R[n-1]);
		else printf("-1\n");
	}
	return 0;
}
			


你可能感兴趣的:(数学,中国,HDU,中国剩余定理)