#include "mushrooms.h"
#include <bits/stdc++.h>
#define X 44
using namespace std;
int arr[20005];
vector<int> A, B;
int count_mushrooms(int n) {
std::vector<int> m;
if(n < 5 * X) {
for(int i = 1; i < n; ++i) {
m.clear(); m.push_back(i);
m.push_back(i - 1);
if(use_machine(m) == 1) arr[i] = !arr[i - 1];
else arr[i] = arr[i - 1];
}
int ans = 0;
for(int i = 0; i < n; ++i) if(arr[i] == 0) ++ans;
return ans;
}
for(int i = 1; i <= 2; ++i) {
m.clear(); m.push_back(i - 1); m.push_back(i);
if(use_machine(m) == 1) arr[i] = !arr[i - 1];
else arr[i] = arr[i - 1];
}
int n1, n2;
if(arr[0] == arr[1]) n1 = 0, n2 = 1;
else if(arr[0] == arr[2]) n1 = 0, n2 = 2;
else n1 = 1, n2 = 2;
m.clear(); m.push_back(n1); m.push_back(3);
m.push_back(n2); m.push_back(4);
int tmp = use_machine(m);
if(tmp == 0) arr[3] = arr[4] = arr[n1];
if(tmp == 1) arr[3] = arr[n1], arr[4] = !arr[n1];
if(tmp == 2) arr[3] = !arr[n1], arr[4] = arr[n1];
if(tmp == 3) arr[3] = arr[4] = !arr[n1];
for(int i = 0; i < 5; ++i) {
if(arr[i] == 0) A.push_back(i);
else B.push_back(i);
}
for(int i = 5; i < 5 * X; i += 5) {
if(A.size() >= 3) {
m.clear();
m.push_back(A[0]); m.push_back(i);
m.push_back(A[1]); m.push_back(i + 1);
m.push_back(A[2]); m.push_back(i + 2);
int tmp = use_machine(m);
if(tmp == 0) arr[i] = arr[i + 1] = arr[i + 2] = 0;
if(tmp == 1) arr[i] = arr[i + 1] = 0, arr[i + 2] = 1;
if(tmp == 4) arr[i] = arr[i + 1] = 1, arr[i + 2] = 0;
if(tmp == 5) arr[i] = arr[i + 1] = arr[i + 2] = 1;
if(tmp == 2) arr[i + 2] = 0;
if(tmp == 3) arr[i + 2] = 1;
if(tmp == 2 || tmp == 3) {
if(B.size() >= 2) {
m.clear();
m.push_back(B[0]); m.push_back(i); m.push_back(B[1]);
m.push_back(A[0]); m.push_back(i + 1); m.push_back(A[1]);
m.push_back(i + 3); m.push_back(A[2]); m.push_back(i + 4);
int tmp = use_machine(m); --tmp;
if(tmp < 4) arr[i] = 1, arr[i + 1] = 0;
else arr[i] = 0, arr[i + 1] = 1;
tmp %= 4;
if(tmp == 0) arr[i + 3] = arr[i + 4] = 0;
if(tmp == 1) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 2) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 1;
}
else {
m.clear(); m.push_back(A[0]); m.push_back(i);
int tmp = use_machine(m);
if(tmp == 1) arr[i] = 1, arr[i + 1] = 0;
else arr[i] = 0, arr[i + 1] = 1;
m.clear();
m.push_back(A[0]); m.push_back(i + 3);
m.push_back(A[1]); m.push_back(i + 4);
tmp = use_machine(m);
if(tmp == 0) arr[i + 3] = arr[i + 4] = 0;
if(tmp == 1) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 2) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 1;
}
}
else {
m.clear();
m.push_back(A[0]); m.push_back(i + 3);
m.push_back(A[1]); m.push_back(i + 4);
int tmp = use_machine(m);
if(tmp == 0) arr[i + 3] = arr[i + 4] = 0;
if(tmp == 1) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 2) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 1;
}
}
else {
m.clear();
m.push_back(B[0]); m.push_back(i);
m.push_back(B[1]); m.push_back(i + 1);
m.push_back(B[2]); m.push_back(i + 2);
int tmp = use_machine(m);
if(tmp == 0) arr[i] = arr[i + 1] = arr[i + 2] = 1;
if(tmp == 1) arr[i] = arr[i + 1] = 1, arr[i + 2] = 0;
if(tmp == 4) arr[i] = arr[i + 1] = 0, arr[i + 2] = 1;
if(tmp == 5) arr[i] = arr[i + 1] = arr[i + 2] = 0;
if(tmp == 2) arr[i + 2] = 1;
if(tmp == 3) arr[i + 2] = 0;
if(tmp == 2 || tmp == 3) {
if(A.size() >= 2) {
m.clear();
m.push_back(A[0]); m.push_back(i); m.push_back(A[1]);
m.push_back(B[0]); m.push_back(i + 1); m.push_back(B[1]);
m.push_back(i + 3); m.push_back(B[2]); m.push_back(i + 4);
int tmp = use_machine(m); --tmp;
if(tmp < 4) arr[i] = 0, arr[i + 1] = 1;
else arr[i] = 1, arr[i + 1] = 0;
tmp %= 4;
if(tmp == 0) arr[i + 3] = arr[i + 4] = 1;
if(tmp == 1) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 2) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 0;
}
else {
m.clear(); m.push_back(B[0]); m.push_back(i);
int tmp = use_machine(m);
if(tmp == 1) arr[i] = 0, arr[i + 1] = 1;
else arr[i] = 1, arr[i + 1] = 0;
m.clear();
m.push_back(B[0]); m.push_back(i + 3);
m.push_back(B[1]); m.push_back(i + 4);
tmp = use_machine(m);
if(tmp == 0) arr[i + 3] = arr[i + 4] = 1;
if(tmp == 1) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 2) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 0;
}
}
else {
m.clear();
m.push_back(B[0]); m.push_back(i + 3);
m.push_back(B[1]); m.push_back(i + 4);
int tmp = use_machine(m);
if(tmp == 0) arr[i + 3] = arr[i + 4] = 1;
if(tmp == 1) arr[i + 3] = 1, arr[i + 4] = 0;
if(tmp == 2) arr[i + 3] = 0, arr[i + 4] = 1;
if(tmp == 3) arr[i + 3] = arr[i + 4] = 0;
}
}
for(int j = i; j < i + 5; ++j) {
if(arr[j] == 0) A.push_back(j);
else B.push_back(j);
}
}
int ans = 0;
for(int i = 0; i < 5 * X; ++i) if(arr[i] == 0) ++ans;
int nxt = 0;
for(int i = 5 * X; i < n; i += nxt) {
if(i + (int)max(A.size(), B.size()) <= n) {
m.clear();
for(int j = i; j < i + (int)max(A.size(), B.size()); ++j) {
m.push_back(j);
if(A.size() >= B.size()) m.push_back(A[j - i]);
else m.push_back(B[j - i]);
}
int tmp = use_machine(m);
int cnt = (tmp % 2) + (tmp / 2);
if(A.size() >= B.size()) ans += (int)A.size() - cnt;
else ans += cnt;
nxt = (int)max(A.size(), B.size());
if(tmp % 2 == 1) {
if(A.size() >= B.size()) B.push_back(i);
else A.push_back(i);
}
else {
if(A.size() >= B.size()) A.push_back(i);
else B.push_back(i);
}
}
else {
m.clear();
for(int j = i; j < n; ++j) {
m.push_back(j);
if(A.size() >= B.size()) m.push_back(A[j - i]);
else m.push_back(B[j - i]);
}
int tmp = use_machine(m);
int cnt = (tmp % 2) + (tmp / 2);
if(A.size() >= B.size()) ans += n - i - cnt;
else ans += cnt;
break;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
4 ms |
320 KB |
Output is correct |
8 |
Correct |
4 ms |
208 KB |
Output is correct |
9 |
Correct |
4 ms |
208 KB |
Output is correct |
10 |
Correct |
5 ms |
208 KB |
Output is correct |
11 |
Correct |
5 ms |
208 KB |
Output is correct |
12 |
Correct |
5 ms |
208 KB |
Output is correct |
13 |
Correct |
6 ms |
324 KB |
Output is correct |
14 |
Correct |
3 ms |
208 KB |
Output is correct |
15 |
Correct |
5 ms |
208 KB |
Output is correct |
16 |
Correct |
6 ms |
372 KB |
Output is correct |
17 |
Correct |
3 ms |
208 KB |
Output is correct |
18 |
Correct |
6 ms |
320 KB |
Output is correct |
19 |
Correct |
6 ms |
208 KB |
Output is correct |
20 |
Correct |
5 ms |
208 KB |
Output is correct |
21 |
Correct |
6 ms |
208 KB |
Output is correct |
22 |
Correct |
5 ms |
320 KB |
Output is correct |
23 |
Correct |
4 ms |
208 KB |
Output is correct |
24 |
Correct |
3 ms |
256 KB |
Output is correct |
25 |
Correct |
5 ms |
208 KB |
Output is correct |
26 |
Correct |
5 ms |
324 KB |
Output is correct |
27 |
Correct |
7 ms |
208 KB |
Output is correct |
28 |
Correct |
7 ms |
208 KB |
Output is correct |
29 |
Correct |
5 ms |
308 KB |
Output is correct |
30 |
Correct |
5 ms |
208 KB |
Output is correct |
31 |
Correct |
4 ms |
320 KB |
Output is correct |
32 |
Correct |
5 ms |
312 KB |
Output is correct |
33 |
Correct |
5 ms |
308 KB |
Output is correct |
34 |
Correct |
5 ms |
208 KB |
Output is correct |
35 |
Correct |
4 ms |
208 KB |
Output is correct |
36 |
Correct |
6 ms |
208 KB |
Output is correct |
37 |
Correct |
6 ms |
208 KB |
Output is correct |
38 |
Correct |
5 ms |
208 KB |
Output is correct |
39 |
Correct |
5 ms |
208 KB |
Output is correct |
40 |
Correct |
7 ms |
312 KB |
Output is correct |
41 |
Correct |
5 ms |
208 KB |
Output is correct |
42 |
Correct |
6 ms |
208 KB |
Output is correct |
43 |
Correct |
5 ms |
208 KB |
Output is correct |
44 |
Correct |
5 ms |
312 KB |
Output is correct |
45 |
Correct |
5 ms |
208 KB |
Output is correct |
46 |
Correct |
4 ms |
208 KB |
Output is correct |
47 |
Correct |
5 ms |
208 KB |
Output is correct |
48 |
Correct |
5 ms |
208 KB |
Output is correct |
49 |
Correct |
6 ms |
320 KB |
Output is correct |
50 |
Correct |
4 ms |
208 KB |
Output is correct |
51 |
Correct |
5 ms |
320 KB |
Output is correct |
52 |
Correct |
5 ms |
208 KB |
Output is correct |
53 |
Correct |
6 ms |
316 KB |
Output is correct |
54 |
Correct |
6 ms |
208 KB |
Output is correct |
55 |
Correct |
5 ms |
256 KB |
Output is correct |
56 |
Correct |
5 ms |
208 KB |
Output is correct |
57 |
Correct |
5 ms |
208 KB |
Output is correct |
58 |
Correct |
7 ms |
208 KB |
Output is correct |
59 |
Correct |
6 ms |
208 KB |
Output is correct |
60 |
Correct |
5 ms |
208 KB |
Output is correct |
61 |
Correct |
5 ms |
208 KB |
Output is correct |
62 |
Correct |
0 ms |
208 KB |
Output is correct |
63 |
Correct |
0 ms |
208 KB |
Output is correct |
64 |
Correct |
0 ms |
208 KB |
Output is correct |
65 |
Correct |
0 ms |
208 KB |
Output is correct |
66 |
Correct |
0 ms |
208 KB |
Output is correct |
67 |
Correct |
1 ms |
276 KB |
Output is correct |
68 |
Correct |
0 ms |
208 KB |
Output is correct |
69 |
Correct |
0 ms |
208 KB |
Output is correct |
70 |
Correct |
0 ms |
208 KB |
Output is correct |
71 |
Correct |
0 ms |
208 KB |
Output is correct |
72 |
Correct |
0 ms |
208 KB |
Output is correct |
73 |
Correct |
0 ms |
208 KB |
Output is correct |
74 |
Correct |
0 ms |
208 KB |
Output is correct |
75 |
Correct |
0 ms |
208 KB |
Output is correct |
76 |
Correct |
1 ms |
292 KB |
Output is correct |
77 |
Correct |
0 ms |
208 KB |
Output is correct |
78 |
Correct |
0 ms |
208 KB |
Output is correct |
79 |
Correct |
0 ms |
208 KB |
Output is correct |
80 |
Correct |
0 ms |
208 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
0 ms |
208 KB |
Output is correct |
83 |
Correct |
0 ms |
208 KB |
Output is correct |
84 |
Correct |
0 ms |
208 KB |
Output is correct |
85 |
Correct |
0 ms |
208 KB |
Output is correct |
86 |
Correct |
0 ms |
208 KB |
Output is correct |
87 |
Correct |
0 ms |
208 KB |
Output is correct |
88 |
Correct |
0 ms |
208 KB |
Output is correct |
89 |
Correct |
0 ms |
208 KB |
Output is correct |
90 |
Correct |
1 ms |
208 KB |
Output is correct |
91 |
Correct |
0 ms |
208 KB |
Output is correct |