Modular arithmetic
 
Betöltés...
Keresés...
Nincs egyezés
mod.h
Ugrás a fájl dokumentációjához.
1
5#ifndef MOD_H
6#define MOD_H
7#include <iostream>
8using namespace std;
9
10namespace bme {
11
14 const int NORMAL_PRINT_MODE = 1;
15 const int SHORT_PRINT_MODE = 2;
18
23
24 class Mod {
25
26 private:
28 int mod;
30 int val;
32 static int default_modulus;
34
38 static int print_mode;
39
41
45 void adjust_val() {
46 val = val % mod;
47 if (val < 0) {
48 val += mod;
49 }
50 }
51
52 public:
53
55
58 Mod() : mod(get_default_modulus()), val(0) {}
59
61
65 Mod(int _val) : mod(get_default_modulus()), val(_val) { adjust_val(); }
66
68
73 Mod(int _val, int _mod){
74 if (_mod <= 0) {
75 val = 0;
76 mod = 0;
77 }
78 else {
79 mod = _mod;
80 val = _val;
81 adjust_val();
82 }
83 }
84
86 Mod(const Mod& other) : mod(other.mod), val(other.val) {}
87
89
92 int get_val()const { return val; }
93
95
100 void set_val(int _val) {
101 if (mod == 0) { return; } //invalid példányt nem változtatunk
102 val = _val;
103 adjust_val();
104 }
105
107 int get_mod()const { return mod; }
108
110
116 void set_mod(int _mod) {
117 if (_mod <= 0) {
118 mod = 0;
119 val = 0;
120 }
121 else {
122 mod = _mod;
123 adjust_val();
124 }
125 }
126
128
133 static void set_default_modulus(int _def_mod) {
134 default_modulus = _def_mod <= 0 ? INITIAL_DEFAULT_MODULUS : _def_mod;
135 }
136
138
142 static void set_normal_print_mode() {
143 print_mode = NORMAL_PRINT_MODE;
144 }
145
147
151 static void set_short_print_mode() {
152 print_mode = SHORT_PRINT_MODE;
153 }
154
155 static int get_print_mode() {
156 return print_mode;
157 }
158
160
165 static int get_default_modulus() {
166 if (default_modulus <= 0) {
168 }
169 return default_modulus;
170 }
171
173
176 bool is_invalid()const { return mod == 0; }
177
178 bool operator==(const Mod& other)const {
179 return ((val == other.val) && (mod == other.mod));
180 }
181
182 bool operator==(int other)const {
183 return (*this) == Mod(other, mod);
184 }
185
186 bool operator!=(const Mod& other)const {
187 return !((*this) == other);
188 }
189
190 bool operator!=(int other)const {
191 return (*this) != Mod(other, mod);
192 }
193
194 bool operator<(const Mod& other)const {
195 if (mod < other.mod) { return true; }
196 if (mod > other.mod) { return false; }
197 if (val < other.val) { return true; }
198 return false;
199 }
200
201 bool operator<=(const Mod& other)const {
202 return (*this) < other || (*this) == other;
203 }
204
205 bool operator>(const Mod& other) const {
206 return !((*this) <= other);
207 }
208
209 bool operator>=(const Mod& other) const {
210 return !((*this) < other);
211 }
212
213
214 Mod& operator=(const Mod& other) {
215 mod = other.mod;
216 val = other.val;
217 return *this;
218 }
219
221
226 Mod add(const Mod& other)const;
227
228 Mod operator+(const Mod& other)const { return add(other); }
229 Mod operator+(int other)const { return add(Mod(other,mod)); }
230 Mod& operator+=(const Mod& other) { return *this = add(other); }
231 Mod& operator+=(int other) { return *this = add(Mod(other, mod)); }
232 Mod operator-() const { return Mod(-val, mod); }
233 Mod operator-(const Mod& other)const { return (*this) + (-other); }
234 Mod operator-(int other)const { return (*this) + (-other); }
235 Mod& operator-=(const Mod& other) { return *this = add(-other); }
236 Mod& operator-=(int other) { return *this = add(Mod(-other, mod)); }
237
239
244 Mod multiply(const Mod& other)const;
245
246 Mod operator*(const Mod& other)const { return multiply(other); }
247 Mod operator*(int other)const { return multiply(Mod(other,mod)); }
248 Mod& operator*=(const Mod& other) { return *this = multiply(other); }
249 Mod& operator*=(int other) { return *this = multiply(Mod(other,mod)); }
250
252
256 Mod inverse()const;
257
258 Mod operator/(const Mod& other)const { return multiply(other.inverse()); }
259 Mod operator/(int other)const { return multiply(Mod(other,mod).inverse()); }
260 Mod& operator/=(const Mod& other) { return *this = multiply(other.inverse()); }
261 Mod& operator/=(int other) { return *this = multiply(Mod(other,mod).inverse()); }
262
264
268 Mod pow(int k)const;
269 };
270
272 ostream& operator<<(ostream& os, const Mod& M);
273
274 inline bool operator==(int x, const Mod& M) { return M == x; }
275 inline bool operator!=(int x, const Mod& M) { return M != x; }
276 inline Mod operator+(int x, const Mod& M) { return M + x; }
277 inline Mod operator-(int x, const Mod& M) { return (-M) + x; }
278 inline Mod operator*(int x, const Mod& M) { return M * x; }
279 inline Mod operator/(int x, const Mod& M) { return M.inverse() * x; }
280
281}
282
283#endif
Definition mod.h:24
Mod(int _val)
Kezdeti érték beállítása a default modulus mellett.
Definition mod.h:65
Mod operator-() const
Definition mod.h:232
Mod multiply(const Mod &other) const
A szorzást megvalósító függvény.
Definition mod.cpp:16
static void set_default_modulus(int _def_mod)
Default modulus beállítása.
Definition mod.h:133
int get_mod() const
Modulus lekérdezése.
Definition mod.h:107
Mod operator*(int other) const
Definition mod.h:247
void set_val(int _val)
Maradékosztály beállítása.
Definition mod.h:100
Mod & operator-=(int other)
Definition mod.h:236
Mod()
Default konstruktor.
Definition mod.h:58
static void set_short_print_mode()
Kiírás módjának beállítása.
Definition mod.h:151
bool operator<=(const Mod &other) const
Definition mod.h:201
Mod & operator*=(int other)
Definition mod.h:249
static void set_normal_print_mode()
Kiírás módjának beállítása.
Definition mod.h:142
Mod operator-(int other) const
Definition mod.h:234
Mod & operator-=(const Mod &other)
Definition mod.h:235
bool operator==(int other) const
Definition mod.h:182
Mod inverse() const
Az inverzet számoló függvény.
Definition mod.cpp:24
Mod operator+(const Mod &other) const
Definition mod.h:228
void set_mod(int _mod)
Modulus beállítása.
Definition mod.h:116
bool operator>(const Mod &other) const
Definition mod.h:205
Mod(const Mod &other)
Másoló konstruktor.
Definition mod.h:86
int get_val() const
Maradékosztály reprezentánsa.
Definition mod.h:92
bool is_invalid() const
Invalid objektum lekérdezése.
Definition mod.h:176
bool operator!=(const Mod &other) const
Definition mod.h:186
Mod & operator+=(const Mod &other)
Definition mod.h:230
Mod(int _val, int _mod)
Kezdeti érték beállítása a default modulus mellett.
Definition mod.h:73
static int get_print_mode()
Kiírás módja.
Definition mod.h:155
Mod operator/(int other) const
Definition mod.h:259
bool operator<(const Mod &other) const
Definition mod.h:194
Mod operator/(const Mod &other) const
Definition mod.h:258
Mod operator*(const Mod &other) const
Definition mod.h:246
Mod pow(int k) const
Hatványozás.
Definition mod.cpp:35
static int get_default_modulus()
Default modulus lekérdezése.
Definition mod.h:165
Mod add(const Mod &other) const
Az összeadást megvalósító függvény.
Definition mod.cpp:9
Mod operator+(int other) const
Definition mod.h:229
bool operator!=(int other) const
Definition mod.h:190
Mod & operator/=(const Mod &other)
Definition mod.h:260
Mod operator-(const Mod &other) const
Definition mod.h:233
Mod & operator=(const Mod &other)
Definition mod.h:214
Mod & operator/=(int other)
Definition mod.h:261
Mod & operator+=(int other)
Definition mod.h:231
bool operator==(const Mod &other) const
Definition mod.h:178
Mod & operator*=(const Mod &other)
Definition mod.h:248
bool operator>=(const Mod &other) const
Definition mod.h:209
Definition gcdx.cpp:5
ostream & operator<<(ostream &os, const Mod &M)
Mod osztály kiírása a képernyőre.
Definition mod.cpp:61
const int NORMAL_PRINT_MODE
Definition mod.h:14
const int SHORT_PRINT_MODE
Definition mod.h:15
Mod operator/(int x, const Mod &M)
Definition mod.h:279
Mod operator*(int x, const Mod &M)
Definition mod.h:278
bool operator==(int x, const Mod &M)
Definition mod.h:274
Mod operator-(int x, const Mod &M)
Definition mod.h:277
const int INITIAL_DEFAULT_MODULUS
a default modulus kezdeti értéke
Definition mod.h:13
bool operator!=(int x, const Mod &M)
Definition mod.h:275
const int INITIAL_PRINT_MODE
a print mode kezdeti értéke
Definition mod.h:17
Mod operator+(int x, const Mod &M)
Definition mod.h:276