java实现 拓展欧几里得算法 exgcd

返回的数组中,第一个值是最大公约数,第二个值表示C++语言实现中的x,第三个值表示y。

存在整数对 x,y ,使得 gcd(a,b)=ax+by

public  static long[] ex_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=ex_gcd(b,a%b);  
        ans = temp[0];  
        result[0]=ans;  
        result[1]=temp[2];  
        result[2]=temp[1]-(a/b)*temp[2];  
        return result;  
    }

猜你喜欢

转载自blog.csdn.net/qq_35821988/article/details/78885980