Skip to content

Commit

Permalink
Port few old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teapotd committed Nov 19, 2023
1 parent 75e5a78 commit 18c38a7
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 186 deletions.
4 changes: 2 additions & 2 deletions src/math/crt.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "../template.h"

using Pll = pair<ll, ll>;
using pll = pair<ll, ll>;

ll egcd(ll a, ll b, ll& x, ll& y) {
if (!a) return x=0, y=1, b;
Expand All @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions src/math/fwht.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 0 additions & 34 deletions tests/manual/math/berlekamp_massey.cpp

This file was deleted.

45 changes: 0 additions & 45 deletions tests/manual/math/crt.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions tests/manual/math/fwht.cpp

This file was deleted.

53 changes: 0 additions & 53 deletions tests/manual/math/modular.cpp

This file was deleted.

32 changes: 32 additions & 0 deletions tests/own/math/berlekamp_massey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "../../../src/math/berlekamp_massey.h"
#include "../base_test.hpp"

void deterministic() {
}

void fuzz() {
rep(t, 0, 4'000) {
vector<ll> A(200);
each(k, A) k = randInt(0, 200);

auto C = massey(A);
assert(sz(C)*2 <= sz(A)+1);

vector<ll> 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() {
}
32 changes: 32 additions & 0 deletions tests/own/math/crt.cpp
Original file line number Diff line number Diff line change
@@ -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<<d)-1) - randInt(1, 1e6), 1ll);
ll m = max(ll((1ull<<e)-1) - randInt(1, 1e6), 1ll);

ll a = randLong(0, n-1);
ll b = randLong(0, m-1);

pll got = crt({a, n}, {b, m});
ll lc = lcm(n, m);

if (got.y == -1) {
assert(a%lc != b%lc);
assert(got.x == -1);
} else {
assert(got.y == lc);
assert(got.x >= 0 && got.x < got.y);
assert(got.x%n == a);
assert(got.x%m == b);
}
}
}

void benchmark() {
}
29 changes: 29 additions & 0 deletions tests/own/math/fwht.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../../../src/math/fwht.h"
#include "../base_test.hpp"

vector<ll> naiveBitConv(vector<ll> A, vector<ll> B) {
vector<ll> 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<ll> A(1<<p), B(1<<p);
each(k, A) k = randInt(-1e5, 1e5);
each(k, B) k = randInt(-1e5, 1e5);

auto expected = naiveBitConv(A, B);
bitConv(A, B);
assert(A == expected);
}
}

void benchmark() {
}
3 changes: 3 additions & 0 deletions tests/own/math/fwht_and.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define FWHT_AND 1
#define FWHT_OP &
#include "fwht.hpp"
3 changes: 3 additions & 0 deletions tests/own/math/fwht_or.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define FWHT_OR 1
#define FWHT_OP |
#include "fwht.hpp"
3 changes: 3 additions & 0 deletions tests/own/math/fwht_xor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define FWHT_XOR 1
#define FWHT_OP ^
#include "fwht.hpp"
49 changes: 49 additions & 0 deletions tests/own/math/modular.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "../../../src/math/modular.h"
#include "../base_test.hpp"

void deterministic() {
assert(Zp().x == 0);
assert(Zp(0).x == 0);
assert(Zp(MOD).x == 0);

for (int i = 1; i <= 500'000; i++) {
for (int j : {i, MOD-i}) {
Zp a = j;
assert(a.x == j);
assert(Zp(-j).x == MOD-j);
assert(Zp(MOD+j).x == j);

Zp naivePow = 1;
for (ll p = 0; p < 10; p++) {
assert(naivePow.x == a.pow(p).x);
assert(naivePow.x == modPow(a.x, p, MOD));
naivePow *= a;
}

Zp inv = a.inv();
assert(inv.x == modInv(j, MOD));
assert(inv.x == (Zp(1)/a).x);
assert((a*inv).x == 1);
}
}

assert((Zp(5)+Zp(-1)).x == 4);
assert((Zp(5)-Zp(-1)).x == 6);
assert((Zp(5)*Zp(5)).x == 25);
assert((Zp(5)*Zp(-5)).x == MOD-25);
assert((Zp(5)/Zp(-5)).x == MOD-1);
assert((-Zp(5)).x == MOD-5);
}

void fuzz() {
rep(i, 0, 500'000) {
ll a = randInt(1, MOD-1);
ll b = randInt(1, MOD-1);
ll x, y, d = egcd(a, b, x, y);
assert(d == gcd(a, b));
assert(x*a + y*b == d);
}
}

void benchmark() {
}
1 change: 1 addition & 0 deletions tests/yosupo/math/bitwise_xor_convolution.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define FWHT_XOR
#include "../../../src/math/modular.h"
#include "../../../src/math/fwht.h"

Expand Down

0 comments on commit 18c38a7

Please sign in to comment.