-
Notifications
You must be signed in to change notification settings - Fork 0
/
EXTENDEDGCD.C
68 lines (54 loc) · 1.03 KB
/
EXTENDEDGCD.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// C program to demonstrate working of extended
// Euclidean Algorithm
#include <stdio.h>
void read(int &a)
{
scanf("%d",&a);
}
void read(int &a,int &b)
{
scanf("%d %d",&a,&b);
}
//r extended Euclidean Algorithm
int gcdExtended(int a, int b, int &x, int &y)
{
// Base Case
if (a == 0)
{
x = 0;
y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, x1, y1);
// Update x and y using results of recursive
// call
x = y1 - (b/a) * x1;
y = x1;
return gcd;
}
// Driver Program
int main()
{
ios;
ll a,b,c,d;
ll k,l,m,n,p,q,r,s,t,x,y;
ll A[100001],B[100001];
fact[0]=1;
fr1(i,1000001)
{
fact[i]=(i*fact[i-1])%MOD;
}
tt(t)
{
read(n,c);
if(fact[n-1]%n == n-1)
a= n+1;
else a= n ;
if(fact[n-2]%(n-1) == n-2) b= n+1;
else b =n ;
int g = gcdExtended(a, b, x, y);
printf("gcd(%d, %d) = %d,%d,%d", a, b, g,x,y);
}
return 0;
}