链接:点击打开链接
题意: 给定两个数a,b
求ax+by=1得方程的解 拓展欧几里得的模板应用
求ax0+by0=gcd(a,b) 此题若gcd(a,b)!=1输出soory 求出x0,y0 正常需要乘gcd(a,b) 但gcd=1 直接输出即可 输出x为非负整数, 所以我们输出前用通解处理一下 x = x0+t*b ; y = y0-t*a;
import java.util.Scanner;
public class Main {
public static long[] ec_gcd(long a, long b) {
long ans;
long result[] = new long[3];
if (b == 0) {
result[0] = a;
result[1] = 1;
result[2] = 0;
return result;
}
long temp[] = ec_gcd(b, a % b);
ans = temp[0];
result[0] = ans;
result[1] = temp[2];
result[2] = temp[1] - (a / b) * temp[2];
return result;
}
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
long a = scanner.nextLong();
long b = scanner.nextLong();
long result[] = ec_gcd(a, b);
if (result[0] != 1) {
System.out.println("sorry");
} else {
while (result[1] < 0) {
result[1] = result[1] + b;
result[2] = result[2] - a;
}
System.out.println(result[1] / result[0] + " " + result[2] / result[0]);
}
}
}
}