Skip to content

Commit 95af284

Browse files
authored
feat: fast_mod was added
1 parent db90c38 commit 95af284

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

numeric/numeric_mod.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
const int MOD = int(1e9+7);
1+
const int md = 1e9 + 7;
22

3-
template<typename T>
4-
T sub(T a, T b) {
5-
return (1LL*(a-b)%MOD + MOD) % MOD;
6-
}
7-
8-
template<typename T>
9-
T add(T a, T b) {
10-
return (1LL*(a%MOD) + 1LL*(b%MOD)) % MOD;
11-
}
12-
13-
template<typename T>
14-
T mul(T a, T b) {
15-
return (1LL*(a%MOD) * (b%MOD)) % MOD;
16-
}
3+
template<typename T> T sub(T a, T b) { return (1LL*(a-b)%md + md) % md; }
4+
template<typename T> T add(T a, T b) { return (1LL*(a%md) + 1LL*(b%md)) % md; }
5+
template<typename T> T mul(T a, T b) { return (1LL*(a%md) * (b%md)) % md; }
176

18-
template<typename T, typename U>
19-
T fastpow(T a, U b) {
7+
template<typename T, typename U> T fastpow(T a, U b) {
208
assert(0 <= b);
219
T answer = static_cast<T>(1);
2210
while (b > 0) {
@@ -29,22 +17,18 @@ T fastpow(T a, U b) {
2917
return answer;
3018
}
3119

32-
template<typename T>
33-
T inverse(T a) {
34-
a %= MOD;
35-
if (a < 0) a += MOD;
36-
T b = MOD, u = 0, v = 1;
20+
template<typename T> T inverse(T a) {
21+
a %= md;
22+
if (a < 0) a += md;
23+
T b = md, u = 0, v = 1;
3724
while (a) {
3825
T t = b / a;
3926
b -= t * a; swap(a, b);
4027
u -= t * v; swap(u, v);
4128
}
4229
assert(b == 1);
43-
if (u < 0) u += MOD;
30+
if (u < 0) u += md;
4431
return u;
4532
}
4633

47-
template<typename T>
48-
T division(T a, T b) {
49-
return mul(a, inverse(b));
50-
}
34+
template<typename T> T division(T a, T b) { return mul(a, inverse(b)); }

numeric/numeric_mod_fast.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// inline int add(int a, int b, const int& mod) { return (a+b>=mod)? a+b-mod : a+b; }
2+
// inline int sub(int a, int b, const int& mod) { return (a-b<0)? a-b+mod : a-b; }
3+
// inline int mul(int a, int b, const int& mod) { return (1LL*a*b)%mod; }
4+
5+
const int md = 1e9 + 7;
6+
7+
inline int add(int a, int b) { return (a+b>=md)? a+b-md : a+b; }
8+
inline int sub(int a, int b) { return (a-b<0)? a-b+md : a-b; }
9+
inline int mul(int a, int b) { return (1LL*a*b)%md; }

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy