数论知识好多都忘记了,今天周末没啥事就复习一下
用扩展欧几里得算法
取地址不是很熟悉就用全局变量吧
#include<bits/stdc++.h>
using namespace std;
long long x, y;
void exgcd(long long a, long long b)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
exgcd(b, a % b);
long long tx = x;
x = y;
y = tx - a / b * y;
}
int main()
{
long long a, b;
cin >> a >> b;
exgcd(a, b);
x = (x % b + b) % b;
printf("%lld\n", x);
return 0;
}