一.题目链接
二.思路
直接将线性同余方程转化即可。
三.代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int exgcd(int a, int b, int &x, int &y)
{
//边界情况
if(b == 0)
{
x = 1;
y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);//交换x, y的位置
y -= a / b * x;
}
int main()
{
int a, b, x, y;
cin >> a >> b;
exgcd(a, b, x, y);//引用必须要传入一个变量。
cout << (x % b + b) % b << endl;
return 0;
}
四.总结
- 扩展欧几里得证明参考:https://www.acwing.com/solution/content/25756/
- 通解情况: