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
|
#include "../util.h"
#include <math/extendedEuclid.cpp>
#include <math/multInv.cpp>
#include <math/linearCongruence.cpp>
ll naive(ll a, ll b, ll m) {
for (ll x = 0; x < m; x++) {
if ((a * x) % m == b) return x;
}
return -1;
}
void stress_test() {
ll work = 0;
ll positive = 0;
for (ll tries = 0; tries < 500'000; tries++) {
ll m = Random::integer<ll>(1, 1'000);
ll a = Random::integer<ll>(0, m);
ll b = Random::integer<ll>(0, m);
ll got = solveLinearCongruence(a, b, m);
ll expected = naive(a, b, m);
if (got != expected) cerr << "got: " << got << ", expected: " << expected << endl;
work++;
if (got >= 0) positive++;
}
cerr << "stress tested: " << work << " (" << positive << ")" << endl;
}
constexpr int N = 500'000;
void performance_test() {
timer t;
hash_t hash = 0;
for (int operations = 0; operations < N; operations++) {
ll m = Random::integer<ll>(1, 1'0000'000'000);
ll a = Random::integer<ll>(0, m);
ll b = Random::integer<ll>(0, m);
t.start();
hash += solveLinearCongruence(a, b, m);
t.stop();
}
if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}
int main() {
stress_test();
if (!sanitize) performance_test();
}
|