问题描述
不应使用类 BigInteger 的 modInverse 方法。我尝试并使用了这种方法,但它不会产生正确的结果。任何帮助将不胜感激。
public static int[] extendedgcd(int a,int b) {
if (b==0) {
return new int[]{1,a};
} else {
int output[] = extendedgcd(b,a%b);
return new int[]{output[0],output[0]-((a/b)*output[1]),output[2]};
}
}
解决方法
在 else 分支中,第一个返回值应该是 output[1]
而不是 output[0]
:
public static int[] extendedgcd(int a,int b) {
if (b == 0) {
return new int[]{1,a};
} else {
int output[] = extendedgcd(b,a % b);
return new int[]{
output[1],output[0] - ((a / b) * output[1]),output[2]
};
}
}
您可以在此处测试实现:https://onlinegdb.com/volwDTwnl