-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZNumber.cpp
257 lines (183 loc) · 4.67 KB
/
ZNumber.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdexcept>
using namespace std;
#include "ZNumber.hpp"
ZNumber ZNumber::operator+(const ZNumber &zn) const{
uint64_t res = static_cast<uint64_t>(this->getValue()) + zn.getValue();
if(res >= module)
res %= module;
return res;
}
ZNumber ZNumber::operator-(const ZNumber &zn) const{
return *this + (-zn);
}
ZNumber ZNumber::operator*(const ZNumber &zn) const{
uint64_t res = static_cast<uint64_t>(this->getValue()) * zn.getValue();
if(res >= module)
res %= module;
return res;
}
inline ZNumber ZNumber::getInverse() const{
if(this->n == 0)
throw logic_error("0 is not invertible");
int64_t t = 0, new_t = 1;
int64_t r = module, new_r = this->getValue();
register int64_t temp;
register int64_t quotient;
while(new_r != 0){
quotient = r / new_r;
temp = t;
t = new_t;
new_t = temp - quotient * new_t;
temp = r;
r = new_r;
new_r = temp - quotient * new_r;
}
if(r > 1)
throw logic_error("N is not invertible");
return ZNumber(t);
}
ZNumber ZNumber::operator/(const ZNumber &zn) const{
return *this * zn.getInverse();
}
ZNumber ZNumber::operator%(const ZNumber &zn) const{
return this->getValue() % zn.getValue();
}
ZNumber ZNumber::operator^(const ZNumber &zn) const{
return this->getValue() ^ zn.getValue();
}
const ZNumber &ZNumber::operator=(const ZNumber &zn){
this->n = zn.getValue();
return *this;
}
const ZNumber &ZNumber::operator+=(const ZNumber &zn){
*this = *this + zn;
return *this;
}
const ZNumber &ZNumber::operator-=(const ZNumber &zn){
*this = *this - zn;
return *this;
}
const ZNumber &ZNumber::operator*=(const ZNumber &zn){
*this = *this * zn;
return *this;
}
const ZNumber &ZNumber::operator/=(const ZNumber &zn){
*this = *this / zn;
return *this;
}
const ZNumber &ZNumber::operator%=(const ZNumber &zn){
*this = *this % zn;
return *this;
}
const ZNumber &ZNumber::operator^=(const ZNumber &zn){
*this = *this ^ zn;
return *this;
}
ZNumber &ZNumber::operator++(){
*this += 1;
return *this;
}
ZNumber ZNumber::operator++(int){
ZNumber copy(*this);
*this += 1;
return copy;
}
ZNumber &ZNumber::operator--(){
*this -= 1;
return *this;
}
ZNumber ZNumber::operator--(int){
ZNumber copy(*this);
*this -= 1;
return copy;
}
bool ZNumber::operator==(const ZNumber &zn) const{
return this->getValue() == zn.getValue();
}
bool ZNumber::operator!=(const ZNumber &zn) const{
return !(*this == zn);
}
bool ZNumber::operator<(const ZNumber &zn) const{
return this->getValue() < zn.getValue();
}
bool ZNumber::operator>(const ZNumber &zn) const{
return this->getValue() > zn.getValue();
}
bool ZNumber::operator<=(const ZNumber &zn) const{
return !(*this > zn);
}
bool ZNumber::operator>=(const ZNumber &zn) const{
return !(*this < zn);
}
bool ZNumber::operator!() const{
return !this->getValue();
}
bool ZNumber::operator&&(const ZNumber &zn) const{
return this->getValue() && zn.getValue();
}
bool ZNumber::operator||(const ZNumber &zn) const{
return this->getValue() || zn.getValue();
}
ZNumber ZNumber::operator-() const{
int64_t n_ = this->getValue();
return ZNumber(-n_);
}
string ZNumber::toString() const{
string res;
res = to_string(this->getValue());
return res;
}
ostream &operator<<(ostream &o, const vector<ZNumber> &v){
for(const auto &n : v)
o << n.toString() << " ";
return o;
}
vector<ZNumber> operator+(const vector<ZNumber> &v1, const vector<ZNumber> &v2){
if(v1.size() != v2.size())
throw logic_error("Vector size does not match");
vector<ZNumber> res(v1.size());
for(uint16_t i = 0; i < res.size(); ++i)
res.at(i) = v1.at(i) + v2.at(i);
return res;
}
vector<ZNumber> operator-(const vector<ZNumber> &v2){
vector<ZNumber> result(v2.size());
for(size_t i = 0; i < v2.size(); ++i)
result.at(i) = -v2.at(i);
return result;
}
vector<ZNumber> operator-(const vector<ZNumber> &v1, const vector<ZNumber> &v2){
return v1 + (-v2);
}
const vector<ZNumber> &operator+=(vector<ZNumber> &v1, const vector<ZNumber> &v2){
v1 = move(v1 + v2);
return v1;
}
vector<ZNumber> operator*(const vector<ZNumber> &v1, const vector<ZNumber> &v2){
if(v1.size() != v2.size())
throw logic_error("Vector size does not match");
vector<ZNumber> res(v1.size());
for(uint16_t i = 0; i < res.size(); ++i)
res.at(i) = v1.at(i) * v2.at(i);
return res;
}
vector<ZNumber> operator*(vector<ZNumber> v, const ZNumber &zn){
for(auto &n : v)
n *= zn;
return v;
}
vector<ZNumber> operator/(vector<ZNumber> v, const ZNumber &zn){
for(auto &n : v)
n /= zn;
return v;
}
const vector<ZNumber> &operator-=(vector<ZNumber> &zv1, const vector<ZNumber> &zv2){
zv1 = move(zv1 - zv2);
return zv1;
}
ZNumber sum(const vector<ZNumber> &v){
ZNumber res = 0;
for(auto &value : v)
res = res + value;
return res;
}