模线性方程
#include <stdio.h>
#include <stdlib.h>
typedef struct _EUCLID_ITEM{
int d;
int x;
int y;
/*
_EUCLID_ITEM(int arg1,int arg2,int arg3){
d=arg1;x=arg2;y=arg3;
}*/
}EUCLID_ITEM;
EUCLID_ITEM extended_euclid(int a,int b){
EUCLID_ITEM tmp,aResult;
if(b==0){
EUCLID_ITEM aResult={a,1,0};
return aResult;
}
tmp=extended_euclid(b,a%b);
aResult.d=tmp.d;
aResult.x=tmp.y;
aResult.y=tmp.x-(a/b)*tmp.y;
return aResult;
}
void modular_linear_equation_solver(int a,int b,int n){
EUCLID_ITEM t=extended_euclid(a,n);
int d=t.d;
int x0;
if(b % d ==0){
x0 = t.x * (b/d) %n; //x0的值可能为负,怎么办?
for(int i=0;i<d;++i){
printf("%d ",(x0+i*(n/d))%n);
}
}else
printf("no solutions");
}
int main(){
EUCLID_ITEM aRes=extended_euclid(99,78);
//printf("d=%d x=%d y=%d",aRes.d,aRes.x,aRes.y);
modular_linear_equation_solver(14,30,100);
return 0;
}