扩展欧几里得算法(求最大公约数与逆)

#include 
class GCD{
    public:
       long long a1,a2;
       long long Gcd;
       long long u,v;
      // Gcd=this->gcd(a1,a2);
    public:
        /*long gcd(long a1,long a2){
            if(a2==0){
                return a1;
            }
            else{
                return gcd(a2,a1%a2);
            }
        }*/
        GCD(long long i,long long j){
            a1=i>=j?i:j;
            a2=i+j-a1;;
        }
        void Euclid_extend(){
            long long U_temp[100],V_temp[100];
            long long b[100];
            int i;
            b[0]=(a1>=a2?a1:a2);
            b[1]=a1+a2-b[0];
            U_temp[0]=0;
            V_temp[0]=1;
            U_temp[1]=1;
            if(b[1]==0){
                u=1;
                v=0;
                Gcd=u*b[0]+v*b[1];
                return;
            }
            V_temp[1]=-(b[0]/b[1]);
            if(b[0]%b[1]==0){
                u=0;
                v=1;
                Gcd=u*b[0]+v*b[1];
                return;
            }
            else{
                b[2]=b[0]%b[1];
                i=1;
                if(b[1]%b[2]==0){
                    u=1;
                    v=V_temp[1];
                    Gcd=u*b[0]+v*b[1];
                    return;
                }
                else{
                    Euclid_extend_compute(U_temp,V_temp,i,b);
                    u=U_temp[i];
                    v=V_temp[i];
                    Gcd=u*b[0]+v*b[1];
                    return;
                }
            }
        }
        void Euclid_extend_compute(long long U_temp[],long long V_temp[], int &i,long long b[]){
            long long q;
            q=b[i]/b[i+1];
            b[i+2]=b[i]%b[i+1];
            if(b[i+2]==0){
                return;
            }
            else{
                 U_temp[i+1]=U_temp[i-1]-q*U_temp[i];
                 V_temp[i+1]=V_temp[i-1]-q*V_temp[i];
                 i++;
                 Euclid_extend_compute( U_temp, V_temp, i, b);
                 return;

            }
        }

};
using namespace std;

int main()
{
    long long i,j;
    cin>>i>>j;
    GCD gcd_compute(i,j);
    //gcd_compute.Gcd=gcd_compute.gcd(i,j);
    //cout<

你可能感兴趣的:(扩展欧几里得算法(求最大公约数与逆))