题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712
1. 这题数据范围太小,直接暴力即可
2. 不过其实这题也是很“直白的”求乘法逆的题目,即当b=1的特殊的模线性方程问题ax≡b mod(n),可以通过扩展欧几里得算法求解:
ax≡b mod(n)
=> (ax) mod n = b mod n
=> ax=k1*n+r ... (1)
b=k2n+r ... (2)
(1)-(2)=>ax-b=(k1-k2)n
记y=(k2-k1)=>ax+ny=b
转化为贝祖等式,这一经典的扩展欧几里得算法了。
具体解法见我之前的博客,传送门:http://www.cnblogs.com/AcIsFun/p/5393550.html
#include <cstdio> #include <iostream> using namespace std; int exgcd(int a, int b, int &x, int &y) { if(b==0) { x = 1; y = 0; return a; } int r = exgcd(b, a%b, x, y); int t = x; x = y; y = t - a/b*y; return r; } bool f(int a, int b, int c, int &x, int &y) { int d = exgcd(a, b, x, y); if(c%d) return 0; int k = c/d; x *= k; y *= k; return 1; } int main () { int T, a, n, x, y; scanf("%d", &T); while(T--) { scanf("%d%d", &a, &n); if(f(a, n, 1, x, y)) { if(x>0) { x = x%n; } else if(x < 0) { // 这里基于 x+k*n>0 => k=(-x)/n+1 x = (x+((-x)%n+1)*n)%n; } else x++; printf("%d\n", x); } else { printf("Not Exist\n"); } } return 0; }
时间: 2024-12-25 12:23:10