-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinatorics.cpp
111 lines (94 loc) · 2.25 KB
/
Combinatorics.cpp
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
//Important Series:
//sum of first n integers = n * (n + 1) / 2
// 1 + 2 + 3 = 3 * 4 / 2
//sum of first n odd integers = n^2
// 1 + 3 + 5 = 3^2
//sum of first n powers of two = (2^n) - 1
// 1 + 2 + 4 = (2^3) - 1
//sum of arithmetic series
//sum of geometric series
/**
* Counting principles
- Product Rule
- Sum Rule (Inclusion-Exclusion)
- Division Rule
* Permutations
* Combinations
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int N = 1e6 + 5, MOD = 1e9 + 7;
lld fact[N];
void calcFactorial(){
fact[0] = 1;
for(int i = 1; i < N; ++i){
fact[i] = (i * fact[i-1]) % MOD;
//cout << fact[i] << " ";
}
}
lld power(lld b, lld po, lld mod){
lld ret = 1;
while(po){
if(po & 1) ret = (ret * b) % mod;
b = (b * b) % mod;
po >>= 1;
}
return ret;
}
lld mulInv(lld x, lld mod){
return power(x, mod - 2, mod);
}
//- - - -
//6 5 4 3
//6! / 2!
//n numbers, k places
//P(n, k) = n! / (n-k)!
//If numbers can be repeated -> n^k
lld P(lld n, lld k){ /// O(log(MOD))
return (fact[n] * mulInv(fact[n-k], MOD)) % MOD;
}
//C(n, k) = P(n, k) / k!
// = n! / ((n-k)! * k!)
lld C(lld n, lld k){ /// O(log(MOD))
//return (P(n, k) * mulInv(fact[k], MOD)) % MOD;
return (fact[n] * mulInv(fact[n-k] * fact[k] % MOD, MOD)) % MOD;
}
int main()
{
/*
123
132
213
231
312
321
*/
int n = 3;
int a[n] = {3, 1, 2};
sort(a, a + n);
//reverse(a, a + n);
do{ /// O(n!)
for(int i = 0; i < n; ++i)
cout << a[i] << " ";
puts("");
}while(next_permutation(a, a + n)); //prev_permutation
puts("");
calcFactorial();
cout << 10 * 9 * 8 << endl;
cout << P(10, 3) << endl;
cout << C(10, 3) << endl;
lld C[103][103];
/// C(n, k) = C(n-1, k-1) + C(n-1, k)
/* Pascal's Triangle, i = row, j = col */
for(int i = 1; i <= 100; ++i){ /// O(n^2)
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; ++j){
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
}
}
cout << C[10][3] << endl;
/// Note1: C(n, k) = C(n, n-k)
/// Note2: C(n, 0) + C(n, 1) + C(n, 2) ..... C(n, n) = 2^n
return 0;
}