题目: http://acm.hdu.edu.cn/showproblem.php?pid=3579
题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lcm(a1,a2,a3,...an)。
别的就没什么注意的了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
__int64 a,b,c,d;
__int64 X,Y;
__int64 gcd(__int64 A,__int64 B)
{
return B==0?A:gcd(B,A%B);
}
void extend(__int64 A,__int64 B,__int64 &d,__int64 &x1,__int64 &y1)
{
if(B==0)
{
x1=1;
y1=0;
d=A;
return ;
}
extend(B,A%B,d,x1,y1);
__int64 temp=x1;
x1=y1;
y1=temp-(A/B)*y1;
return ;
}
int main()
{
__int64 S[120],E[120];
__int64 a1,r1,a2,r2,Lcm;
__int64 T;
int m,K=0;
scanf("%I64d",&T);
while(T--)
{
scanf("%d",&m);
Lcm=1;
for(int i=1; i<=m; i++)
{
scanf("%I64d",&S[i]);
Lcm=Lcm/gcd(Lcm,S[i])*S[i];//在一定程度上可以防止爆类型(Lcm*S[i]/gcd())
}
for(int i=1; i<=m; i++)
{
scanf("%I64d",&E[i]);
}
bool ifhave=true;
a1=S[1],r1=E[1];
for(__int64 i=2; i<=m; i++)
{
a2=S[i],r2=E[i];
a=a1;
b=a2;
c=r2-r1;
extend(a,b,d,X,Y);
if(c%d)
{
ifhave=false;
break;
}
__int64 t=b/d;
X=(X*(c/d)%t+t)%t;
X=a1*X+r1;
a1=a1*(a2/d);
r1=X;
}
printf("Case %d: ",++K);
if(!ifhave)
{
printf("-1\n");
continue;
}
if(r1==0)
r1+=Lcm;
printf("%I64d\n",r1);
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.