-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d17fdcb
commit ade1477
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int gcd(int a, int b) {return b==0?a:gcd(b,a%b);} | ||
|
||
int solve(int a, int b){ | ||
|
||
int value = 1, x = 0; | ||
while(value < max(a,b)) { | ||
if(a%value == 0 and gcd(value,b)==1) | ||
x = value; | ||
value++; | ||
} | ||
|
||
return x; | ||
} | ||
|
||
int main(){ | ||
|
||
int a,b; cin >> a >> b; | ||
cout << solve(a,b) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* | ||
find the values of x and y thats | ||
A * x + B * y = gcd(A,B) | ||
where A and B are inputs | ||
*/ | ||
int gcd(int a, int b) { | ||
return b==0?a:gcd(b,a%b); | ||
} | ||
|
||
pair<int,int> extendedGCD(int a, int b) { | ||
|
||
// base case | ||
if(b==0) { | ||
// returning the values of x and y | ||
return {1,0}; | ||
} | ||
|
||
pair<int,int> result = extendedGCD(b,a%b); | ||
int smallX = result.first; | ||
int smallY = result.second; | ||
|
||
int x = smallY; | ||
int y = smallX - floor(a/b)*smallY; | ||
|
||
return {x,y}; | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
|
||
int a,b; cin >> a >> b; | ||
// a x + b y = gcd(a,b); | ||
pair<int,int> ans = extendedGCD(a,b); | ||
cout << ans.first << " " << ans.second << endl; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int gcd(int a, int b) { | ||
|
||
if(b == 0) | ||
return a; | ||
|
||
return gcd(b,a%b); | ||
|
||
} | ||
|
||
int main(){ | ||
|
||
int a,b; cin >> a >> b; | ||
cout << gcd(a,b) << endl; | ||
|
||
return 0; | ||
} |