Skip to content

Commit

Permalink
fix template
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaanNyaan committed May 3, 2024
1 parent 915a6bd commit 0b6ecbc
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 29 deletions.
6 changes: 3 additions & 3 deletions fps/multivariate-fps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ struct MultivariateFormalPowerSeries {
template <typename Head, typename... Tail>
long long _id(int i, Head&& head, Tail&&... tail) {
assert(i < (int)base.size() && (int)head < base[i]);
return head + _id(i + 1, forward<Tail>(tail)...) * base[i];
return head + _id(i + 1, std::forward<Tail>(tail)...) * base[i];
}
template <typename... Args>
long long id(Args&&... args) {
return _id(0, forward<Args>(args)...);
return _id(0, std::forward<Args>(args)...);
}
template <typename... Args>
mint& operator()(Args&&... args) {
return f[id(forward<Args>(args)...)];
return f[id(std::forward<Args>(args)...)];
}

mfps& operator+=(const mfps& rhs) {
Expand Down
3 changes: 2 additions & 1 deletion graph/graph-template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,

// Input of Edges
template <typename T>
Edges<T> esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) {
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
Expand Down
4 changes: 2 additions & 2 deletions misc/fastio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ void wt() {}
template <typename Head, typename... Tail>
void wt(const Head& head, const Tail&... tail) {
single_write(head);
wt(forward<const Tail>(tail)...);
wt(std::forward<const Tail>(tail)...);
}
template <typename... Args>
void wtn(const Args&... x) {
wt(forward<const Args>(x)...);
wt(std::forward<const Args>(x)...);
wt('\n');
}

Expand Down
18 changes: 0 additions & 18 deletions misc/fixpoint.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion template/bitop.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Nyaan {
__attribute__((target("popcnt"))) inline int popcnt(const u64 &a) {
return _mm_popcnt_u64(a);
return __builtin_popcountll(a);
}
inline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
inline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
Expand Down
2 changes: 1 addition & 1 deletion template/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void trace(Head&& head, Tail&&... tail) {
cerr << " ";
dump(head);
if (sizeof...(tail) != 0) cerr << ",";
trace(forward<Tail>(tail)...);
trace(std::forward<Tail>(tail)...);
}

} // namespace DebugImpl
Expand Down
42 changes: 40 additions & 2 deletions template/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ T mkrev(const T &v) {
}

template <typename T>
bool nxp(vector<T> &v) {
bool nxp(T &v) {
return next_permutation(begin(v), end(v));
}

Expand Down Expand Up @@ -192,8 +192,46 @@ T Power(T a, long long n, const T &I, const function<void(T &)> &f) {
}
// T : 整数型のときはオーバーフローに注意する
template <typename T>
T Power(T a, long long n, const T &I) {
T Power(T a, long long n, const T &I = T{1}) {
return Power(a, n, I, function<void(T &)>{[](T &) -> void {}});
}

template <typename T>
T Rev(const T &v) {
T res = v;
reverse(begin(res), end(res));
return res;
}

template <typename T>
vector<T> Transpose(const vector<T> &v) {
using U = typename T::value_type;
if(v.empty()) return {};
int H = v.size(), W = v[0].size();
vector res(W, T(H, U{}));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
res[j][i] = v[i][j];
}
}
return res;
}

template <typename T>
vector<T> Rotate(const vector<T> &v, int clockwise = true) {
using U = typename T::value_type;
int H = v.size(), W = v[0].size();
vector res(W, T(H, U{}));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (clockwise) {
res[W - 1 - j][i] = v[i][j];
} else {
res[j][H - 1 - i] = v[i][j];
}
}
}
return res;
}

} // namespace Nyaan
54 changes: 53 additions & 1 deletion verify/verify-unit-test/template.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
#include "../../template/template.hpp"
//
#include "../../matrix/matrix-fast.hpp"
#include "../../modint/montgomery-modint.hpp"
//
#include "../../misc/rng.hpp"
Expand Down Expand Up @@ -63,7 +64,6 @@ void verify_bitop() {
}

// gbit, sbit
// sbitは現在バグり中
assert(gbit(5, 0) == 1);
assert(gbit(5, 1) == 0);
assert(gbit(5, 2) == 1);
Expand All @@ -80,6 +80,10 @@ void verify_bitop() {
assert(((x >> i) & 1) == 0u && "sbit");
sbit(x, i, g);
assert(((x >> i) & 1) == g && "sbit");
sbit(x, i, 1);
assert(((x >> i) & 1) == 1u && "sbit");
sbit(x, i, g);
assert(((x >> i) & 1) == g && "sbit");
}
}

Expand Down Expand Up @@ -306,6 +310,24 @@ void verify_util() {
// Power
{
using mint = LazyMontgomeryModInt<998244353>;

{
assert(Power(3, 5) == 243);
assert(Power(10LL, 18) == TEN(18));
assert(Power(mint{2}, 30) == mint{2}.pow(30));
}

{
using Mat = Matrix<mint, 2, 2>;
Mat m;
m[0][0] = 2, m[1][1] = 1;
Mat m2 = Power(m, 10, Mat::I());
assert(m2[0][0] == 1024);
assert(m2[0][1] == 0);
assert(m2[1][0] == 0);
assert(m2[1][1] == 1);
}

for (ll x = 1; x <= 100; x++) {
i128 y = 1;
for (ll e = 0; e <= 64; e++) {
Expand All @@ -320,12 +342,42 @@ void verify_util() {
}
}
}

// Rev
{
vector a{1, 2, 3};
vector a2{3, 2, 1};
assert(Rev(a) == a2);

string b = "abc";
string b2 = "cba"s;
assert(Rev(b) == b2);

vector c{vector{1}, vector{1, 2}};
vector c2{vector{1, 2}, vector{1}};
assert(Rev(c) == c2);
}

// Transpose, Rotate
{
vector b{"abc"s, "def"s};

vector b2{"ad"s, "be"s, "cf"s};
vector b3{"cf"s, "be"s, "ad"s};
vector b4{"da"s, "eb"s, "fc"s};

assert(b2 == Transpose(b));
assert(b3 == Rotate(b));
assert(b4 == Rotate(b, 0));
}
}

void Nyaan::solve() {
verify_bitop();
verify_util();

cerr << "OK" << endl;

int a, b;
cin >> a >> b;
cout << a + b << endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_mod_2"
//
#include "../../template/template.hpp"
//
#include "../../matrix/f2_matrix.hpp"
using namespace Nyaan;

void q() {
ini(N);
F2_Matrix<4096, 4096> m(N, N);
rep(i, N) {
ins(S);
m[i] = bitset<4096>{Rev(S)};
}
int d = m.determinant();
out(d);
}

void Nyaan::solve() {
int t = 1;
// in(t);
while (t--) q();
}
28 changes: 28 additions & 0 deletions verify/verify-yosupo-math/yosupo-matrix-product-mod-2.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product_mod_2"
//
#include "../../template/template.hpp"
//
#include "../../matrix/f2_matrix.hpp"
using namespace Nyaan;

void q() {
ini(N, M, K);
using Mat = F2_Matrix<4096, 4096>;
Mat a(N, M), b(M, K);
rep(i, N) {
ins(S);
a.A[i] = bitset<4096>{Rev(S)};
}
rep(i, M) {
ins(S);
b.A[i] = bitset<4096>{Rev(S)};
}
Mat c = a * b;
rep(i, N) out(Rev(c.A[i].to_string()).substr(0, K));
}

void Nyaan::solve() {
int t = 1;
// in(t);
while (t--) q();
}

0 comments on commit 0b6ecbc

Please sign in to comment.