Mdular Linear Equatin 模线性方程(同余方程)

来源:互联网 发布:游戏程序员招聘 编辑:程序博客网 时间:2024/05/24 07:27
//如果GCD(a,b)不能整除c,则ax + by = c 没有整数解//ax≡b(mod n)  (n>0)//上式等价于二元一次方程ax - ny = bvoid 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 solutionint 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;}

原创粉丝点击