QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#203461 | #7508. Fast Debugger | ucup-team004# | AC ✓ | 53ms | 22136kb | C++20 | 5.6kb | 2023-10-06 17:35:59 | 2023-10-06 17:35:59 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using TransBit = std::array<int, 16>;
using Trans = std::array<TransBit, 8>;
Trans identity() {
Trans t;
for (int i = 0; i < 8; i++) {
std::iota(t[i].begin(), t[i].end(), 0);
}
return t;
}
Trans operator*(const Trans &a, const Trans &b) {
Trans c;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 16; j++) {
c[i][j] = b[i][a[i][j]];
}
}
return c;
}
Trans get(std::string o, int x, int y) {
Trans t;
for (int i = 0; i < 8; i++) {
for (int a = 0; a < 16; a++) {
int vx = a >> x & 1;
int vy = a >> y & 1;
int b = a ^ (vx << x);
int v;
if (o == "or") {
v = vx | vy;
} else if (o == "and") {
v = vx & vy;
} else {
v = vx ^ vy;
}
b ^= v << x;
t[i][a] = b;
}
}
return t;
}
Trans geti(std::string o, int x, int y) {
Trans t;
for (int i = 0; i < 8; i++) {
for (int a = 0; a < 16; a++) {
int vx = a >> x & 1;
int vy = y >> i & 1;
int b = a ^ (vx << x);
int v;
if (o == "ori") {
v = vx | vy;
} else if (o == "andi") {
v = vx & vy;
} else {
v = vx ^ vy;
}
b ^= v << x;
t[i][a] = b;
}
}
return t;
}
Trans power(Trans a, int b) {
Trans res = identity();
for (; b; b /= 2, a = a * a) {
if (b % 2) {
res = res * a;
}
}
return res;
}
std::array<int, 4> trans(std::array<int, 4> a, Trans t) {
std::array<int, 4> ans{};
for (int i = 0; i < 8; i++) {
int mask = 0;
for (int j = 0; j < 4; j++) {
mask |= (a[j] >> i & 1) << j;
}
mask = t[i][mask];
for (int j = 0; j < 4; j++) {
ans[j] |= (mask >> j & 1) << i;
}
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
constexpr int inf = 1E9 + 1;
std::vector<int> path{0};
std::vector<Trans> val{identity()};
std::vector<int> rep{1};
std::vector<std::vector<int>> adj(1);
std::vector<int> cnt{0};
int tot = 1;
for (int i = 0; i < n; i++) {
std::string ins;
std::cin >> ins;
if (ins == "repeat") {
int m;
std::cin >> m;
path.push_back(tot++);
val.push_back(identity());
rep.push_back(m);
adj.push_back({});
cnt.push_back(0);
} else if (ins == "end") {
int x = path.back();
path.pop_back();
int y = path.back();
adj[y].push_back(x);
val[y] = val[y] * power(val[x], rep[x]);
cnt[y] = std::min(1LL * inf, cnt[y] + 1LL * cnt[x] * rep[x]);
} else if (ins.back() == 'i') {
std::string x;
std::cin >> x;
int y;
std::cin >> y;
int u = path.back();
int v = tot++;
val.push_back(geti(ins, x[0] - 'a', y));
rep.push_back(1);
adj.push_back({});
cnt.push_back(1);
adj[u].push_back(v);
val[u] = val[u] * val[v];
cnt[u] = std::min(inf, cnt[u] + 1);
} else {
std::string x, y;
std::cin >> x >> y;
int u = path.back();
int v = tot++;
val.push_back(get(ins, x[0] - 'a', y[0] - 'a'));
rep.push_back(1);
adj.push_back({});
cnt.push_back(1);
adj[u].push_back(v);
val[u] = val[u] * val[v];
cnt[u] = std::min(inf, cnt[u] + 1);
}
}
auto dfs = [&](auto self, int x) -> void {
std::vector<int> tmp;
int u = x, i = 0;
while (i < adj[u].size()) {
int y = adj[u][i];
if (cnt[y] < inf) {
tmp.push_back(y);
i++;
} else {
u = y, i = 0;
}
}
adj[x] = std::move(tmp);
for (auto y : adj[x]) {
self(self, y);
}
};
dfs(dfs, 0);
std::vector<std::vector<int>> prec(tot);
std::vector<std::vector<Trans>> prev(tot);
for (int i = 0; i < tot; i++) {
prec[i].resize(adj[i].size() + 1);
prev[i].resize(adj[i].size() + 1);
prev[i][0] = identity();
for (int j = 0; j < adj[i].size(); j++) {
int x = adj[i][j];
prec[i][j + 1] = std::min(1LL * inf, prec[i][j] + 1LL * rep[x] * cnt[x]);
prev[i][j + 1] = prev[i][j] * power(val[x], rep[x]);
}
}
for (int i = 0; i < q; i++) {
int k;
std::cin >> k;
std::array<int, 4> v;
for (int j = 0; j < 4; j++) {
std::cin >> v[j];
}
int x = 0;
while (k > 0) {
int j = std::upper_bound(prec[x].begin(), prec[x].end(), k) - prec[x].begin() - 1;
k -= prec[x][j];
v = trans(v, prev[x][j]);
if (k == 0) {
break;
}
x = adj[x][j];
v = trans(v, power(val[x], k / cnt[x]));
k %= cnt[x];
}
for (int j = 0; j < 4; j++) {
std::cout << v[j] << " \n"[j == 3];
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
input:
6 2 repeat 5 xor ax bx xori ax 3 and cx ax xor cx dx end 10 1 2 4 3 8 4 1 2 3
output:
0 2 2 3 4 1 3 3
result:
ok 8 numbers
Test #2:
score: 0
Accepted
time: 42ms
memory: 20592kb
input:
11982 10000 repeat 2 repeat 2 andi bx 201 xori cx 241 repeat 4 xor cx bx xor ax dx repeat 8 or dx bx xori dx 22 end repeat 2 xor dx bx xor dx bx repeat 7 xor bx bx or cx dx and bx cx end andi dx 33 xori ax 179 xori bx 56 end xori dx 63 xori cx 91 xori dx 228 or cx dx repeat 6 xor cx dx xori bx 198 x...
output:
24 195 0 75 0 195 0 245 199 193 6 13 252 73 245 67 0 0 0 0 51 0 0 245 99 26 0 196 51 195 0 75 247 253 247 37 119 198 119 186 235 235 44 36 4 0 181 0 76 228 253 0 51 245 245 33 119 119 176 0 198 198 1 11 199 93 154 3 106 106 152 21 93 92 42 3 236 236 247 147 198 198 204 135 0 51 0 75 119 118 10 151 1...
result:
ok 40000 numbers
Test #3:
score: 0
Accepted
time: 21ms
memory: 20648kb
input:
11978 10000 repeat 3 repeat 3 xor ax dx xori cx 74 repeat 2 repeat 2 repeat 10 xor bx dx xori bx 167 xor dx bx and cx bx end xori cx 140 xor bx cx repeat 5 repeat 2 xor ax cx xor ax cx xor bx ax repeat 2 xor bx cx xori bx 237 xori ax 68 xor cx cx and cx bx xori ax 225 end xor dx dx repeat 5 repeat 5...
output:
141 144 167 0 250 230 51 0 243 168 87 0 195 106 164 164 151 138 51 151 94 34 164 94 245 207 174 0 59 152 164 164 219 239 54 147 49 1 27 0 186 96 30 0 70 15 143 0 43 70 0 0 46 207 138 138 150 146 164 50 114 180 164 114 19 155 164 164 69 249 253 147 236 100 5 72 202 160 184 157 192 90 234 0 246 164 78...
result:
ok 40000 numbers
Test #4:
score: 0
Accepted
time: 53ms
memory: 21996kb
input:
11981 10000 repeat 5 repeat 4 or cx ax repeat 2 xor ax ax repeat 6 xori dx 75 xor cx dx ori ax 200 xori ax 90 and cx bx xori bx 10 end repeat 6 ori dx 63 xor bx dx end xor cx cx xori dx 157 end xori cx 137 xor bx dx repeat 5 repeat 3 xori bx 43 xor dx dx xor cx ax xori ax 116 xori bx 239 xori dx 87 ...
output:
103 34 24 11 0 102 129 2 103 34 24 108 142 108 142 142 253 34 253 201 0 35 253 0 20 111 20 2 71 34 95 0 139 109 139 2 253 34 253 201 35 50 20 209 0 102 133 2 135 49 171 184 0 35 253 2 0 34 205 142 98 239 205 145 2 103 0 2 117 34 13 68 242 235 20 17 165 34 24 210 117 34 192 123 16 160 8 134 18 0 167 ...
result:
ok 40000 numbers
Test #5:
score: 0
Accepted
time: 46ms
memory: 22136kb
input:
11986 10000 repeat 5 and ax cx repeat 5 xori ax 116 xor bx dx xori bx 66 xori bx 206 repeat 2 xor bx bx xor bx dx xor cx bx end end repeat 5 xor ax cx xor cx bx xori bx 22 repeat 4 repeat 7 or ax dx xor cx bx end repeat 5 xori dx 24 or dx ax repeat 3 repeat 9 xori cx 249 xori ax 168 xor dx cx andi a...
output:
0 89 9 0 0 89 25 0 0 155 229 199 0 89 25 230 0 89 25 0 0 89 9 0 0 89 9 0 0 89 25 0 78 224 9 7 209 148 76 154 0 89 9 0 0 126 230 229 0 126 229 196 0 255 230 230 0 89 9 0 0 89 139 0 0 89 9 230 0 89 25 0 0 89 9 0 0 89 9 0 0 89 9 191 0 89 25 0 0 126 230 196 0 89 9 0 0 89 9 0 78 10 248 84 0 89 9 0 0 0 25...
result:
ok 40000 numbers