summaryrefslogtreecommitdiff
path: root/test/math/linearRecurrence.cpp
blob: f0ebe7685ef41cf1753e31f76206c1808441fe37 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "../util.h"
vector<ll> mul(const vector<ll>& a, const vector<ll>& b);
#include <math/linearRecurrence.cpp>

vector<ll> mul(const vector<ll>& a, const vector<ll>& b) {
	return mulSlow(a, b);
}


struct RandomRecurence {
	vector<ll> f, c, cache;
	RandomRecurence(int n) : f(Random::integers<ll>(n, 0, mod)), c(Random::integers<ll>(n, 0, mod)), cache(f) {}

	ll operator()(ll k){
		while (sz(cache) <= k) {
			ll cur = 0;
			for (ll i = 0; i < sz(c); i++) {
				cur += (c[i] * cache[sz(cache) - i - 1]) % mod;
			}
			cur %= mod;
			cache.push_back(cur);
		}
		return cache[k];
	}
};

void stress_test() {
	int queries = 0;
	for (int i = 0; i < 10'000; i++) {
		int n = Random::integer<int>(1, 10);
		RandomRecurence f(n);
		for (int j = 0; j < 100; j++) {
			ll k = Random::integer<ll>(0, 1000);

			ll got = kthTerm(f.f, f.c, k);
			ll expected = f(k);

			if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
			queries++;
		}
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 1'000;
void performance_test() {
	timer t;
	RandomRecurence f(N);
	t.start();
	hash_t hash = kthTerm(f.f, f.c, 1e18);
	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();
	performance_test();
}