/**
* 要么前进97个单位 要么后退127个单位
* 经过一系列的操作 要到达前面1个单位
* 求操作最少之和
*/
package 数学问题;
public class 欧几里得扩展小明自救 {
static long x;
static long y;
public static void main(String[] args) {
try {
linearequetion(97,-127,1); //因为a,b都是质数并且ax+by=1 所以一定有解
System.out.println(Math.abs(欧几里得扩展小明自救.x)+Math.abs(欧几里得扩展小明自救.y));
} catch (Exception e) {
System.out.println("无解");
e.printStackTrace();
}
}
private static long ext_gcd(long a,long b){
if(b==0){
x=1;
y=0;
return a;
}
long res=ext_gcd(b,a%b); //递归的一个过程 上一层的a=b,b=a%b;
long x1=x;
x=y;
y=x1-(a/b)*y;
return res;
}
private static long linearequetion(long a,long b,long m ) throws Exception{
long d=ext_gcd(a,b);
if(m%d!=0) throw new Exception("无解");
long n=m/d;
x*=n;
y*=n;
return d;
}
}