File size: 2,143 Bytes
1fd0050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;

struct Inst {
    bool isHalt = false;
    int a = 0, b = 0, x = 0, y = 0; // for POP: a,x,b,y ; for HALT: b,y
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long k;
    if (!(cin >> k)) return 0;

    const int MARK = 1024;
    const int DUMMY_A = 1023;

    if (k == 1) {
        cout << 1 << "\n";
        cout << "HALT PUSH 1 GOTO 1\n";
        return 0;
    }

    long long m = k - 1; // even
    vector<int> blocks;  // each element is mBits = p-1, block cost = 2^p
    for (int p = 30; p >= 1; --p) {
        if ((m >> p) & 1LL) blocks.push_back(p - 1);
    }

    vector<int> startIdx(blocks.size());
    int cur = 1;
    for (size_t i = 0; i < blocks.size(); ++i) {
        startIdx[i] = cur;
        cur += blocks[i] + 2; // push marker + mBits bit-insts + return
    }
    int haltIdx = cur;
    int n = haltIdx;

    vector<Inst> inst(n + 1);

    for (size_t bi = 0; bi < blocks.size(); ++bi) {
        int mBits = blocks[bi];
        int start = startIdx[bi];
        int firstBit = start + 1;
        int retIdx = start + mBits + 1;
        int after = (bi + 1 < blocks.size()) ? startIdx[bi + 1] : haltIdx;

        // Unconditional push marker -> firstBit
        inst[start] = Inst{false, DUMMY_A, MARK, firstBit, firstBit};

        // Bit instructions
        for (int j = 1; j <= mBits; ++j) {
            int idx = start + j;
            inst[idx] = Inst{false, j, j, idx + 1, firstBit};
        }

        // Return: pop marker -> after
        inst[retIdx] = Inst{false, MARK, 1, after, retIdx};
    }

    inst[haltIdx] = Inst{true, 0, 1, 0, haltIdx};

    if (n > 512) {
        // Guaranteed solvable; this should never happen with the construction.
        return 0;
    }

    cout << n << "\n";
    for (int i = 1; i <= n; ++i) {
        if (inst[i].isHalt) {
            cout << "HALT PUSH " << inst[i].b << " GOTO " << inst[i].y << "\n";
        } else {
            cout << "POP " << inst[i].a << " GOTO " << inst[i].x
                 << " PUSH " << inst[i].b << " GOTO " << inst[i].y << "\n";
        }
    }

    return 0;
}