-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMy CPP Template.cpp
174 lines (168 loc) · 4.1 KB
/
My CPP 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
//template provided by Yuwono Bangun Nagoro a.k.a SurgicalSteel for competitive programming purposes
#include <bits/stdc++.h>
#include <algorithm>
#define PI acos(-1)
#define MOD 1000000007
#define psb push_back
#define mkp make_pair
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long int lli;
typedef unsigned long long ull;
/*
Leave the sleep and let the springtime talk
In tongues from the time before man
Listen to a daffodil tell her tale
Let the guest in, walk out, be the first to greet the morn
The meadows of heaven await harvest
The cliffs unjumped, cold waters untouched
The elsewhere creatures yet unseen
Finally your number came up, free fall awaits the brave
Come
Taste the wine, race the blind
They will guide you from the light
Writing noughts till the end of time
Come
Surf the clouds, race the dark
It feeds from the runs undone
Meet me where the cliff greets the sea
*/
struct point{int x,y;};
int toInt(string x)
{
istringstream ss(x);
int a;
ss>>a;
return a;
}
string tostr(int x)
{
ostringstream ss;
ss<<x;
return ss.str();
}
string low(string inp)
{
string res;
locale loc;
for(string::size_type i=0;i<inp.length();i++)
{res+=tolower(inp[i],loc);}
return res;
}
bool isprime(int a) { //this prime test function was originally authored by Egor Suvorov a.k.a yeputons
if(a==1){return false;}
for (int i=2;i*i<=a;i++)
if(a%i==0)
return false;
return true;
}
vector<int> sieve(int n)
{
vector <int> res;
int temp=1;
for(int i=2;i<=n;i++)
{
for(int a=2;a<=i;a++)
{if(i%a==0){temp++;}}
if(temp==2){res.psb(i);}
temp=1;
}
return res;
}
string eliminateAt(string x, int num)//eliminates a single substring of a string in a given position
{
if(num==0){return x.substr(1,x.length()-1);}
else if(num==x.length()-1){return x.substr(0,x.length()-1);}
else{return x.substr(0,num)+x.substr(num+1,x.length()-1);}
}
long long factorial(int num) //finds factorial of given integer
{
if(num==0){return 1;}
else{return num*factorial(num-1);}
}
long long pangkatp(int base,int exp) //powers base by exponent
{
if(exp==0){return 1;}
else{return base*pangkatp(base,exp-1);}
}
int sumdigit(string a) //sums all digit on given string (if the string only contains digits)
{
int sum=0;
for(int x=0;x<a.length();x++){sum+=toInt(a.substr(x,1));}
return sum;
}
int absolutey(int a) //returns absolute value of an integer
{if(a<0){return a*-1;}else{return a;}}
bool contains(string a,string b) //Checks if string a contains string b, where a.length()>=b.length()
{
bool valid=false;
int x=0;
while(x<a.length()-b.length()&&!valid)
{
if(a.substr(x,b.length())==b){valid=true;}
else{x++;}
}
return valid;
}
char toCharSingle(string x)//single substring as input. Converts single substring to single char
{
char a[1];
strncpy(a,x.c_str(),sizeof(a));
return a[0];
}
string toStringSingle(char x) //converts single char to single substring
{
string c;
stringstream ss;
ss<<x;
ss>>c;
return c;
}
string reverse(string a) //reverses a given string
{
string x;
for(int y=a.length()-1;y>=0;y--)
{x+=a.substr(y,1);}
return x;
}
string tobase(int num,int base) //translates bitmask from decimal number to base n. 2<=n<=9
{
string a;
while(num>0)
{
a+=tostr(num%base);
num=(num-(num%base))/base;
}
return reverse(a);
}
int binarysearch(vector<int> a, int val) //requires a sorted vector, will return -1 if there's no such element exists in the vector
{
if (a.size() == 0) { return -1; }
int l = 0;
int r = a.size() - 1;
while (l < r)
{
int m = (l + r) / 2;
if (a[m] > val)
{
if (m == r) { m--; }
r = m;
}
if (a[m] < val)
{
if (m == l) { m++; }
l = m;
}
if (a[m] == val) { return m; }//return the index
}
return -1;
}
int main()
{
//MAIN SECTION GOES HERE
/* //In case you need, use them like cin and cout
fstream fin("input.txt");
fstream fout("output.txt");
*/
return 0;
}