Skip to content

Instantly share code, notes, and snippets.

@latte0119
Last active November 20, 2019 11:30
Show Gist options
  • Save latte0119/c967eed6ee1718d2d60ea0fd143c10e0 to your computer and use it in GitHub Desktop.
Save latte0119/c967eed6ee1718d2d60ea0fd143c10e0 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
int extgcd(int a,int b,int &x,int &y){
if(b==0){ //a*1+b*0=a なので!
x=1;
y=0;
return a;
}
int g=extgcd(b,a%b,x,y);
int next_x,next_y;
next_x=y;
next_y=x-(a/b)*y;
x=next_x;
y=next_y;
return g;
}
int main(){
int a=12;
int b=18;
int x,y;
int g=extgcd(a,b,x,y);
cout<<g<<endl; //6
cout<<x<<" "<<y<<endl; //-1 1
cout<<a*x+b*y<<endl; //6
}
int extgcd(int a,int b,int &x,int &y){
if(b==0){ //a*1+b*0=a なので!
x=1;
y=0;
return a;
}
int g=extgcd(b,a%b,x,y);
swap(x,y);
int next_x,next_y;
next_x=x;
next_y=y-(a/b)*x;
x=next_x;
y=next_y;
return g;
}
int extgcd(int a,int b,int &x,int &y){
if(b==0){ //a*1+b*0=a なので!
x=1;
y=0;
return a;
}
int g=extgcd(b,a%b,x,y);
swap(x,y);
//next_x=x;
//next_y=y-(a/b)*x;
y-=(a/b)*x;
return g;
}
int extgcd(int a,int b,int &x,int &y){
if(b==0){ //a*1+b*0=a なので!
x=1;
y=0;
return a;
}
//int g=extgcd(b,a%b,x,y);
//swap(x,y);
int g=extgcd(b,a%b,y,x);
//next_x=x;
//next_y=y-(a/b)*x;
y-=(a/b)*x;
return g;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment