中国剩余定理
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
clock_t pre,aft;
pre=clock();
int Ext_Enuclidean(int ,int);
/*应用扩展欧几里得算法*/
int a1,b1,c1,d1,e1,f1,g1,h1,a2,b2,c2,d2,e2,f2,g2,h2,y1,y2,y3,y4,y5,y6,y7,y8;
long long s;
scanf("%d%d%d%d%d%d%d%d",&a1,&b1,&c1,&d1,&e1,&f1,&g1,&h1);
/*if((a<0&&a>=3)||(b<0&&b>=5)||(c<0&&c>=7)){
printf("No answer\n");
return;
}*/
scanf("%d%d%d%d%d%d%d%d",&a2,&b2,&c2,&d2,&e2,&f2,&g2,&h2);
if(a2>a1||b2>b1||c2>c1||d2>d1||e2>e1||f2>f1||g2>g1||h2>h1){
printf("No answer\n");
return 1;
}
int M,M1,M2,M3,M4,M5,M6,M7,M8;
M=a1*b1*c1*d1*e1*f1*g1*h1;
M1=b1*c1*d1*e1*f1*g1*h1;
M2=a1*c1*d1*e1*f1*g1*h1;
M3=a1*b1*d1*e1*f1*g1*h1;
M4=a1*b1*c1*e1*f1*g1*h1;
M5=a1*b1*c1*d1*f1*g1*h1;
M6=a1*b1*c1*d1*e1*g1*h1;
M7=a1*b1*c1*d1*e1*f1*h1;
M8=a1*b1*c1*d1*e1*f1*g1;
y1=Ext_Enuclidean(a1,M1);
y2=Ext_Enuclidean(b1,M2);
y3=Ext_Enuclidean(c1,M3);
y4=Ext_Enuclidean(d1,M4);
y5=Ext_Enuclidean(e1,M5);
y6=Ext_Enuclidean(f1,M6);
y7=Ext_Enuclidean(g1,M7);
y8=Ext_Enuclidean(h1,M8);
if(y1<0||y2<0||y3<0||y4<0||y5<0||y6<0||y7<0||y8<0){
printf("No answer\n");
return 1;
}
s=M1*y1*a2+M2*y2*b2+M3*y3*c2+M4*y4*d2+M5*y5*e2+M6*y6*f2+M7*y7*g2+M8*y8*h2;
/*printf("s=%d\n",s);*/
s%=M;
if(s<0||s>pow(2,63))
printf("No answer\n");
else
printf("%d\n",s);
aft=clock();
printf("clock time=%d\n",aft-pre);
}
int Ext_Enuclidean(int pa1,int pa2)
/*这里计算乘法逆元*/
{
if(pa1>pa2){
/*交换后pa2为较大的数*/
int tmp=pa1;
pa1=pa2;
pa2=tmp;
}
int x1,x2,x3,y1,y2,y3,t1,t2,t3;
x1=1,x2=0,x3=pa2,y1=0,y2=1,y3=pa1;
while(y3>1){
int q=x3/y3;
t1=x1-q*y1,t2=x2-q*y2,t3=x3-q*y3;
x1=y1,x2=y2,x3=y3;
y1=t1,y2=t2,y3=t3;
}
if(y1<0)
y1+=pa1;
if(y3==1)
return y1;
/*y1为乘法逆元*/
else
return -1;
}
[此贴子已经被作者于2016-3-16 14:04编辑过]