-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtemplate.cpp
123 lines (106 loc) · 2.4 KB
/
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
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define SI(x) cin>>x
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define FI(w, x, y, z) cin>>w>>x>>y>>z
#define SO(x) cout<<x<<endl
#define DO(x, y) cout<<x<<" "<<y<<endl
#define TO(x, y, z) cout<<x<<" "<<y<<" "<<z<<endl
#define FO(w, x, y, z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pr pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define F(i, a, n) for(i=a;i<n;i++)
#define B(i, a, n) for(i=n-1;i>=0;i--)
#define mod 1000000007
#define pi 3.141592653589793238
template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}
template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
int l=v.size();for(int i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}
//GCD
lli gcd(lli a, lli b){
if(b < a) {
lli temp = a;
a = b;
b = temp;
}
if(a == 0){
return b;
}
return gcd(b%a,a);
}
//LCM
lli lcm(lli a, lli b){
return (a*b/gcd(a,b));
}
//Factorial
lli fact(lli x){
lli ans = 1;
if(x == 0){
return 1;
}
while(x > 0){
ans = (ans*x)%mod;
x--;
}
return ans%mod;
}
//power a^b
lli power(lli a, lli b){
lli ans = 1;
while(b > 0){
if(b%2 == 1){
ans *= a;
ans = ans;
}
b/=2;
a*=a;
a = a;
}
return ans;
}
//binary of a number
vector <lli> getbinary(int x){
vector <lli> bin(10,0);
lli iter = 0;
while(x > 0){
if(x%2 == 1){
bin[iter] = 1;
}
else{
bin[iter] = 0;
}
iter++;
x/=2;
}
return bin;
}
//Count number of ones in binary representation of a number T(n) = O(number of ones in binary)
int countOnesInBinary(int n){
int count=0;
while (n!=0)
{
n = n & (n-1);
count++;
}
return count;
}
int countOnes(int n){
// T(n) = O(1)
return __builtin_popcount(n);
}
// Check if number if of power two in constant time
int powerOfTwoCheck(int n){
if(n & (n-1) == 0) return 1;
return 0;
}