标签:ref \n using print inverse main 欧几里得 return 链接
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712
对于ax+by=1; 即ax=1(mod b) 当且仅当gcd(a,b)!=1 的时候,无解!
AC代码:
#include <iostream>
#include <cstdio>
using namespace std;
int test;
int a, b;
//拓展欧几里得模板
int extgcd(int a, int b, int &x, int &y) {
int ans, temp;
if(b == 0) {
x = 1;
y = 0;
return a;
}
ans = extgcd(b, a % b, x, y);
temp = x;
x = y;
y = temp - a / b * y;
return ans;
}
int main() {
scanf("%d", &test);
int x, y;
while(test--) {
scanf("%d%d", &a, &b);
int ans = extgcd(a, b, x, y);
if(ans != 1)
printf("Not Exist\n");
else {
x %= b;
if(x <= 0) //要求最小正整数,所以 x==0 也要考虑
x += b;
printf("%d\n", x);
}
}
return 0;
}
标签:ref \n using print inverse main 欧几里得 return 链接
原文地址:https://www.cnblogs.com/youpeng/p/10807450.html