-
Notifications
You must be signed in to change notification settings - Fork 6
/
math.h
110 lines (100 loc) · 2.44 KB
/
math.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Ilia Mirkin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
template <typename T>
static T abs(T x) {
if (x >= 0) return x;
return -x;
}
template <typename T>
static T min(T x, T y) {
if (x < y) return x;
return y;
}
template <typename T>
static T max(T x, T y) {
if (x > y) return x;
return y;
}
template <typename T>
static T gcd(T x, T y) {
x = abs(x);
y = abs(y);
T a = min(x, y);
T b = max(x, y);
while (a != 0) {
T nexta = b % a;
T nextb = a;
a = nexta;
b = nextb;
}
return b;
}
template <typename T>
static T mod(T a, T b) {
while (a < 0) a += b;
return a % b;
}
template <typename T>
static void fixSign(T* vals) {
if (vals[0] < 0) {
vals[0] *= -1;
vals[1] *= -1;
vals[2] *= -1;
}
}
/**
* Returns an array A of length 3. A[0] is the GCD(x,y), and A[0] == A[1] * x
* + A[2] * y.
*/
template <typename T>
static void extendedGcd(T x, T y, T* ret) {
if (x > y) {
extendedGcd(y, x, ret);
T tmp = ret[1];
ret[1] = ret[2];
ret[2] = tmp;
fixSign(ret);
return;
}
if (x == 0) {
ret[0] = y;
ret[1] = 0;
ret[2] = 1;
fixSign(ret);
return;
} else if (y == 0) {
ret[0] = x;
ret[1] = 1;
ret[2] = 0;
fixSign(ret);
return;
}
extendedGcd(mod(y, x), x, ret);
T k = y / x;
T tmp = ret[2] - k * ret[1];
ret[2] = ret[1];
ret[1] = tmp;
fixSign(ret);
}