Skip to content

Commit a386689

Browse files
update
1 parent 446bd96 commit a386689

File tree

7 files changed

+110
-49
lines changed

7 files changed

+110
-49
lines changed

Cpp/library/IO.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
// doot diddly donger cuckerino Hahahahahah
22

33
#pragma once
4-
//#pragma GCC optimize("Ofast")
5-
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
6-
//#pragma GCC optimize("unroll-loops")
4+
#pragma GCC optimize("Ofast")
5+
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
6+
#pragma GCC optimize("unroll-loops")
77

88
#include <bits/stdc++.h>
9-
#include <ext/pb_ds/assoc_container.hpp>
10-
#include <ext/pb_ds/tree_policy.hpp>
11-
12-
using namespace __gnu_pbds;
13-
using namespace __gnu_cxx;
149
using namespace std;
1510

1611
using ll = long long;
1712
using ld = long double;
18-
using pll = pair<ll, ll>;
13+
using pii = pair<int, int>;
1914
using vi = vector<int>;
2015

2116
#define all(x) (x).begin(), (x).end()

Cpp/library/collections/DSU.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,25 @@ struct DSU {
2323
}
2424
}
2525

26+
bool same(int x, int y) {
27+
return get(x) == get(y);
28+
}
29+
2630
int get(int x) {
2731
return par[x] = (x == par[x]) ? x : get(par[x]);
2832
}
2933

3034
bool unite(int x, int y) {
3135
x = get(x);
3236
y = get(y);
33-
if (x == y) return true;
37+
if (x == y) return false;
3438
if (rank[x] < rank[y]) swap(x, y);
3539
par[y] = x;
3640
if (heuristics == SIZE) {
3741
rank[x] += rank[y]; // Size use any one.
3842
} else {
3943
if (rank[x] == rank[y]) rank[x]++; // Rank Heuristics
4044
}
41-
return false;
45+
return true;
4246
}
4347
};

Cpp/library/generators.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ vector<T> randomVector(int n, T low, T high) {
5151
}
5252
return res;
5353
}
54+
55+
template <typename T>
56+
vector<T> permutation(int n, int start = 0){
57+
vector<T> res(n);
58+
iota(res.begin(), res.end(), start);
59+
return res;
60+
}
61+
62+
template <typename T>
63+
vector<T> uniqueVector(int n, T low, T high) {
64+
set<int> st;
65+
uniform_int_distribution<T> distribution(low, high);
66+
while ((int) st.size() != n) {
67+
st.insert(distribution(rng));
68+
}
69+
return vector<T>(st.begin(), st.end());
70+
}
71+
72+
template <typename T>
73+
set<T> randomSet(int n, T low, T high) {
74+
set<int> st;
75+
uniform_int_distribution<T> distribution(low, high);
76+
while ((int) st.size() != n) {
77+
st.insert(distribution(rng));
78+
}
79+
return st;
80+
}
81+
5482
template <typename T>
5583
vector<vector<T>> randomMatrix(int n, int m, vector<T> vals) {
5684
vector<vector<T>> res(n, vector<T>(m, vals[0]));

Cpp/library/number/Mint.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ class Modular {
8484
template <typename U>
8585
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
8686

87-
template <typename U>
88-
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
87+
template <typename V, typename U>
88+
friend V& operator>>(V& stream, Modular<U>& number);
8989

9090
private:
9191
Type value;
@@ -135,14 +135,16 @@ string to_string(const Modular<T>& number) {
135135
return to_string(number());
136136
}
137137

138-
template <typename T>
139-
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
138+
// U == std::ostream? but done this way because of fastoutput
139+
template <typename U, typename T>
140+
U& operator<<(U& stream, const Modular<T>& number) {
140141
return stream << number();
141142
}
142143

143-
template <typename T>
144-
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
145-
typename common_type<typename Modular<T>::Type, int64_t>::type x;
144+
// U == std::istream? but done this way because of fastinput
145+
template <typename U, typename T>
146+
U& operator>>(U& stream, Modular<T>& number) {
147+
typename common_type<typename Modular<T>::Type, long long>::type x;
146148
stream >> x;
147149
number.value = Modular<T>::normalize(x);
148150
return stream;
@@ -157,5 +159,5 @@ ModType& md = VarMod::value;
157159
using Mint = Modular<VarMod>;
158160
*/
159161

160-
constexpr int md = 1e9 + 7;
162+
constexpr int md = (int) 1e9 + 7;
161163
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

Cpp/library/number/Sieve.hpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
struct Sieve {
55
int maxn;
66
public:
7+
using MapType = unordered_map<long long, long long>;
78
std::vector<long long> spf;
89
std::vector<long long> primes;
910
std::vector<long long> mobius;
1011
std::vector<bool> isPrime;
1112

12-
Sieve(int _n = 2e6 + 7) : maxn(_n + 1) {
13+
explicit Sieve(int _n = 2e6 + 7) : maxn(_n + 1) {
1314
spf.resize(maxn, 1);
1415
isPrime.resize(maxn, true);
1516
primes.clear();
@@ -78,8 +79,8 @@ struct Sieve {
7879
return ans;
7980
}
8081

81-
vector<pll> divisorPair(long long x) {
82-
vector<pll> res;
82+
vector<pair<long long, long long>> divisorPair(long long x) {
83+
vector<pair<long long, long long>> res;
8384
for (long long i = 1; i * i <= x; i++) {
8485
if (x % i == 0) {
8586
if (x / i == i) {
@@ -92,9 +93,25 @@ struct Sieve {
9293
return res;
9394
}
9495

95-
map<long long, long long> primeFactors(long long val) {
96+
set<long long> divisorSet(long long x) {
97+
set<long long> res;
98+
for (long long i = 1; i * i <= x; i++) {
99+
if (x % i == 0) {
100+
res.insert(i);
101+
if (x / i != i) {
102+
res.insert(x / i);
103+
}
104+
}
105+
}
106+
return res;
107+
}
108+
109+
MapType primeFactors(long long val) {
96110
if (val >= maxn) return primeFactorBrute(val);
97-
map<long long, long long> fac;
111+
static unordered_map<int, MapType> memo;
112+
if(memo.count(val)) return memo[val];
113+
long long temp = val;
114+
MapType fac;
98115
while (val > 1) {
99116
int pf = spf[val];
100117
if (pf == 1) break;
@@ -105,11 +122,11 @@ struct Sieve {
105122
}
106123
fac[pf] = cnt;
107124
}
108-
return fac;
125+
return memo[temp] = fac;
109126
}
110127

111-
map<long long, long long> primeFactorBrute(long long val) {
112-
map<long long, long long> mp;
128+
MapType primeFactorBrute(long long val) {
129+
MapType mp;
113130
while (val % 2 == 0) {
114131
mp[2]++;
115132
val /= 2;

Cpp/library/number/factorizer.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ bool IsPrime(T n, const vector<T>& bases) {
5454
return true;
5555
}
5656

57-
bool IsPrime(int64_t n) {
57+
bool IsPrime(long long n) {
5858
return IsPrime(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
5959
}
6060

6161
bool IsPrime(int32_t n) {
6262
return IsPrime(n, {2, 7, 61});
6363
}
6464

65-
// but if you really need uint64_t version...
65+
// but if you really need unsigned long long version...
6666
/*
67-
bool IsPrime(uint64_t n) {
67+
bool IsPrime(unsigned long long n) {
6868
if (n < 2) {
6969
return false;
7070
}
@@ -81,9 +81,9 @@ bool IsPrime(uint64_t n) {
8181
return true;
8282
}
8383
uint32_t s = __builtin_ctzll(n - 1);
84-
uint64_t d = (n - 1) >> s;
85-
function<bool(uint64_t)> witness = [&n, &s, &d](uint64_t a) {
86-
uint64_t cur = 1, p = d;
84+
unsigned long long d = (n - 1) >> s;
85+
function<bool(unsigned long long)> witness = [&n, &s, &d](unsigned long long a) {
86+
unsigned long long cur = 1, p = d;
8787
while (p > 0) {
8888
if (p & 1) {
8989
cur = (__uint128_t) cur * a % n;
@@ -102,8 +102,8 @@ bool IsPrime(uint64_t n) {
102102
}
103103
return true;
104104
};
105-
vector<uint64_t> bases_64bit = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
106-
for (uint64_t a : bases_64bit) {
105+
vector<unsigned long long> bases_64bit = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
106+
for (unsigned long long a : bases_64bit) {
107107
if (a % n == 0) {
108108
return true;
109109
}
@@ -239,7 +239,7 @@ vector<pair<T, int>> Factorize(T x) {
239239
}
240240
return ret;
241241
}
242-
if (x <= static_cast<int64_t>(precalculated) * precalculated) {
242+
if (x <= static_cast<long long>(precalculated) * precalculated) {
243243
vector<pair<T, int>> ret;
244244
if (!IsPrime(x)) {
245245
for (T i : primes) {

Cpp/library/string/suffix_array.hpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ struct suffix_array {
2424
private:
2525
string s;
2626
public:
27-
vector<int> p, c;
27+
vector<int> p, c, lcp;
2828
int n;
2929
// strings are s.substr(p[i], n - p[i]);
3030

31-
explicit suffix_array(string _s) : s(std::move(_s)) {
31+
explicit suffix_array(string _s) : s(std::move(_s)) {
32+
lcp.clear();
3233
s += "$";
3334
n = (int) s.length();
3435
// Ordering and Equivalence classes
@@ -51,7 +52,7 @@ struct suffix_array {
5152
}
5253
for (int k = 0; ((1ll << k) < n); ++k) {
5354
for (int i = 0; i < n; ++i) {
54-
p[i] = (p[i] - (1 << k) + n) % n;
55+
p[i] = (p[i] - (1ll << k) + n) % n;
5556
}
5657
count_sort(p, c);
5758
vector<int> c_new(n, 0);
@@ -75,16 +76,9 @@ struct suffix_array {
7576
return s.substr(p[i], n - 1 - p[i]);
7677
}
7778

78-
vector<string> allSubs() {
79-
vector<string> res;
80-
for (int i = 0; i < n; ++i) {
81-
res.push_back(sub(i));
82-
}
83-
return res;
84-
}
85-
86-
vector<int> lcp() {
87-
vector<int> lcp(n, 0);
79+
vector<int> build_lcp() {
80+
if (!lcp.empty()) return lcp;
81+
lcp = vector<int>(n, 0);
8882
int k = 0;
8983
for (int i = 0; i < n - 1; ++i) {
9084
int pi = c[i]; // Pos of suffix i in suffix array
@@ -98,6 +92,27 @@ struct suffix_array {
9892
return lcp;
9993
}
10094

95+
long long countDistinct() {
96+
build_lcp();
97+
long long result = 0;
98+
for (int i = 0; i < lcp.size(); i++)
99+
result += (n - 1 - p[i]) - lcp[i];
100+
return result;
101+
}
102+
103+
int operator[](int index) const {
104+
return p[index];
105+
}
106+
107+
// Returns all the substrings of the given string
108+
vector<string> allSubs() {
109+
vector<string> res;
110+
for (int i = 0; i < n; ++i) {
111+
res.push_back(sub(i));
112+
}
113+
return res;
114+
}
115+
101116
int count(const string& sub) {
102117
int high = upper(sub);
103118
int low = lower(sub);

0 commit comments

Comments
 (0)