| #include <bits/stdc++.h> |
| using namespace std; |
|
|
| struct Instr { |
| bool halt; |
| int a, x, b, y; |
| }; |
|
|
| int main() { |
| ios::sync_with_stdio(false); |
| cin.tie(nullptr); |
| |
| long long k; |
| if (!(cin >> k)) return 0; |
| |
| vector<Instr> prog; |
| auto addPop = [&](int a, int x, int b, int y) -> int { |
| prog.push_back({false, a, x, b, y}); |
| return (int)prog.size(); |
| }; |
| auto addHalt = [&](int b, int y) -> int { |
| prog.push_back({true, 0, 0, b, y}); |
| return (int)prog.size(); |
| }; |
| |
| if (k == 1) { |
| cout << 1 << "\n"; |
| cout << "HALT PUSH 1 GOTO 1\n"; |
| return 0; |
| } |
| |
| long long R = k - 1; |
| vector<int> bits; |
| for (int j = 1; j <= 30; ++j) { |
| if (R & (1LL << j)) bits.push_back(j); |
| } |
| int t = (int)bits.size(); |
| |
| int sentinel = 1024; |
| |
| |
| int initIdx = addPop(1, 1, sentinel, 0); |
| |
| vector<int> entryIdx; entryIdx.reserve(t); |
| vector<int> popSIdx; popSIdx.reserve(t); |
| vector<int> pushSIdx; pushSIdx.reserve(max(0, t-1)); |
| |
| for (int i = 0; i < t; ++i) { |
| int j = bits[i]; |
| int m = j - 1; |
| |
| int entry = (int)prog.size() + 1; |
| vector<int> levelIdx; |
| levelIdx.reserve(m); |
| for (int l = 1; l <= m; ++l) { |
| |
| int Tl = l; |
| int li = addPop(Tl, 0, Tl, entry); |
| levelIdx.push_back(li); |
| } |
| int popS = addPop(sentinel, 0, sentinel, 0); |
| |
| |
| for (int l = 0; l < m; ++l) { |
| int xline = (l + 1 < m) ? levelIdx[l + 1] : popS; |
| prog[levelIdx[l] - 1].x = xline; |
| } |
| |
| entryIdx.push_back((m > 0) ? entry : popS); |
| popSIdx.push_back(popS); |
| |
| if (i + 1 < t) { |
| |
| int pushS = addPop(1, 1, sentinel, 0); |
| |
| prog[popS - 1].x = pushS; |
| prog[popS - 1].y = pushS; |
| pushSIdx.push_back(pushS); |
| } |
| } |
| |
| |
| int haltIdx = addHalt(1, 1); |
| |
| |
| prog[popSIdx.back() - 1].x = haltIdx; |
| prog[popSIdx.back() - 1].y = haltIdx; |
| |
| |
| for (int i = 0; i + 1 < t; ++i) { |
| prog[pushSIdx[i] - 1].y = entryIdx[i + 1]; |
| } |
| |
| |
| prog[initIdx - 1].y = entryIdx[0]; |
| |
| |
| cout << prog.size() << "\n"; |
| for (auto &ins : prog) { |
| if (!ins.halt) { |
| cout << "POP " << ins.a << " GOTO " << ins.x << " PUSH " << ins.b << " GOTO " << ins.y << "\n"; |
| } else { |
| cout << "HALT PUSH " << ins.b << " GOTO " << ins.y << "\n"; |
| } |
| } |
| return 0; |
| } |