Skip to content

Commit 7133528

Browse files
committed
ModInt
1 parent f8c544c commit 7133528

2 files changed

Lines changed: 98 additions & 10 deletions

File tree

‎ModInt/ModInt.java‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @verified
3+
* <ul>
4+
* <li> https://atcoder.jp/contests/arc050/tasks/arc050_c
5+
* <li> https://atcoder.jp/contests/abc129/tasks/abc129_f
6+
* </ul>
7+
*/
18
class ModIntFactory {
29
private final ModArithmetic ma;
310
private final int mod;
@@ -23,6 +30,12 @@ private ModInt(int value) {
2330
public int mod() {
2431
return mod;
2532
}
33+
public int value() {
34+
if (ma instanceof ModArithmetic.ModArithmeticMontgomery) {
35+
return ((ModArithmetic.ModArithmeticMontgomery) ma).reduce(value);
36+
}
37+
return value;
38+
}
2639
public ModInt add(ModInt mi) {
2740
return new ModInt(ma.add(value, mi.value));
2841
}
@@ -41,11 +54,21 @@ public ModInt inv() {
4154
public ModInt pow(long b) {
4255
return new ModInt(ma.pow(value, b));
4356
}
44-
public int value() {
45-
if (ma instanceof ModArithmetic.ModArithmeticMontgomery) {
46-
return ((ModArithmetic.ModArithmeticMontgomery) ma).reduce(value);
47-
}
48-
return value;
57+
public ModInt addAsg(ModInt mi) {
58+
this.value = ma.add(value, mi.value);
59+
return this;
60+
}
61+
public ModInt subAsg(ModInt mi) {
62+
this.value = ma.sub(value, mi.value);
63+
return this;
64+
}
65+
public ModInt mulAsg(ModInt mi) {
66+
this.value = ma.mul(value, mi.value);
67+
return this;
68+
}
69+
public ModInt divAsg(ModInt mi) {
70+
this.value = ma.div(value, mi.value);
71+
return this;
4972
}
5073
@Override
5174
public String toString() {
@@ -151,7 +174,8 @@ public int inv(int a) {
151174
public int pow(int a, long b) {
152175
if (b < 0) throw new ArithmeticException("negative power");
153176
long res = 1;
154-
long pow2 = a, idx = 1;
177+
long pow2 = a;
178+
long idx = 1;
155179
while (b > 0) {
156180
long lsb = b & -b;
157181
for (; lsb != idx; idx <<= 1) {
@@ -201,7 +225,8 @@ public int inv(int a) {
201225
public int pow(int a, long b) {
202226
if (b < 0) throw new ArithmeticException("negative power");
203227
long res = 1;
204-
long pow2 = a, idx = 1;
228+
long pow2 = a;
229+
long idx = 1;
205230
while (b > 0) {
206231
long lsb = b & -b;
207232
for (; lsb != idx; idx <<= 1) {
@@ -323,7 +348,8 @@ public int inv(int a) {
323348
public int pow(int a, long b) {
324349
if (b < 0) throw new ArithmeticException("negative power");
325350
int res = 1;
326-
int pow2 = a, idx = 1;
351+
int pow2 = a;
352+
long idx = 1;
327353
while (b > 0) {
328354
long lsb = b & -b;
329355
for (; lsb != idx; idx <<= 1) {

‎ModInt/README.md‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# クラス ModIntFactory, ModIntFactory$ModInt
2+
23
- - -
34

45
剰余演算をサポートするクラスです.
@@ -35,7 +36,9 @@ public static void main(String[] args) {
3536
```
3637

3738
## コンストラクタ
39+
3840
### ModIntFactory
41+
3942
```java
4043
public ModIntFactory(int mod)
4144
```
@@ -44,19 +47,24 @@ public ModIntFactory(int mod)
4447
計算量: $O(1)$
4548

4649
### ModIntFactory$ModInt
50+
4751
外部からコンストラクタを呼ぶことは出来ません. (呼ばないで下さい)
4852

4953
## メソッド
54+
5055
### ModIntFactory
56+
5157
```java
52-
public ModInt ModIntFactory(int value)
58+
public ModInt create(long value)
5359
```
5460

5561
値 `value % mod` を持つ `ModInt` を生成します.
5662
計算量: $O(1)$
5763

5864
### ModIntFactory$ModInt
65+
5966
#### mod
67+
6068
```java
6169
public int mod()
6270
```
@@ -66,6 +74,7 @@ public int mod()
6674
計算量: $O(1)$
6775

6876
#### value
77+
6978
```java
7079
public int value()
7180
```
@@ -75,6 +84,7 @@ public int value()
7584
計算量: $O(1)$
7685

7786
#### add
87+
7888
```java
7989
public ModInt add(ModInt mi)
8090
```
@@ -84,6 +94,7 @@ public ModInt add(ModInt mi)
8494
計算量: $O(1)$
8595

8696
#### sub
97+
8798
```java
8899
public ModInt sub(ModInt mi)
89100
```
@@ -93,6 +104,7 @@ public ModInt sub(ModInt mi)
93104
計算量: $O(1)$
94105

95106
#### mul
107+
96108
```java
97109
public ModInt mul(ModInt mi)
98110
```
@@ -102,6 +114,7 @@ public ModInt mul(ModInt mi)
102114
計算量: $O(1)$
103115

104116
#### div
117+
105118
```java
106119
public ModInt div(ModInt mi)
107120
```
@@ -111,24 +124,73 @@ public ModInt div(ModInt mi)
111124
計算量: $O(\log \mod)$
112125

113126
制約
127+
114128
- `gcd(b, mod) = 1`
115129

116130
#### inv
131+
117132
```java
118133
public ModInt inv()
119134
```
135+
120136
`ModInt a` に対して, `(a * x) % mod = 1` を満たす値 `x` を持つ `ModInt` を新たに生成します (`a` の値は書き換わりません).
121137

122138
計算量: $O(\log \rm{mod})$
123139

124140
制約
141+
125142
- `gcd(a, mod) = 1`
126143

127144
#### pow
145+
128146
```java
129147
public ModInt pow(long n)
130148
```
131149

132-
`(a ^ n) % mod ` を満たす値 `x` を持つ `ModInt` を新たに生成します (`a` の値は書き換わりません).
150+
`(a ^ n) % mod` を満たす値 `x` を持つ `ModInt` を新たに生成します (`a` の値は書き換わりません).
133151

134152
計算量: $O(\log n)$
153+
154+
#### addAsg
155+
156+
```java
157+
public ModInt addAsg(ModInt mi)
158+
```
159+
160+
`a += b` を行います. `a` の値は書き換えられます.
161+
162+
計算量: $O(1)$
163+
164+
#### subAsg
165+
166+
```java
167+
public ModInt subAsg(ModInt mi)
168+
```
169+
170+
`a -= b` を行います. `a` の値は書き換えられます.
171+
172+
計算量: $O(1)$
173+
174+
#### mulAsg
175+
176+
```java
177+
public ModInt mulAsg(ModInt mi)
178+
```
179+
180+
`a *= b` を行います. `a` の値は書き換えられます.
181+
182+
計算量: $O(1)$
183+
184+
#### divAsg
185+
186+
```java
187+
public ModInt divAsg(ModInt mi)
188+
```
189+
190+
`a *= b^(-1)` を行います (`a /= b`). `a` の値は書き換えられます. ただし,`b^(-1)` は `(b * x) % mod = 1` を満たす `x` です.
191+
192+
計算量: $O(\log \mod)$
193+
194+
制約
195+
196+
- `gcd(b, mod) = 1`

0 commit comments

Comments
 (0)