-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathref_file.cpp
381 lines (343 loc) · 11.5 KB
/
ref_file.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Simple code of segment tree, evrything 0 indexed
class NumArray {
public:
// Check the constructor for the initialization of these variables.
vector<int> seg; // Segment Tree to be stored in a vector.
int n; // Length of the segment tree vector.
// Function to build the Segment Tree
// This function will fill up the child values first
// (left == right) will satisfy for the leaf values and they will be updated in segment tree
// seg[pos]=seg[2*pos+1]+ seg[2*pos+2]; -> This will help populate all other intermediate nodes
// as well as the root node with the "sum" of their child nodes.
// Finally we have a segment tree which has all 'nodes' with sum values of their child.
void buildTree(vector<int>& nums, int pos, int left, int right){
if (left == right){
seg[pos] = nums[left];
return;
}
int mid = (left+right)/2;
buildTree(nums, 2*pos+1, left, mid);
buildTree(nums, 2*pos+2, mid+1, right);
seg[pos]=seg[2*pos+1]+ seg[2*pos+2];
}
// Function to update a node in the segment tree
// When a node is updated, then the change in the node value has to be propagated to the root
// left, right -> represents the range of the node of segment tree. (Ex: [0, n-1] -> root)
// pos -> represents "position" in the segment tree data structure (Ex: 0 -> root)
// Using left, right and pos -> we have all the information on the segment tree
// Node at 'pos' in segment tree will have children at 2pos+1(left) and 2pos+2(right)
// If index is less than left or more than right, then it is out of bound
// for this node's range so we ignore it and return (This makes the algo O(logn))
// If left==right==index, then we found the index,
// update the value of the segment tree node & return
// Otherwise, we need to find the index and we do this by checking child nodes (2pos+1, 2pos+2)
// update the segment tree pos with the updated child values' sum.
// This would help propagate the updated value of the chid indexes to the parent (through recursion)
void updateUtil(int pos, int left, int right, int index, int val) {
// no overlap
if(index <left || index >right) return;
// total overlap
if(left==right){
if(left==index)
seg[pos]=val;
return;
}
// partial overlap
int mid=(left+right)/2;
updateUtil(2*pos+1,left,mid,index,val); // left child
updateUtil(2*pos+2,mid+1,right,index,val); // right child
seg[pos]=seg[2*pos+1]+seg[2*pos+2];
}
// Function to get the sum from the range [qlow, qhigh]
// low, high -> represents the range of the node of segment tree. (Ex: [0, n-1] -> root)
// pos -> represents "position" in the segment tree data structure (Ex: 0 -> root)
// Using low, high and pos -> we have all the information on the segment tree
// Node at 'pos' in segment tree will have children at 2pos+1(left) and 2pos+2(right)
// While searching for the range, there will be three cases: (Ex: arr: [-1, 4, 2, 0])
// - Total Overlap: Return the value. (Ex: qlow, qhigh: 0,3 and low, high: 1,2)
// - No Overlap: Return 0. (Ex: qlow, qhigh: 0,1 and low, high: 2,3)
// - Partial Overlap: Search for it in both the child nodes and their ranges.
// (Ex: Searching for 1,2 and node range is 0,1)
int rangeUtil(int qlow, int qhigh, int low, int high, int pos){
if (qlow <= low && qhigh>= high){ // total overlap
return seg[pos];
}
if (qlow > high || qhigh < low) { // no overlap
return 0;
}
// partial overlap
int mid = low+(high-low)/2;
return (rangeUtil(qlow, qhigh, low, mid, 2*pos+1) + rangeUtil(qlow, qhigh, mid+1, high, 2*pos+2));
}
// Constructor for initializing the variables.
NumArray(vector<int>& nums) {
if(nums.size() > 0){
n = nums.size();
seg.resize(4*n,0); // Maximum size of a segment tree for an array of size n is 4n
buildTree(nums, 0, 0, n-1); // Build the segment tree
// Print Segment Tree
// for(int i=0;i<4*n;i++)
// cout<<seg[i]<<" ";
// cout<<endl;
}
}
// Update the segment Tree recurively using updateUtil
void update(int index, int val) {
if(n==0)return;
updateUtil(0,0,n-1, index, val);
}
// Get the sum for a specific range for the segment Tree
int sumRange(int left, int right) {
if(n==0)return 0;
return rangeUtil(left, right, 0, n-1, 0);
// query from left to right while segment tree is now at 'root' (pos=0) and range(0, n-1)
}
};
// Euler Circuit (recursive code)
void EulerCycle(){
vector<int> adj[4] = {
{1,2} , {0,3} , {3} , {0,1}
} , ans;
function<void(int)> dfs = [&](int node){
while(adj[node].size()){
int child = adj[node].back();
adj[node].pop_back();
dfs(child);
}
ans.push_back(node);
};
dfs(0);
for (int i=ans.size()-1; i>=0; i--)
{
cout << ans[i];
if (i)
cout<<" -> ";
}
}
class DSU{
vector<int> p;
public:
DSU(vector<int> a){
int n = a.size();
p = vector<int>(n);
for(int i = 0; i < n; i++){
p[i] = i;
}
}
int get_parent(int a){
if(a == p[a]) return p[a];
return p[a] = get_parent(p[a]);
}
void set_union(int child , int parent){
child = get_parent(child);
parent = get_parent(parent);
p[child] = parent;
}
};
// bth root of a
double powx(double a , int b){
if(b < 2)
return pow(a,b);
if(b & 1)
return a * powx(a,b-1);
return powx(a*a,b/2);
}
int root(int a , int b){
double prev_x = INT_MIN, x = ((double)a)/b;
while(abs(x - prev_x) > 0.5){
prev_x = x;
x = prev_x - ((pow(prev_x,b) - a)/(b * pow(prev_x,b-1)));
}
return llround(x);
}
// ----------------------------------------------------------------
// Flexible Segment tree implementation
class SegmentTree{
// 1 based indexing of Segment tree, 0 based indexing of input vector
// start is inclusive, end is exclusive
vector<int> t , v;
int original_size;
int DODGING_VALUE;
function<int(int, int)> OPERATING_FUNCTION;
void build(int start , int end , int node = 1) {
if(start + 1 > end)
return;
if(start + 1 == end){
t[node] = v[start];
return;
}
int M = (start + end) >> 1;
build(start, M, 2*node);
build(M, end, 2*node+1);
t[node] = OPERATING_FUNCTION(t[2*node] , t[2*node+1]);
}
public:
SegmentTree(vector<int> a, function<int(int, int)> INPUT_FUNCTION , int INPUT_DODGING_VALUE){
v = a;
OPERATING_FUNCTION = INPUT_FUNCTION;
DODGING_VALUE = INPUT_DODGING_VALUE;
original_size = v.size();
t = vector<int>(4*original_size);
build(0 , original_size);
}
int get(int left , int right ,int start = 0, int end = -1 , int node = 1){
if(end == -1)
end = original_size , right++; // make right exclusive
if(left >= end || right <= start)
return DODGING_VALUE;
if(start >= left && end <= right)
return t[node];
int M = (start + end) >> 1;
return OPERATING_FUNCTION(
get(left,right,start,M,2*node) ,
get(left,right,M,end,2*node+1)
);
}
void update(int newValue , int left , int right ,int start = 0, int end = -1 , int node = 1){
if(end == -1)
end = original_size , right++;
if(left >= end || right <= start)
return;
if(start + 1 == end){
t[node] = newValue;
return;
}
int M = (start + end) >> 1;
update(newValue , left , right , start, M, 2*node);
update(newValue , left , right , M, end, 2*node+1);
t[node] = OPERATING_FUNCTION(t[2*node] , t[2*node+1]);
}
void print(int start = 0, int end = -1, int node = 1){
if(end == -1) end = original_size;
cout << start << ", " << end << " : " << t[node] << endl;
if(start + 1 >= end)
return;
int M = (start + end) >> 1;
print(start, M, 2*node);
print(M, end, 2*node+1);
}
};
// BIT implementation
class BIT{
int n;
vector<int> v;
public:
int getSum(int pos){
pos++;
int sum = 0;
while(pos > 0){
sum += v[pos];
pos -= pos & (-pos);
}
return sum;
}
void update(int addVal , int pos){
pos++;
while(pos <= n){
v[pos] += addVal;
pos += pos & (-pos);
}
}
BIT(vector<int> a){
n = a.size();
v = vector<int>(n+1);
for(int i = 0; i < n; i++){
update(a[i] , i);
}
}
};
void testBIT(){
vector<int> a;
for(int i = 1; i < 11; i++) a.push_back(i);
BIT bt(a);
cout << bt.getSum(2) << endl;
bt.update(1,3);
cout << bt.getSum(3) << endl;
cout << bt.getSum(4) << endl;
}
// Extended Euclidean algorithm for gcd
int gcd_extended_euclidean(int a , int b , int& x , int& y){
if(a == 0){
x = 0;
y = 1;
return b;
}
int x1 , y1;
int gcd = gcd_extended_euclidean(b%a , a , x1 , y1);
x = y1 - (b/a) * x1;
y = x1;
return gcd;
}
// Modular Inverse
int modInverse(int a , int m){
int x , y;
int gcd = gcd_extended_euclidean(a , m , x , y);
if(gcd != 1) return -1;
return (x%m + m) % m;
}
// Chinese Remainder Theorem
int crt(vector<int> p , vector<int> k){
int n = p.size() , ans , pp = 1;
for(int i = 0; i < n; i++){
pp *= p[i];
}
for(int i = 0; i < n; i++){
ans = (ans +
(( (k[i] * modInverse(pp/p[i] , p[i])) % pp ) * (pp / p[i])) % pp
) % pp;
}
return ans;
}
// number of topological arrangements in a directed tree
const int M = 1e9+7;
int binpow(int a, int b){
if(b == 0) return 1;
if(b & 1)
return (a * 1ll * binpow(a , b-1)) % M;
return binpow((a*1ll*a)%M , b/2);
}
int div(int a, int b){
return mul(a,binpow(b , M-2));
}
int mul(int a , int b){
return (a*1ll*b)%M;
}
int waysToBuildRooms(vector<int>& p) {
// p[i] is parent of i;
int n = p.size();
vector<int> adj[n] , sz(n) , fact(1e5+1);
for(int i = 1; i < n; i++){
adj[p[i]].push_back(i);
}
fact[0] = 1;
for(int i = 1; i <= 1e5; i++){
fact[i] = (i * 1ll * fact[i-1]) % M;
}
unordered_map<int , int> mp;
function<int(int)> dp = [&](int node){
if(mp.find(node) != mp.end())
return mp[node];
mp[node] = 1;
for(int child : adj[node]){
mp[node] = mul(mp[node] , dp(child));
mp[node] = div(mp[node] , fact[sz[child]]);
sz[node] += sz[child];
}
mp[node] = mul(mp[node] , fact[sz[node]]);
sz[node]++; // add self
return mp[node];
};
return dp(0);
}
signed main(){
// testBIT();
vector<int> num = { 2,3,5,7,11,13,17,19 } , rem = { 1,2, 3,4,5,6,7,8 };
cout << crt(num , rem) << endl;
return 0;
}