From 18c38a7eec613585be3987c3aec302b6fe5ce722 Mon Sep 17 00:00:00 2001 From: teapotd <4314728+teapotd@users.noreply.github.com> Date: Sun, 19 Nov 2023 15:23:28 +0100 Subject: [PATCH] Port few old tests --- src/math/crt.h | 4 +- src/math/fwht.h | 14 +++-- tests/manual/math/berlekamp_massey.cpp | 34 ------------ tests/manual/math/crt.cpp | 45 ---------------- tests/manual/math/fwht.cpp | 47 ---------------- tests/manual/math/modular.cpp | 53 ------------------- tests/own/math/berlekamp_massey.cpp | 32 +++++++++++ tests/own/math/crt.cpp | 32 +++++++++++ tests/own/math/fwht.hpp | 29 ++++++++++ tests/own/math/fwht_and.cpp | 3 ++ tests/own/math/fwht_or.cpp | 3 ++ tests/own/math/fwht_xor.cpp | 3 ++ tests/own/math/modular.cpp | 49 +++++++++++++++++ tests/yosupo/math/bitwise_xor_convolution.cpp | 1 + 14 files changed, 163 insertions(+), 186 deletions(-) delete mode 100644 tests/manual/math/berlekamp_massey.cpp delete mode 100644 tests/manual/math/crt.cpp delete mode 100644 tests/manual/math/fwht.cpp delete mode 100644 tests/manual/math/modular.cpp create mode 100644 tests/own/math/berlekamp_massey.cpp create mode 100644 tests/own/math/crt.cpp create mode 100644 tests/own/math/fwht.hpp create mode 100644 tests/own/math/fwht_and.cpp create mode 100644 tests/own/math/fwht_or.cpp create mode 100644 tests/own/math/fwht_xor.cpp create mode 100644 tests/own/math/modular.cpp diff --git a/src/math/crt.h b/src/math/crt.h index 8b45842..019fcfa 100644 --- a/src/math/crt.h +++ b/src/math/crt.h @@ -1,7 +1,7 @@ #pragma once #include "../template.h" -using Pll = pair; +using pll = pair; ll egcd(ll a, ll b, ll& x, ll& y) { if (!a) return x=0, y=1, b; @@ -17,7 +17,7 @@ ll egcd(ll a, ll b, ll& x, ll& y) { // WARNING: a.x and b.x are assumed to be // in [0;a.y) and [0;b.y) respectively. // Works properly if lcm(a.y, b.y) < 2^63. -Pll crt(Pll a, Pll b) { +pll crt(pll a, pll b) { if (a.y < b.y) swap(a, b); ll x, y, g = egcd(a.y, b.y, x, y); ll c = b.x-a.x, d = b.y/g, p = a.y*d; diff --git a/src/math/fwht.h b/src/math/fwht.h index 91cdbec..6f3f9b3 100644 --- a/src/math/fwht.h +++ b/src/math/fwht.h @@ -14,15 +14,19 @@ void fwht(auto& b) { for (int i = 0; i < sz(b); i += s*2) { rep(j, i, i+s) { auto &x = b[j], &y = b[j+s]; + #if FWHT_XOR //!HIDE tie(x, y) = make_pair(x+y, x-y); // XOR - // x += inv ? -y : y; // AND - // y += inv ? -x : x; // OR + #elif FWHT_AND //!HIDE + x += inv ? -y : y; // AND + #elif FWHT_OR //!HIDE + y += inv ? -x : x; // OR + #endif //!HIDE } } } - - // ONLY FOR XOR: - if (inv) each(e, b) e /= sz(b); + #if FWHT_XOR //!HIDE + if (inv) each(e, b) e /= sz(b); // ONLY XOR + #endif //!HIDE } // Compute convolution of a and b such that diff --git a/tests/manual/math/berlekamp_massey.cpp b/tests/manual/math/berlekamp_massey.cpp deleted file mode 100644 index 3a64139..0000000 --- a/tests/manual/math/berlekamp_massey.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "../../../src/math/berlekamp_massey.h" -#include "../testing.h" - -void check(vector& A) { - vector C = massey(A); - assert(sz(C)*2 <= sz(A)+1); - - vector B(sz(A)); - - rep(i, 0, sz(A)) { - if (i < sz(C)) { - B[i] = A[i]; - } else { - rep(j, 0, sz(C)) { - B[i] = (B[i] + B[i-j-1]*C[j]) % MOD; - } - } - } - - cerr << '\n'; - deb(A); - deb(B); - deb(C); - assert(A == B); -} - -int main() { - rep(i, 0, 10) { - vector data(20); - each(k, data) k = r(0, 20); - check(data); - } - return 0; -} diff --git a/tests/manual/math/crt.cpp b/tests/manual/math/crt.cpp deleted file mode 100644 index 7726733..0000000 --- a/tests/manual/math/crt.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "../../../src/math/crt.h" -#include "../testing.h" - -ll gcd(ll a, ll b) { - while (b) { - a %= b; - swap(a, b); - } - return a; -} - -ll lcm(ll a, ll b) { - return a*b / gcd(a, b); -} - -int main() { - ll maxLcm = 0; - - rep(i, 0, 2000000) { - int d = r(1, 63), e = 63-d; - - ll n = max(ll((1ull<= 0 && ans.x < ans.y); - assert(ans.x%n == a); - assert(ans.x%m == b); - maxLcm = max(maxLcm, lc); - } - - deb(maxLcm); - return 0; -} diff --git a/tests/manual/math/fwht.cpp b/tests/manual/math/fwht.cpp deleted file mode 100644 index b0c3b3e..0000000 --- a/tests/manual/math/fwht.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "../../../src/math/fwht.h" -#include "../testing.h" - -vector naiveXorConv(vector A, vector B) { - vector ret(sz(A)); - rep(i, 0, sz(A)) rep(j, 0, sz(A)) ret[i^j] += A[i]*B[j]; - return ret; -} - -vector naiveOrConv(vector A, vector B) { - vector ret(sz(A)); - rep(i, 0, sz(A)) rep(j, 0, sz(A)) ret[i|j] += A[i]*B[j]; - return ret; -} - -vector naiveAndConv(vector A, vector B) { - vector ret(sz(A)); - rep(i, 0, sz(A)) rep(j, 0, sz(A)) ret[i&j] += A[i]*B[j]; - return ret; -} - -template -void test(F fast, G naive) { - rep(i, 0, 100) { - int p = r(0, 10); - vector A(1< expected = naive(A, B); - fast(A, B); - - if (A != expected) { - deb(A); - deb(expected); - exit(0); - } - } -} - -int main() { - auto fastConv = [](auto& x, auto& y) { bitConv(x, y); }; - test(fastConv, naiveXorConv); - // test(fastConv, naiveAndConv); - // test(fastConv, naiveOrConv); - return 0; -} diff --git a/tests/manual/math/modular.cpp b/tests/manual/math/modular.cpp deleted file mode 100644 index b05ff7c..0000000 --- a/tests/manual/math/modular.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "../../../src/math/modular.h" -#include "../testing.h" - -ll naivePow(ll a, ll p) { - ll ret = 1; - for (int i = 0; i < p; i++) { - ret = (ret*a) % MOD; - } - return ret; -} - -ll gcd(ll a, ll b) { - while (b) { - a %= b; - swap(a, b); - } - return a; -} - -void testExtGcd() { - rep(i, 0, 1000000) { - ll a = r(1, 1e9); - ll b = r(1, 1e9); - ll x, y, d = egcd(a, b, x, y); - assert(d == gcd(a, b)); - assert(x*a + y*b == d); - } -} - -int main() { - testExtGcd(); - assert(Zp(-3243245).x >= 0); - - for (Zp a = 1; a.x <= 3000000; a.x++) { - Zp inv = a.inv(); - assert(inv.x == (Zp(1)/a).x); - assert((a*inv).x == 1); - - for (ll p = 0; p < 10; p++) { - ll naive = naivePow(a.x, p); - assert(naive == a.pow(p).x); - assert(naive == modPow(a.x, p, MOD)); - } - - assert((a * -50).x >= 0); - assert((a - (a + 5)).x >= 0); - assert((a - 100).x >= 0); - } - - Zp test = -5; - deb(test); - return 0; -} diff --git a/tests/own/math/berlekamp_massey.cpp b/tests/own/math/berlekamp_massey.cpp new file mode 100644 index 0000000..53f2c56 --- /dev/null +++ b/tests/own/math/berlekamp_massey.cpp @@ -0,0 +1,32 @@ +#include "../../../src/math/berlekamp_massey.h" +#include "../base_test.hpp" + +void deterministic() { +} + +void fuzz() { + rep(t, 0, 4'000) { + vector A(200); + each(k, A) k = randInt(0, 200); + + auto C = massey(A); + assert(sz(C)*2 <= sz(A)+1); + + vector B(sz(A)); + + rep(i, 0, sz(A)) { + if (i < sz(C)) { + B[i] = A[i]; + } else { + rep(j, 0, sz(C)) { + B[i] = (B[i] + B[i-j-1]*C[j]) % MOD; + } + } + } + + assert(A == B); + } +} + +void benchmark() { +} diff --git a/tests/own/math/crt.cpp b/tests/own/math/crt.cpp new file mode 100644 index 0000000..7dd7010 --- /dev/null +++ b/tests/own/math/crt.cpp @@ -0,0 +1,32 @@ +#include "../../../src/math/crt.h" +#include "../base_test.hpp" + +void deterministic() { +} + +void fuzz() { + rep(i, 0, 2'000'000) { + int d = 1 + i%63, e = 63-d; + ll n = max(ll((1ull<= 0 && got.x < got.y); + assert(got.x%n == a); + assert(got.x%m == b); + } + } +} + +void benchmark() { +} diff --git a/tests/own/math/fwht.hpp b/tests/own/math/fwht.hpp new file mode 100644 index 0000000..2306380 --- /dev/null +++ b/tests/own/math/fwht.hpp @@ -0,0 +1,29 @@ +#include "../../../src/math/fwht.h" +#include "../base_test.hpp" + +vector naiveBitConv(vector A, vector B) { + vector ret(sz(A)); + rep(i, 0, sz(A)) rep(j, 0, sz(A)) { + ret[i FWHT_OP j] += A[i] * B[j]; + } + return ret; +} + +void deterministic() { +} + +void fuzz() { + rep(i, 0, 5'000) { + int p = i%11; + vector A(1<