您的位置:首页 > 其它

Modular Inverse ZOJ - 3609

2017-09-22 10:25 387 查看
题目传送门

题意:ax≡1 (mod m) 求x

思路:就是一个裸的扩展欧几里得,但是要注意一下m为1的情况

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define MAXN 20100
#define MAXE 5
#define INF 1000000000
#define MOD 9901
#define LL long long
#define pi acos(-1.0)

using namespace std;

void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
exgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}

int main() {
std::ios::sync_with_stdio(false);
int T;
cin >> T;
LL a, m;
for (int kase = 1; kase <= T; ++kase) {
cin >> a >> m;
if (m == 1) {
cout << 1 << endl;
} else {
LL d, x, y;
exgcd(a, m, d, x, y);
if (d != 1) {
cout << "Not Exist\n";
} else {
cout << (x + m) % m << endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: