-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModInt.cpp
92 lines (85 loc) · 2.13 KB
/
ModInt.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdint>
// MODを扱うクラス
// ここを参考にした->https://noshi91.hatenablog.com/entry/2019/03/31/174006
// #include <cstdint>が必要
template <std::uint_fast64_t Modulus>
class modint
{
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} //コンストラクタ
constexpr u64 &value() noexcept { return a; } //値を返す
constexpr const u64 &value() const noexcept { return a; } //値を返す
constexpr modint operator+(const modint rhs) const noexcept
{
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept
{
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept
{
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept
{
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept
{
a += rhs.a;
if (a >= Modulus)
{
a -= Modulus; //MODを超えたので調節
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept
{
if (a < rhs.a)
{
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept
{
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(const modint rhs) noexcept
{
u64 exp = Modulus - 2;
while (exp)
{
if (exp % 2)
{
*this = *rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
constexpr bool operator==(const modint rhs) const noexcept
{
if (a == rhs.a)
{
return true;
}
return false;
}
};
//使用例
int main(void)
{
const int MOD = 1000000007;
modint<MOD> a, b, c, d;
std::cin >> a.a >> b.a >> c.a >> d.a;
std::cout << (a * b + c).a << std::endl;
return 0;
}