Skip to content

Commit 4d82236

Browse files
author
Amit K S
authored
Add files via upload
1 parent 56c38dd commit 4d82236

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

Template.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,65 @@ using namespace std;
1111
#define speed ios::sync_with_stdio(false)
1212

1313

14+
// FAST I/O
15+
16+
void fastscan(int &x){
17+
register int c = getchar_unlocked();
18+
x = 0;
19+
for(; (c<48 || c>57); c = getchar_unlocked());
20+
for(; c>47 && c<58; c = getchar_unlocked()){
21+
x = (x<<1) + (x<<3) + c - 48;
22+
}
23+
}
24+
25+
// GCD - EUCLID'S ALGORITHM
26+
27+
ll gcd(ll a, ll b){
28+
if(b == 0){
29+
return a;
30+
}
31+
else{
32+
return gcd(b, a % b);
33+
}
34+
}
35+
36+
// MILLER RABIN PRIMALITY TEST
37+
38+
bool millerrabin(ll n){
39+
if(n <=1 || n % 2 == 0){
40+
if(n != 2){
41+
return false;
42+
}
43+
}
44+
if(n == 2 || n == 3){
45+
return true;
46+
}
47+
ll d = n-1;
48+
while(d % 2 == 0){
49+
d /= 2;
50+
}
51+
ll a[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
52+
for(int i = 0; i < 12 && a[i] < n; ++i){
53+
ll temp = d;
54+
ll mod = largepow(a[i], temp, n);
55+
if( mod == 1 || mod == n-1){
56+
continue;
57+
}
58+
while(temp != n-1 && mod != n-1){
59+
mod = fastmul(mod, mod, n);
60+
temp *= 2;
61+
}
62+
if(mod != n-1){
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
1469
// DISJOINT SUBSET
1570

71+
// two arrays arr[] and size[] of size <n> required
72+
1673
void setupDS(int arr[], int size[], int n){
1774
for(int i = 0; i < n; i++){
1875
arr[i] = i;
@@ -39,4 +96,4 @@ void unionDS(int arr[], int size[], int n, int A, int B){
3996
arr[rootB] = arr[rootA];
4097
size[rootA] += size[rootB];
4198
}
42-
}
99+
}

0 commit comments

Comments
 (0)