"vscode:/vscode.git/clone" did not exist on "013f0651f934b3b4120b5169c65aa8998723b6f8"
online_driver_common.hpp 726 Bytes
Newer Older
1
2
#ifndef ONLINE_DRIVER_COMMON_HPP
#define ONLINE_DRIVER_COMMON_HPP
3

4
namespace ck_driver {
5

6
7
// greatest common divisor, aka highest common factor
inline int gcd(int x, int y)
8
{
9
    if(x < 0)
10
    {
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        return gcd(-x, y);
    }
    else if(y < 0)
    {
        return gcd(x, -y);
    }
    else if(x == y || x == 0)
    {
        return y;
    }
    else if(y == 0)
    {
        return x;
    }
    else if(x > y)
    {
        return gcd(x % y, y);
    }
29
30
    else
    {
31
32
33
        return gcd(x, y % x);
    }
}
34

35
36
37
38
39
40
41
template <typename X,
          typename... Ys,
          typename std::enable_if<sizeof...(Ys) >= 2, bool>::type = false>
auto gcd(X x, Ys... ys)
{
    return gcd(x, gcd(ys...));
}
42

43
} // namespace ck_driver
44
#endif