33,028
社区成员
发帖
与我相关
我的任务
分享
void exgcd(int m,int n,int *a,int *b)
{
int a1,b1,q,r,t,c,d;
a1=*b=1;
b1=*a=0;
c=m;
d=n;
while(r=c%d)
{
q = c/d;
c = d;
d = r;
t = a1;
a1 = *a;
*a = t-q*(*a);
t = b1;
b1 = *b;
*b = t-q*(*b);
}
}
void solution( )
{
int j,i=0;
int *ptr1,*ptr2,*ptr3,*ptr4,*a,*b;
int M=1;
int x=0;
printf("input how many datas in your problem:\n");
scanf("%d",&n);
ptr1 = malloc( n*sizeof ( int ) ); /* 存放输入xi */
ptr2 = malloc( n*sizeof ( int ) ); /* 存放输入mi */
ptr3 = malloc( n*sizeof ( int ) ); /* 存放乘法逆元Si */
ptr4 = malloc( n*sizeof ( int ) );/* 存放Mi */
if(( ptr1==NULL) || (ptr2==NULL) ||( ptr3==NULL) || (ptr4==NULL) )
{
printf("error in memory malloc!\n");
return;
}
for(j=0;j<n;j++)
{
printf("please input your %dth group data: ",++i);
scanf("%d %d",&ptr1[j],&ptr2[j]);
M *= ptr2[j];
}
for(j=0;j<n;j++)
{
ptr4[j] = M/ptr2[j];
exgcd(ptr4[j],ptr2[j],a,b);
ptr3[j]= *a;
while(ptr3[j]<0)
{
ptr3[j]+=ptr2[j];
}
}
for(j=0;j<n;j++)
{
x+ = ( ptr1[j]*ptr4[j]*ptr3[j] )%M;
}
printf("%d",x);
}
void main( )
{
solution();
getch();
}