-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.hpp
122 lines (100 loc) · 2.38 KB
/
gcd.hpp
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
111
112
113
114
115
116
117
118
119
120
121
122
// algocpp/math/gcd.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_MATH_GCD
#define ALGOCPP_MATH_GCD
#include <stdexcept>
#include <vector>
#include <array>
#if !defined(ALGOCPP_NO_LIB) && __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#endif
#if !defined(ALGOCPP_NO_LIB) && __has_include(<boost/array.hpp>)
#include <boost/array.hpp>
#endif
namespace algocpp
{
namespace math
{
#ifdef BOOST_MP_CPP_INT_HPP
using max_integer = boost::multiprecision::cpp_int;
#else
using max_integer = unsigned long long;
#endif
namespace base
{
inline long long base_gcd(long long a, long long b)
{
if (a < 0 || b < 0)
{
throw std::invalid_argument("Any value must be a positive integer.");
}
return (b == 0 ? a : base_gcd(b, a % b));
}
inline max_integer base_gcd(max_integer a, max_integer b)
{
#ifdef BOOST_MP_CPP_INT_HPP
if (a < 0 || b < 0)
{
throw std::invalid_argument("Any value must be a positive integer.");
}
#endif
return (b == 0 ? a : base_gcd(b, a % b));
}
}
inline long long gcd(long long a, long long b)
{
return base::base_gcd(a, b);
}
inline max_integer gcd(max_integer a, max_integer b)
{
return base::base_gcd(a, b);
}
template <typename T>
inline T gcd(std::vector<T> v)
{
if (v.size() < 2)
{
throw std::invalid_argument("The length of the argument must be at least 2.");
}
T result = gcd(v[0], v[1]);
for (unsigned long long i = 2; i < v.size(); ++i)
{
result = gcd(result, v[i]);
}
return result;
}
template <typename T, std::size_t N>
inline T gcd(std::array<T, N> v)
{
if (v.size() < 2)
{
throw std::invalid_argument("The length of the argument must be at least 2.");
}
T result = gcd(v[0], v[1]);
for (unsigned long long i = 2; i < v.size(); ++i)
{
result = gcd(result, v[i]);
}
return result;
}
#ifdef BOOST_ARRAY_HPP
template <typename T, std::size_t N>
inline T gcd(boost::array<T, N> v)
{
if (v.size() < 2)
{
throw std::invalid_argument("The length of the argument must be at least 2.");
}
T result = gcd(v[0], v[1]);
for (unsigned long long i = 2; i < v.size(); ++i)
{
result = gcd(result, v[i]);
}
return result;
}
#endif
}
}
#endif // ALGOCPP_MATH_GCD