Skip to content

Commit dbad12f

Browse files
committed
Modint.factorial / combination / permutation
1 parent e452602 commit dbad12f

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

2SAT/Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void addNand(int x, boolean f, int y, boolean g)
7171

7272
ならし $O(1)$
7373

74+
### set
75+
76+
```java
77+
public void set(int x, boolean f)
78+
```
79+
80+
`(x[i] = f)` というクローズを足します。
81+
7482
### satisfiable
7583

7684
```java

2SAT/TwoSAT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void addImplication(int x, boolean f, int y, boolean g) {
2929
public void addNand(int x, boolean f, int y, boolean g) {
3030
addClause(x, !f, y, !g);
3131
}
32+
public void set(int x, boolean f){
33+
addClause(x, f, x, f);
34+
}
35+
3236

3337
public boolean satisfiable() {
3438
hasCalledSatisfiable = true;

Convolution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) {
1515
int mod = 998244353;
1616
long[] a = { 1, 2, 3, 4, 5 };
1717
long[] b = { 6, 7, 8 };
18-
// 畳み込みを計算しまqす。a.length + b.length - 1 の配列が帰ります。
18+
// 畳み込みを計算します。a.length + b.length - 1 の配列が帰ります。
1919
long[] ret = Convolution.convolution(a, b, mod);
2020
System.out.println(Arrays.toString(ret));
2121
}

DSU/README.md

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

99
を $O(α(N))$ 時間で処理することが出来ます。
1010

11-
また、内部的に各連結成分ごとに代表となる頂点を 11 つ持っています。辺の追加により連結成分がマージされる時、新たな代表元は元の連結成分の代表元のうちどちらかになります。
11+
また、内部的に各連結成分ごとに代表となる頂点を 1 つ持っています。辺の追加により連結成分がマージされる時、新たな代表元は元の連結成分の代表元のうちどちらかになります。
1212

1313
## コンストラクタ
1414
### DSU

ModInt/ModInt.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import java.util.*;
2+
3+
import java.util.ArrayList;
4+
15
/**
26
* @verified
37
* <ul>
@@ -6,6 +10,7 @@
610
* <li> https://atcoder.jp/contests/abc129/tasks/abc129_f : (2 <= M <= 1000000000)
711
* <li> https://atcoder.jp/contests/arc050/tasks/arc050_c : (2 <= M <= 1000000000)
812
* <li> https://atcoder.jp/contests/arc012/tasks/arc012_4 : (1 <= M <= 1000000007)
13+
* <li> https://atcoder.jp/contests/abc042/tasks/arc058_b : (M = 1000000007, combination ver.)
914
* </ul>
1015
*/
1116
class ModIntFactory {
@@ -15,11 +20,15 @@ class ModIntFactory {
1520
private final boolean usesMontgomery;
1621
private final ModArithmetic.ModArithmeticMontgomery maMontgomery;
1722

23+
private ArrayList<Integer> factorial;
24+
1825
public ModIntFactory(int mod) {
1926
this.ma = ModArithmetic.of(mod);
2027
this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery;
2128
this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null;
2229
this.mod = mod;
30+
31+
this.factorial = new ArrayList<>();
2332
}
2433

2534
public ModInt create(long value) {
@@ -31,6 +40,30 @@ public ModInt create(long value) {
3140
}
3241
}
3342

43+
private void prepareFactorial(int max){
44+
factorial.ensureCapacity(max+1);
45+
if(factorial.size()==0) factorial.add(1);
46+
for(int i=factorial.size(); i<=max; i++){
47+
factorial.add(ma.mul(factorial.get(i-1), i));
48+
}
49+
}
50+
51+
public ModInt factorial(int i){
52+
prepareFactorial(i);
53+
return create(factorial.get(i));
54+
}
55+
56+
public ModInt permutation(int n, int r){
57+
if(n < 0 || r < 0 || n < r) return create(0);
58+
prepareFactorial(n);
59+
return create(ma.div(factorial.get(n), factorial.get(r)));
60+
}
61+
public ModInt combination(int n, int r){
62+
if(n < 0 || r < 0 || n < r) return create(0);
63+
prepareFactorial(n);
64+
return create(ma.div(factorial.get(n), ma.mul(factorial.get(r),factorial.get(n-r))));
65+
}
66+
3467
public int getMod() {
3568
return mod;
3669
}

ModInt/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ public ModInt create(long value)
6161
`value % mod` を持つ `ModInt` を生成します.
6262
計算量: $O(1)$
6363

64+
```java
65+
public ModInt factorial(int i)
66+
```
67+
68+
`(i!) % mod` を持つ `ModInt` を生成します.
69+
70+
```java
71+
public ModInt permutation(int n, int r)
72+
```
73+
74+
`n P r` を持つ `ModInt` を生成します.
75+
76+
```java
77+
public ModInt combination(int n, int r)
78+
```
79+
80+
`n C r` を持つ `ModInt` を生成します.
81+
6482
### ModIntFactory$ModInt
6583

6684
#### mod

0 commit comments

Comments
 (0)