拓展欧几里得的模板题(因为他的b不一定是质数所以不能用费马小定理来解决)
证明拓展欧几里得的式子
p∗a+q∗b=GCD(a,b)=GCD(b,amodb)=p∗b+q∗(amodb)=
p∗b+q∗(a−(a/b)∗b)=q∗a+(p−(a/b)∗q)∗b
#include <cstdio>
using namespace std;
int a, b, x, y, d;
int read() {
int f = 1, k = 0;
char c = getchar();
while(c < '0' || c > '9') {
if( c == '-') {
f = -1;
}
c = getchar();
}
while(c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
int exgcd(int a, int b, int &d, int &x, int &y) {
if(!b) {
d = a;
x = 1;
y = 0;
} else {
exgcd(b, a % b, d, x, y) ;
int x1 = x;
x = y;
y = x1 - (a / b) * y;
}
}
int main() {
a = read(), b = read();
exgcd(a, b, d, x, y);
printf("%d", (x + b) % b);
return 0;
}