blob: 1694589f283908a15632eacf3234e24db984a786 (
plain)
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
|
#include "../util.h"
#include <math/cycleDetection.cpp>
pair<ll, ll> naive(ll x0, function<ll(ll)> f) {
map<ll, ll> seen;
ll d = 0;
while (seen.find(x0) == seen.end()) {
seen[x0] = d;
d++;
x0 = f(x0);
}
return {seen[x0], d - seen[x0]};
}
void stress_test() {
ll queries = 0;
for (ll i = 0; i < 1000'000; i++) {
int m = Random::integer<int>(1, 100);
int c = Random::integer<int>(0, m);
auto f = [&](ll x){return (x*x + c) % m;};
int x0 = Random::integer<int>(0, m);
auto got = cycleDetection(x0, f);
auto expected = naive(x0, f);
if (got != expected) cerr << "error: " << got.first << " " << got.second << " " << expected.first << " " << expected.second << FAIL;
queries += got.second;
}
cerr << "tested queries: " << queries << endl;
}
constexpr ll M = 18'086'183;
void performance_test() {
timer t;
auto f = [&](ll x){return (1337*x + 42) % M;};
t.start();
auto [a, b] = cycleDetection(42, f);
t.stop();
hash_t hash = a + b;
if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}
int main() {
stress_test();
performance_test();
}
|