Skip to content

Commit 65c69a3

Browse files
committed
Added math algorithms
1 parent e38e050 commit 65c69a3

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

graph/lca_binary_lifting.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ int lca(int x, int y)
3434
if (dep[y]-(1<<i) >= dep[x])
3535
y = anc[y][i];
3636

37-
if (x == y) return x;
38-
37+
if (x == y) return x
3938
for (int i = maxlog-1; i >= 0; i--)
4039
if (anc[x][i] != anc[y][i] and
4140
anc[x][i] != 0 and anc[y][i] != 0)

math/gcdext.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
tuple<ll, ll, ll> gcdext(ll a, ll b)
8+
{
9+
if (a == 0)
10+
return {1, 0, a};
11+
int x, y, g;
12+
tie(x, y, g) = gcdext(b, a%b);
13+
return {y, x-(a/b)*y, g};
14+
}
15+
16+
ostream& operator<<(ostream& out, const tuple<ll,ll,ll>& x)
17+
{
18+
out << "(";
19+
out << get<0>(x) << ", ";
20+
out << get<1>(x) << ", ";
21+
out << get<2>(x) << ")";
22+
return out;
23+
}
24+
25+
int main()
26+
{
27+
ll x, y;
28+
cin >> x >> y;
29+
30+
auto ans = gcdext(x, y);
31+
32+
cout << ans << "\n";
33+
}

math/phi.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
const int maxn = 1010101;
6+
int s[maxn];
7+
8+
void sieve_phi()
9+
{
10+
for (int i = 1; i < maxn; i++)
11+
s[i] = i;
12+
13+
for (int i = 2; i < maxn; i++)
14+
if (s[i] == i)
15+
for (int j = i; j < maxn; j += i)
16+
s[j] -= s[j]/i;
17+
}
18+
19+
int phi(int x)
20+
{
21+
int ans = x, k = x;
22+
for (int i = 2; i*i <= x and k > 1; i++) {
23+
if (k%i ==0) {
24+
ans -= ans/i;
25+
while (k%i == 0)
26+
k /= i;
27+
}
28+
}
29+
30+
if (k != 1)
31+
ans -= ans/k;
32+
33+
return ans;
34+
}
35+
36+
int main()
37+
{
38+
sieve_phi();
39+
}

math/sieve.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
const int maxn = 10101010;
6+
7+
char s[maxn];
8+
// bitset<maxn> s;
9+
vector<int> primes;
10+
11+
12+
void init()
13+
{
14+
for (int i = 2; i*i < maxn; i++)
15+
if (!s[i])
16+
for (int j = i*i; j < maxn; j++)
17+
s[j] = 1;
18+
19+
for (int i = 2; i < maxn; i++)
20+
if (!s[i]) primes.push_back(i);
21+
}
22+
23+
int main()
24+
{
25+
init();
26+
}

0 commit comments

Comments
 (0)