#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
vector<int> A;
int N;
int iterate = 0;
int use_machine(vector<int> x) {
iterate++;
int ret = 0;
for (int i = 1; i < (int) x.size(); i++) {
assert(0 <= x[i] && x[i] < N);
assert(0 <= x[i - 1] && x[i - 1] < N);
ret += A[x[i]] != A[x[i - 1]];
}
return ret;
}
const int magic = 84;
const int bound = 226;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// generate random number between l, r : uniform_int_distribution<long long>(l, r)(rng)
// random shuffle : shuffle(.begin(), .end(), rng)
int count_mushrooms(int n) {
if (n <= bound + 1) {
int ret = 1;
for (int i = 1; i < n; i++) {
ret += use_machine({0, i}) ^ 1;
}
return ret;
}
vector<int> a(n, -1);
a[0] = 0;
vector<int> zeros = {0};
vector<int> ones;
int cnt = 1;
while (max(zeros.size(), ones.size()) < magic) {
vector<int> ids = zeros.size() >= ones.size() ? zeros : ones;
int memo = cnt;
int L = cnt, R = cnt + min(ids.size(), magic - ids.size()) - 1;
cnt = R + 1;
int bit = ids == ones;
vector<int> cur;
for (int i = L; i <= R; i++) cur.push_back(i);
shuffle(cur.begin(), cur.end(), rng);
vector<int> que;
for (int i = 0; i < (int) cur.size(); i++) {
que.push_back(ids[i]);
que.push_back(cur[i]);
}
int sum = use_machine(que);
a[cur.back()] = bit ^ (sum & 1);
sum -= a[cur.back()] ^ bit;
sum >>= 1;
cur.pop_back();
sum = bit == 1 ? cur.size() - sum : sum;
function<void(vector<int>, int)> solve = [&](vector<int> cur, int sum) {
assert(sum >= 0 && sum <= (int) cur.size());
if (sum == 0) {
for (int i : cur) a[i] = 0;
return;
}
if (sum == (int) cur.size()) {
for (int i : cur) a[i] = 1;
return;
}
assert(cur.size() >= 2);
vector<int> Lq, Rq;
int mid = cur.size() / 2;
for (int i = 0; i < (int) cur.size(); i++) {
if (i < mid) Lq.push_back(cur[i]);
else Rq.push_back(cur[i]);
}
vector<int> p;
for (int i = 0; i < (int) Lq.size(); i++) {
p.push_back(ids[i]);
p.push_back(Lq[i]);
}
p.push_back(ids[Lq.size()]);
p.push_back(cnt++);
int Lsum = use_machine(p);
a[cnt - 1] = bit ^ (Lsum & 1);
Lsum -= a[cnt - 1] ^ bit;
Lsum >>= 1;
Lsum = bit == 1 ? Lq.size() - Lsum : Lsum;
solve(Lq, Lsum);
solve(Rq, sum - Lsum);
};
solve(cur, sum);
for (int i = memo; i < cnt; i++) {
assert(a[i] == A[i]);
if (a[i] == 0) zeros.push_back(i);
else ones.push_back(i);
}
}
int ret = 0;
ret += zeros.size();
while (cnt < n) {
int len = min(n - cnt, (int) max(zeros.size(), ones.size()));
vector<int> ids = zeros.size() >= ones.size() ? zeros : ones;
vector<int> que;
for (int i = 0; i < len; i++) {
que.push_back(ids[i]);
que.push_back(cnt++);
}
int st = use_machine(que);
if (ids == ones) {
a[cnt - 1] = 1 ^ (st & 1);
ret += a[cnt - 1] == 0;
st -= a[cnt - 1] == 0;
if (a[cnt - 1] == 0) zeros.push_back(cnt - 1);
else ones.push_back(cnt - 1);
assert(st % 2 == 0);
ret += st >> 1;
} else {
a[cnt - 1] = 0 ^ (st & 1);
ret += a[cnt - 1] == 0;
st -= a[cnt - 1] == 1;
if (a[cnt - 1] == 0) zeros.push_back(cnt - 1);
else ones.push_back(cnt - 1);
assert(st % 2 == 0);
ret += (len - 1) - (st >> 1);
}
}
return ret;
}
int main() {
N = 20000;
A.resize(N);
for (int i = 1; i < N; i++) A[i] = uniform_int_distribution<long long>(0, 1)(rng);
debug(count(A.begin(), A.end(), 0));
int ret = count_mushrooms(N);
debug(ret)
assert(ret == count(A.begin(), A.end(), 0));
debug(iterate) debug(bound)
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccfgV9d5.o: in function `use_machine(std::vector<int, std::allocator<int> >)':
stub.cpp:(.text+0x210): multiple definition of `use_machine(std::vector<int, std::allocator<int> >)'; /tmp/ccuvTjJ2.o:mushrooms.cpp:(.text+0x680): first defined here
/usr/bin/ld: /tmp/ccfgV9d5.o: in function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccuvTjJ2.o:mushrooms.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status