mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 14:18:47 +00:00
878. 线性同余方程
https://www.acwing.com/problem/content/submission/code_detail/14083066/
This commit is contained in:
parent
7ee05130ad
commit
7f2397c665
40
AcWing/878/878.cpp
Normal file
40
AcWing/878/878.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
int n, a, b, m, x, y;
|
||||
|
||||
int exgcd(int a, int b, int &x, int &y) {
|
||||
if (!b) {
|
||||
x = 1, y = 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
int d = exgcd(b, a % b, y, x);
|
||||
|
||||
y -= a / b * x;
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
|
||||
cin >> n;
|
||||
|
||||
while (n--) {
|
||||
cin >> a >> b >> m;
|
||||
|
||||
int d = exgcd(a, m, x, y);
|
||||
|
||||
if (b % d) {
|
||||
cout << "impossible" << endl;
|
||||
} else {
|
||||
cout << 1ll * b / d * x % m << endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user