-
Notifications
You must be signed in to change notification settings - Fork 2
/
Combinatorics Elements
204 lines (165 loc) · 4.7 KB
/
Combinatorics Elements
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
prob: https://codeforces.com/problemset/problem/1992/G
solution: https://codeforces.com/problemset/submission/1992/277568061
int extended_euclid(LL a,LL b,LL &x,LL &y){
if(b==0)
{
x=1;
y=0;
return a ;
}
/**
gcd = x*b + y*(a%b) ;
gcd = x*b + y*(a-(a/b)*b) ;
gcd = x*b + y*a - y*(a/b)*b ;
gcd = y*a + (x-(y*(a/b)))*b ;
/**/
LL temp , g ;
g=extended_euclid(b,a%b,x,y);
temp=x-y*(a/b) , x=y , y=temp ;
return g ;
}
LL inverse_mod(LL C){
LL x,y;
extended_euclid(MOD,C,x,y);
return ((y%MOD)+MOD)%MOD;
}
int mod_expo(int a,int b){
if(b==0)
return 1 ;
int x=mod_expo(a,b>>1) ;
x=x*x%MOD ; if(b&1)x=x*a%MOD ; return x ;
}
LL ncr(LL n,LL r){ /// this function calculates ncr upto N = 1e6 in O(n) time.
if(r>n)
{
return 0 ;
}
if(Fact[0]==0) /// for initialization.
{
Fact[0]=1;
for(int i=1;i<N;i++)
{
Fact[i]=Fact[i-1]*i%MOD;
}
Inv[N-1] = mod_expo(Fact[N-1],MOD - 2);
for (int i = N-2; i>=0; i--)
{
Inv[i] = Inv[i + 1] * (i + 1) % MOD;
}
}
r=Inv[r]*Inv[n-r]%MOD;
return Fact[n]*r%MOD;
/// ncr calculate using triangle formula nCr = (n-1)Cr + (n-1)C(r-1) ;
}
------------------------------------------------------------
void Stirling_Number()
{
s[0][0]=1 ;
for(int i=1;i<=2000;++i)
{
for(int j=1;j<=2000;++j)
{
s[i][j] = ( s[i-1][j-1] + j*s[i-1][j] ) % MOD ;
}
}
}
lucas theorem .
https://brilliant.org/wiki/lucas-theorem/
nice property of ncr
https://codeforces.com/problemset/problem/1717/D
for(int i=1;i<=k;++i)
s+=ncr(n,i) ;
/*catalan number
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700 ....(wihout any modulo)
2.
applications : https://www.geeksforgeeks.org/applications-of-catalan-numbers/
name : counting perfect bst
link : https://lightoj.com/problem/counting-perfect-bst
description : given a set of numbers . find how many number of binary search tree possible .
why :
(i) you need to know what is katalan number . (2n)!/((n+1)!*n!) is it's formula .
(ii) still it was solvable because perfect numbers won't be too much . dp will work .
(iii) but if the constraint would be too large we must have to use catalan number .
//// for online contest modulo operation ....
#include<bits/stdc++.h>
using namespace std;
template <int MOD_> struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
modnum inv() const {
modnum res;
res.v = minv(v, MOD);
return res;
}
modnum neg() const {
modnum res;
res.v = v ? MOD-v : 0;
return res;
}
modnum operator- () const {
return neg();
}
modnum operator+ () const {
return modnum(*this);
}
modnum& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modnum& operator += (const modnum& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum& operator /= (const modnum& o) {
return *this *= o.inv();
}
friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};
using num = modnum<998244353>;
const int MAXN = 2.1e5;
int N;
bool X[MAXN];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
num ans = 0;
int x = 100 ;
ans=ans+x ;
return 0;
}