-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_template.cpp
580 lines (544 loc) · 12.4 KB
/
standard_template.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <cstring>
#include <cstdio>
#include <typeinfo>
using namespace std;
#define GotuZeno
/*=====================================================================================================*/
#ifdef GotuZeno
#define debug(x) cerr << #x <<" "; _print(x); cerr << "\n";
#else
#define debug(x);
#endif
// #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define deb(x) cerr << #x << " = " << x << "\n"
#define deb2(x,y) cerr<<#x<<" = "<<x<<"::"<<#y<<" = "<<y<<"\n"
#define deb3(x,y,z) cerr<<#x<<" = "<<x<<"::"<<#y<<" = "<<y<<"::"<<#z<<" = "<<z<<"\n"
#define bpc __builtin_popcount
#define bpcll __builtin_popcountll
//#define PI 3.1415926535897932384626
#define fast_io() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<int> vi;
typedef vector<ll> vl;
/*=====================================================================================================*/
const ll INF = 9223372036854775807;
const lld PI = acos(-1.0);
const lld eps = 1e-12;
const ll mod = 1000000007;
const ll mmod = 998244353;
/*=====================================================================================================*/
template <typename T, typename T1>
T amax(T &a, T1 b)
{
if (b > a)
a = b;
return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b)
{
if (b < a)
a = b;
return a;
}
void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }
// template declarations are here
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
// template implementation are here
template <class T, class V>
void _print(pair<T, V> p)
{
cerr << "{";
_print(p.ff);
cerr << ",";
_print(p.ss);
cerr << "}";
}
template <class T>
void _print(vector<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v)
{
cerr << "[ ";
for (auto i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}
/*=====================================================================================================*/
void google(int tc){
cout << "Case #" << tc << ": ";
}
ll gcd(ll a, ll b)
{
if (a < b)
{
return gcd(b, a);
}
if (b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
ll fast_pow(ll a, ll b, ll MOD = mod) // O(log(b))
{
ll res = 1;
a = a % MOD;
b = b % MOD; //if b is larger than or equal to mod
while (b > 0)
{
if (!(b % 2 == 0)) //b is odd
{
res = (res * a) % MOD;
}
//b is even
b = b / 2; // b >>= 1;
a = a * a % MOD; //a =a^2
}
return res;
}
//only for prime m
ll mminvprime(ll a, ll m){
return fast_pow(a,m-2,m);//fermats little theorem,(a^p-a)modp = 0;
}
ll mod_add(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
ll mod_mul(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
ll mod_sub(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
ll mod_div(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
ll bs_upper_bound(vector<ll> &a, ll n, ll x)
{
ll start = 0;
ll end = n;
// end is not n - 1.i.e why start<end and mid/not mid-1
ll ans=0;
while (start < end)
{
ll mid = start + (end - start) / 2;
//when a[mid] == x,i am incrementing l,i.e not giving lowerbound(equal)
if (a[mid] <= x)
{
ans = mid+1;
start = mid + 1;
}
else
{
end = mid;
}
}
return ans;//return start;
}
ll bs_lower_bound(vector<int> &a, int n, int x)
{
ll start = 0;
ll end = n;
// end is not n - 1,i.e why start<end and mid/not mid-1
// ans will at max go upto n]
ll ans=0;
while (start < end)
{
ll mid = start + (end - start) / 2;
//when a[mid]==x, l is in its position only,i.e getting its lowerbound
// if(a[mid] == x){
// return mid;
// }
if (a[mid] < x)
{
ans = mid+1;
start = mid + 1;
}
else
{
end = mid;
}
}
return ans;//return start;
}
string CONVERT_TO_BINARY(ll s)
{
string res = "";
while (s != 0)
{
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin(), res.end());
return res;
}
ll printNCR(int n, int r)
{
ll p = 1;
ll k = 1;
//C(n,r) = C(n,n-r)
if (n - r < r)
{
//reduces operations
r = n - r;
}
if (r != 0)
{
while (r > 0)
{
p *= n;
k *= r;
long long m = __gcd(p, k);
p = p / m;
k = k / m;
//saves us from overflow of numerator or denominator
n--;
r--;
}
//C(n,r)
// n(n-1)(n-2)....(n-(r-1))
// ------------------------
// r(r-1)(r-2)....(r-(r-1))
}
else
{
p = 1;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
return p;
}
/*=====================================================================================================*/
/*
const ll N = 50001;
//#define NCR
ll fact[N], invfact[N];
ll pw(ll a, ll p = mod - 2, ll MOD = mod)
{
ll result = 1;
while (p > 0)
{
if (p & 1ll){
result = result*a % MOD;
}
p = p>>1;//p /= 2;
a = a * a % MOD;//a ^=2;
}
return result;
}
void init()
{
ll p = mod;//PRIME modulus
fact[0] = 1;invfact[0] = 1;
ll i;
for (i = 1; i < N; i++)
{
fact[i] = i * fact[i - 1] % p;
}
i--;
invfact[i] = pw(fact[i], p - 2, p);
for (i--; i >= 0; i--)
{
invfact[i] = invfact[i + 1] * (i + 1) % p;
}
//do not prefer below for strict time complexity
// for (ll i = 1; i < N; i++)
// {
// fact[i] = (fact[i - 1] * i) % mod;
// invfact[i] = fast_pow(fact[i], mod - 2);
// }
}
ll ncr(ll n, ll r)
{
if (r > n || n < 0 || r < 0)
return 0;
return fact[n] * invfact[r] % mod * invfact[n - r] % mod;
}
*/
/*
//#define SIEVE
vector<ll> prime_array(N+1,1);
// vecror<ll> premes;
void sieve(){
prime_array[0]=prime_array[1] = 0;//neither prime nor composite
for(ll i=2; i*i<=N; p++){
if(i*i > N){
break;
}
if(prime_array[i] == 1){
for(ll j=i*i; j<=N; j+=i){
prime_array[j] = 0;//not prime
}
}
}
// Print all prime numbers
for (ll p=0; p<=N; p++){
if (prime_array[p]){
// primes.push_back(p);
cout << p << " ";
}
}
}
*/
//#define SS
/*
void strstm(){
string s;
// cin.ignore(numeric_limits<streamsize>::max(), '\n');
//ignores buffer chars/whitespaces like ' ' until next line'\n;' comes or EOF occurs
//cin.ignore();//ignores the buffer chars after EOF...,because getline will read buffers until '\n'
getline(cin,s,',');
deb(s);
stringstream ss(s);//isstream object reads string wise
string word = "";
while(getline(ss,word,'.')){
//read words from stream until . occurs
deb2(word,word.size());
}
}
*/
/*
#define PF
const ll MAXN = 100001;
vector<ll> spf(MAXN);
//O(nloglogn)
void prime_factorization()
{
spf[1] = 1;
//for remaining spf is no one
for(ll i=2; i<=MAXN; i++){
spf[i] = i;
}
for(ll i=4; i<=MAXN; i+=2){
spf[i] = 2;
}
for(ll i=3; i*i<=MAXN; i++){
if(spf[i] == i){
//prime
for(ll j=i*i; j<=MAXN; j+=i){
// marking SPF for all numbers divisible by i
// marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// till now smallest prime factors are precomputed
// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
// vector<ll> getFactorization(int x)
// {
// vector<ll> ret;
// while (x != 1)
// {
// ret.push_back(spf[x]);
// x = x / spf[x];
// }
// return ret;
// }
}
*/
/*=====================================================================================================*/
class dsu
{
public:
vl parent, rank;
dsu(ll n)
{
for (ll i = 0; i < n + 5; i++)
parent.push_back(i), rank.push_back(1);
}
ll find(ll v)
{
return parent[v] = (v == parent[v] ? v : find(parent[v]));
}
void join(ll u, ll v)
{
u = find(u), v = find(v);
if (rank[u] < rank[v])
swap(u, v);
if (u != v)
parent[v] = parent[u], rank[u] += rank[v];
}
};
class compare{
public:
inline bool operator()(ll &a,ll &b){
return a>b;
}
// sort(v.begin(),v.end(),compare());
// priority_queue<ll,vector<ll>,compare> pq;
};
class patternMatching{
public:
string text,pattern;
// cin>>text>>pattern;
vector<ll> pattern_found;
void compute_LPS(string &pattern,vector<ll> &lps,ll m){
ll j = 0,i = 1;
lps[0] = 0;//single char lps is 0,proper prefix==suffix
while(i<m){
if(pattern[i] == pattern[j]){
lps[i] = j+1;
i += 1;
j += 1;
}
else{// (pattern[i] != pattern[j])
if(j != 0){
j = lps[j-1];
}
else{
lps[i] = 0;
i += 1;
}
}
}
}
void find_pattern(){
ll n = text.size();
ll m = pattern.size();
if(m == 0){
return;
}
vector<ll> lps(m,0);
compute_LPS(pattern,lps,m);//lps of pattern
ll i = 0,j = 0;
while(i<n){
if(pattern[j] == text[i]){
i += 1;
j += 1;
if(j == m){
//pattern[0,.,j-1] found in text at text[i-j,.,i-1]
pattern_found.push_back(i-j);
if(j>0){
j = lps[j-1];
}
}
}
else{
if(j != 0){
j = lps[j-1];
}
else{
//j == 0
i += 1;//move forward in text
}
}
}
// _print(pattern_found);
}
};
void test_case(){
return;
}
int main(){
// ios::sync_with_stdio(false);cin.tie(NULL);
fast_io();
// cout << fixed << setprecision(20);//commonly used precision limit
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
#ifdef PF
prime_factorization();
#endif
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
#ifdef GotuZeno
freopen("error.txt", "w", stderr);
#endif
#ifdef SS
strstm();
#endif
// In time.h header file
clock_t start1 = clock();
int tt = 1;
cin >> tt;
for (int it = 1; it <= tt; it++)
{
// google(it);
test_case();
}
clock_t stop1 = clock();
#ifdef GotuZeno
cerr<<"\nTotal Time:"<<setprecision(6)<<fixed<<((long double)(stop1 - start1) / CLOCKS_PER_SEC);
#endif
return 0;
}