//如果GCD(a,b)不能整除c,则ax + by = c 没有整数解
//ax≡b(mod n) (n>0)
//上式等价于二元一次方程ax - ny = b
void modular_linear_equation(int a, int b, int n)
{
int d, x, y, x0, gcd;
//可以减少扩展欧几里得溢出的可能
gcd = GCD(a, n);
if (b%gcd != 0) {
cout << "no solution" << endl;
return;
}
a /= gcd;
b /= gcd;
n /= gcd;
d = extended_euclid(a, n, x, y);
if (b%d == 0) {
x0 = (x*(b / d)) % n;//x0:basic solution
int ans = n;//min x = (x0%(n/d)+(n/d))%(n/d)
for (int i = 0; i < d; i++) {
ans = (x0 + i*(n / d)) % n;
cout << ans << endl;
}
}
else cout << "no solution" << endl;
}
Mdular Linear Equatin 模线性方程(同余方程)
最新推荐文章于 2024-07-15 22:10:26 发布