-
Notifications
You must be signed in to change notification settings - Fork 10
/
diophantine.cpp
72 lines (68 loc) · 1.47 KB
/
diophantine.cpp
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
69
70
71
72
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm> // binsearch
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string.h> // memset
#include <cstdlib> // abs(int),labs(int),llabs(int),min,max
#include <limits.h> // int_max,int_min,long_long_max,long_long_min
#include <tr1/unordered_map>
#include <functional> // for the default hash functions. There exists a class Hash inside it.
using namespace std;
#define read(x) cin>>x
#define EPS 0.0000001
typedef long long LL;
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
vector<int> diophantiine(int a,int b)
{ int c = gcd(a,b);
bool swapped = false;
int xCurrent,yCurrent;
if(a>b)
{
int temp = a;
a = b;
b = temp;
swapped = true;
}
int xPrev = 0,yPrev = 1;
int cPrev= b;
xCurrent = 1;
yCurrent = 0;
int cCurrent = a;
while(cCurrent!=c)
{
int temp = cCurrent;
int factor;
factor = cPrev/cCurrent;
cCurrent = cPrev%cCurrent;
int tempX = xCurrent,tempY = yCurrent;
xCurrent = xPrev - factor*(xCurrent);
yCurrent = yPrev - factor*(yCurrent);
cCurrent = xCurrent*a + yCurrent*b;
xPrev = tempX;
yPrev = tempY;
cPrev = temp;
}
vector<int> ret;
ret.push_back(xCurrent);
ret.push_back(yCurrent);
if(swapped)
{
return vector<int>(ret.rbegin(),ret.rend());
}
return ret;
}
int main()
{ int a,b;
scanf("%d %d",&a,&b);
vector<int> test = diophantiine(a,b);
for(int i=0;i<test.size();i++) printf("%d\t",test[i]);
return 0;
}