This documentation is automatically generated by online-judge-tools/verification-helper
#include "string/substring_abundant_string.hpp"#include "other/bit.hpp"
#include "string/run_length.hpp"
// 部分文字列の種類数が最大であるような 01 文字列の構成
// https://qoj.ac/contest/1096/problem/5434
// https://oeis.org/A094913
// https://www.mimuw.edu.pl/~rytter/MYPAPERS/paper.pdf
string substring_abundant_string(ll N) {
ll N0 = N;
N = 1;
while ((1 << N) + (N - 1) < N0) ++N;
string S = [&]() -> string {
if (N == 1) return "01";
if (N == 2) return "00110";
auto SHIFT = [&](string x, string y) -> string {
int n = len(x);
x += x;
FOR(i, n, n + n) {
if (x.substr(i - len(y), len(y)) == y) {
return x.substr(i - n, n);
}
}
return "";
};
auto oplus = [&](string x, string y) -> string {
int n = topbit(len(y));
assert(len(x) == (1 << n) && len(y) == (1 << n));
return x + SHIFT(y, x.substr(len(x) - n, n));
};
auto NOT = [&](string x) -> string {
string y;
for (auto&& s : x) y += (s == '0' ? '1' : '0');
return y;
};
auto PSI = [&](string x) -> string {
int a = 0;
FOR(i, len(x)) {
a ^= (x[i] - '0');
x[i] = ('0' + a);
}
return x;
};
auto NEXT = [&](string x) -> string {
x = PSI(x);
return oplus(x, NOT(x));
};
auto otimes = [&](string x, string y) -> string {
string t;
FOR(topbit(len(x))) t += '0';
x = SHIFT(x, t);
y = SHIFT(y, t);
rotate(x.begin(), x.end() - len(t), x.end());
rotate(y.begin(), y.end() - len(t), y.end());
int n = len(x);
int x0 = 0, x1 = 0, y0 = 0, y1 = 1;
for (auto&& [k, v] : run_length(x)) {
if (k == '0') chmax(x0, v);
if (k == '1') chmax(x1, v);
}
for (auto&& [k, v] : run_length(y)) {
if (k == '0') chmax(y0, v);
if (k == '1') chmax(y1, v);
}
string X, Y;
for (auto&& [k, v] : run_length(x)) {
if (k == '0' && v < x0) {
X += string(v, k);
}
if (k == '0' && v == x0) {
X += string(v - 1, k);
}
if (k == '1' && v < x1) {
X += string(v, k);
}
if (k == '1' && v == x1) {
X += string(v + 1, k);
}
}
for (auto&& [k, v] : run_length(y)) {
if (k == '0' && v < y0) {
Y += string(v, k);
}
if (k == '0' && v == y0) {
Y += string(v + 1, k);
}
if (k == '1' && v < y1) {
Y += string(v, k);
}
if (k == '1' && v == y1) {
Y += string(v - 1, k);
}
}
return X + Y;
};
string x = "0011", y = "0011";
FOR(i, 2, N - 1) {
string t;
FOR(i) t += '1';
x = SHIFT(x, t);
x = NEXT(x);
y = SHIFT(y, t);
y = NOT(NEXT(y));
}
x = otimes(x, y);
FOR(i, N - 1) x += x[i];
return x;
}();
return S.substr(0, N0);
}#line 1 "other/bit.hpp"
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename UINT>
struct all_subset {
static_assert(is_unsigned<UINT>::value);
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
#line 1 "string/run_length.hpp"
template <typename STRING = string>
vc<pair<typename STRING::value_type, ll>> run_length(const STRING& S) {
vc<pair<typename STRING::value_type, ll>> res;
for (auto&& x : S) {
if (res.empty() || res.back().fi != x) {
res.emplace_back(x, 0);
}
res.back().se++;
}
return res;
}
#line 3 "string/substring_abundant_string.hpp"
// 部分文字列の種類数が最大であるような 01 文字列の構成
// https://qoj.ac/contest/1096/problem/5434
// https://oeis.org/A094913
// https://www.mimuw.edu.pl/~rytter/MYPAPERS/paper.pdf
string substring_abundant_string(ll N) {
ll N0 = N;
N = 1;
while ((1 << N) + (N - 1) < N0) ++N;
string S = [&]() -> string {
if (N == 1) return "01";
if (N == 2) return "00110";
auto SHIFT = [&](string x, string y) -> string {
int n = len(x);
x += x;
FOR(i, n, n + n) {
if (x.substr(i - len(y), len(y)) == y) {
return x.substr(i - n, n);
}
}
return "";
};
auto oplus = [&](string x, string y) -> string {
int n = topbit(len(y));
assert(len(x) == (1 << n) && len(y) == (1 << n));
return x + SHIFT(y, x.substr(len(x) - n, n));
};
auto NOT = [&](string x) -> string {
string y;
for (auto&& s : x) y += (s == '0' ? '1' : '0');
return y;
};
auto PSI = [&](string x) -> string {
int a = 0;
FOR(i, len(x)) {
a ^= (x[i] - '0');
x[i] = ('0' + a);
}
return x;
};
auto NEXT = [&](string x) -> string {
x = PSI(x);
return oplus(x, NOT(x));
};
auto otimes = [&](string x, string y) -> string {
string t;
FOR(topbit(len(x))) t += '0';
x = SHIFT(x, t);
y = SHIFT(y, t);
rotate(x.begin(), x.end() - len(t), x.end());
rotate(y.begin(), y.end() - len(t), y.end());
int n = len(x);
int x0 = 0, x1 = 0, y0 = 0, y1 = 1;
for (auto&& [k, v] : run_length(x)) {
if (k == '0') chmax(x0, v);
if (k == '1') chmax(x1, v);
}
for (auto&& [k, v] : run_length(y)) {
if (k == '0') chmax(y0, v);
if (k == '1') chmax(y1, v);
}
string X, Y;
for (auto&& [k, v] : run_length(x)) {
if (k == '0' && v < x0) {
X += string(v, k);
}
if (k == '0' && v == x0) {
X += string(v - 1, k);
}
if (k == '1' && v < x1) {
X += string(v, k);
}
if (k == '1' && v == x1) {
X += string(v + 1, k);
}
}
for (auto&& [k, v] : run_length(y)) {
if (k == '0' && v < y0) {
Y += string(v, k);
}
if (k == '0' && v == y0) {
Y += string(v + 1, k);
}
if (k == '1' && v < y1) {
Y += string(v, k);
}
if (k == '1' && v == y1) {
Y += string(v - 1, k);
}
}
return X + Y;
};
string x = "0011", y = "0011";
FOR(i, 2, N - 1) {
string t;
FOR(i) t += '1';
x = SHIFT(x, t);
x = NEXT(x);
y = SHIFT(y, t);
y = NOT(NEXT(y));
}
x = otimes(x, y);
FOR(i, N - 1) x += x[i];
return x;
}();
return S.substr(0, N0);
}