-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigmodTemplate.java
More file actions
163 lines (138 loc) · 4.62 KB
/
Copy pathBigmodTemplate.java
File metadata and controls
163 lines (138 loc) · 4.62 KB
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
package com.fqh;
/**
* 大数取模模板 来自 https://codeforces.com/profile/yvbf
*/
public class BigmodTemplate {
static {
Bigmod.reset(false, 300300);
}
static
class Bigmod {
static long M9 = 998244353L, M1 = 1000_000_007L;
static long M = 0;
static int MAXN = -1;
static long[] cacheStepmul;
static long[] cacheStepmulInv;
static long[] exp2;
long num = 0;
// TODO: reset this before anything.
static void reset(boolean use998, int maxN) {
// 300300
M = use998 ? M9 : M1;
MAXN = maxN;
cacheStepmul = new long[MAXN];
cacheStepmulInv = new long[MAXN];
exp2 = new long[MAXN];
}
Bigmod(long num) {
if (num >= 0) {
this.num = num % M;
} else {
this.num = M - (-num) % M;
}
}
static Bigmod valueOf(long num) {
return new Bigmod(num);
}
@Override
public String toString() {
return Long.toString(num);
}
Bigmod add(long x) {
if (x > 0) {
num = (num + x % M) % M;
} else if (x < 0) {
num = (num - (-x) % M + M) % M;
}
return this;
}
Bigmod add(Bigmod bm) {
return add(bm.num);
}
Bigmod sub(long x) {
return add(-x);
}
Bigmod sub(Bigmod bm) {
return add(-bm.num);
}
Bigmod mul(long x) {
num = num * (x % M) % M;
return this;
}
Bigmod mul(Bigmod bm) {
return mul(bm.num);
}
Bigmod div(long x) {
if (x == 0) throw new RuntimeException("cannot div 0.");
return mul(exp(x, M - 2));
}
Bigmod div(Bigmod bm) {
return div(bm.num);
}
Bigmod exp(long p) {
long ret = 1L, baseLong = num % M;
for (int i = 0; i <= 60 && p > 0; ++i) { //1<<30=1073741824
if ((p & (1L << i)) > 0) {
ret = ret * baseLong % M;
p ^= (1L << i);
}
baseLong = baseLong * baseLong % M;
}
if (p != 0) throw new RuntimeException("p should reduced to 0 in exp.");
num = ret;
return this;
}
Bigmod exp(Bigmod bm) {
return exp(bm.num);
}
static long exp(long x, long p) {
return new Bigmod(x).exp(p).num;
}
static long C(long n, long m) {
if (n < 0 || m < 0) throw new RuntimeException("n/m cannot be negative in C(n,m).");
if (n < m) throw new RuntimeException("n is less than m in C(n,m).");
if (n == 0) return 1;
if (m == 0 || m == n) return 1;
return new Bigmod(P(n, m)).mul(stepmulInv(m)).num;
}
static long P(long n, long m) {
if (n < 0 || m < 0) throw new RuntimeException("n/m cannot be negative in P(n,m).");
if (n < m) throw new RuntimeException("n is less than m in P(n,m).");
if (n == 0) return 1;
if (n < cacheStepmul.length) {
// P(n,m)=n!/(n-m)!
return new Bigmod(stepmul(n)).mul(stepmulInv(n - m)).num;
}
// NOTE: O(m) complexity when n is large.
Bigmod r = new Bigmod(1);
long val = n;
for (long i = 0; i < m; ++i) r.mul(val--);
return r.num;
}
static long stepmul(long n) { // n!
if (n >= cacheStepmul.length) throw new RuntimeException("overflow cacheSm size.");
if (cacheStepmul[0] == 0) {
cacheStepmul[0] = 1;
cacheStepmulInv[0] = exp(cacheStepmul[0], M - 2);
for (int i = 1; i < cacheStepmul.length; ++i) {
cacheStepmul[i] = cacheStepmul[i - 1] * i % M;
cacheStepmulInv[i] = exp(cacheStepmul[i], M - 2);
}
}
return cacheStepmul[(int) n];
}
static long stepmulInv(long n) {
if (cacheStepmulInv[1] == 0) stepmul(2);
return cacheStepmulInv[(int) n];
}
static long exp2(long n) { // 2^n
if (n >= exp2.length) throw new RuntimeException("overflow exp2 size.");
if (exp2[0] == 0) {
exp2[0] = 1;
for (int i = 1; i < exp2.length; ++i)
exp2[i] = exp2[i - 1] * 2 % M;
}
return exp2[(int) n];
}
}
}