Skip to content

Commit 464bfe7

Browse files
committed
Updated modulararithmetic with necessary functions
1 parent e165e20 commit 464bfe7

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

content/number-theory/ModularArithmetic.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@
88
*/
99
#pragma once
1010

11-
#include "euclid.h"
12-
1311
const ll mod = 17; // change to something else
1412
struct Mod {
15-
ll x;
16-
Mod():x(0) {}
17-
Mod(ll xx) : x(xx) {}
18-
Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
19-
Mod operator-(Mod b) { return Mod((x - b.x + mod) % mod); }
20-
Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
21-
Mod operator/(Mod b) { return *this * invert(b); }
22-
Mod invert(Mod a) {
23-
ll x, y, g = euclid(a.x, mod, x, y);
24-
assert(g == 1); return Mod((x + mod) % mod);
25-
}
13+
ll v;
14+
Mod() : v(0) {}
15+
Mod(ll vv) : v(vv % mod) {}
16+
Mod operator+(Mod b) { return Mod((v + b.v) % mod); }
17+
Mod operator-(Mod b) { return Mod(v - b.v + mod); }
18+
Mod operator*(Mod b) { return Mod(v * b.v); }
19+
Mod operator/(Mod b) { return *this * invert(b); }
20+
Mod invert(Mod a) { return a^(mod-2); }
2621
Mod operator^(ll e) {
27-
if (!e) return Mod(1);
28-
Mod r = *this ^ (e / 2); r = r * r;
29-
return e&1 ? *this * r : r;
22+
ll ans = 1, b = (*this).v;
23+
for (; e; b = b * b % mod, e /= 2)
24+
if (e & 1) ans = ans * b % mod;
25+
return ans;
3026
}
31-
explicit operator ll() const { return x; }
32-
};
27+
explicit operator ll() const { return v; }
28+
};

content/numerical/PolyBase.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "../number-theory/ModularArithmetic.h"
99
#include "FastFourierTransform.h"
1010
#include "FastFourierTransformMod.h"
11-
// #include "NumberTheoreticTransform.h"
11+
#include "NumberTheoreticTransform.h"
1212

1313
typedef Mod num;
1414
typedef vector<num> poly;
@@ -30,8 +30,8 @@ poly &operator*=(poly &a, const poly &b) {
3030
res[i + j] = (res[i + j] + a[i] * b[j]);
3131
return (a = res);
3232
}
33-
auto res = convMod<mod>(vl(all(a)), vl(all(b)));
34-
// auto res = conv(vl(all(a)), vl(all(b)));
33+
// auto res = convMod<mod>(vl(all(a)), vl(all(b)));
34+
auto res = conv(vl(all(a)), vl(all(b)));
3535
return (a = poly(all(res)));
3636
}
3737
poly operator*(poly a, const num b) {

content/numerical/PolyPow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
poly pow(poly a, ll m) {
1212
int p = 0, n = sz(a);
13-
while (p < sz(a) && a[p].x == 0)
13+
while (p < sz(a) && a[p].v == 0)
1414
++p;
1515
if (ll(m)*p >= sz(a)) return poly(sz(a));
1616
num j = a[p];

fuzz-tests/numerical/Polynomial.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,21 @@ ll modpow(ll a, ll e) {
399399
}
400400
#include "../../content/numerical/FastFourierTransformMod.h"
401401
struct Mod {
402-
ll x;
403-
Mod() : x(0) {}
404-
Mod(ll xx) : x(xx % mod) {}
405-
Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
406-
Mod operator-(Mod b) { return Mod(x < b.x ? x - b.x + mod : x - b.x); }
407-
Mod operator*(Mod b) { return Mod(x * b.x); }
402+
ll v;
403+
Mod() : v(0) {}
404+
Mod(ll vv) : v(vv % mod) {}
405+
Mod operator+(Mod b) { return Mod((v + b.v) % mod); }
406+
Mod operator-(Mod b) { return Mod(v < b.v ? v - b.v + mod : v - b.v); }
407+
Mod operator*(Mod b) { return Mod(v * b.v); }
408408
Mod operator/(Mod b) { return *this * invert(b); }
409409
Mod invert(Mod a) { return a^(mod-2); }
410410
Mod operator^(ll e) {
411-
if (!e) return Mod(1);
412-
Mod r = *this ^ (e / 2); r = r * r;
413-
return e&1 ? *this * r : r;
411+
ll ans = 1, b = (*this).v;
412+
for (; e; b = b * b % mod, e /= 2)
413+
if (e & 1) ans = ans * b % mod;
414+
return ans;
414415
}
415-
explicit operator ll() const { return x; }
416+
explicit operator ll() const { return v; }
416417
};
417418

418419
typedef Mod num;
@@ -442,10 +443,10 @@ bool checkEqual(mine::poly a, MIT::poly b) {
442443
return false;
443444
int ml = min(sz(a), sz(b));
444445
for (int i = 0; i < ml; i++)
445-
if (a[i].x != b[i].v)
446+
if (a[i].v != b[i].v)
446447
return false;
447448
// for (int i = ml; i < sz(a); i++)
448-
// if (a[i].x != 0)
449+
// if (a[i].v != 0)
449450
// return false;
450451
// for (int i = ml; i < sz(b); i++)
451452
// if (b[i].v != 0)
@@ -456,7 +457,7 @@ bool checkEqual(mine::poly a, MIT::poly b) {
456457
template <class A, class B> void fail(A mine, B mit) {
457458
cout<<"mine: ";
458459
for (auto i : mine)
459-
cout << i.x << ' ';
460+
cout << i.v << ' ';
460461
cout << endl;
461462
cout<<"MIT: ";
462463
for (auto i : mit)

0 commit comments

Comments
 (0)