#include
// 递推法实现扩展欧几里德算法
long exgcd(long a, long b, long *x, long *y)
{
long x0=1, y0=0, x1=0, y1=1;
long r, q;
*x=0;
*y=1;
r = a % b;
q = (a - r) / b;
while(r)
{
*x = x0 - q * x1;
*y = y0 - q * y1;
x0 = x1;
y0 = y1;
x1 = *x;
y1 = *y;
a = b;
b = r;
r = a % b;
q = (a - r) / b;
}
return b;
}
// 扩展欧几里德算法求逆元
long minv(long a, long p)
{
long x, y;
exgcd(a, p, &x, &y);
return x<0 ? x+p : x;
}
// 试探法求逆元
long minv2(long a, long p)
{
long y=1, t;
int i;
if(a < 0) {
a = a % p;
a += p;
}
for(i=1; i
程序运行结果:a=65 m=83 x=23 x=23
a=11663 m=103 x=73 x=73
a=11227 m=107 x=40 x=40
a=11021 m=103 x=100 x=100
x=23