『STA - H1』Code Golf (16bit[v]) 题解

· · 题解

Solution

题目中给出的位比较少,甚至可能只能以四个数字的形式存下整个排列,因此我们考虑直接存储排列使用电路进行复合并判断置换环个数。
考虑从低位到高位依次存储,且 1,2,3,4 依次存为 0,1,2,3,每次依次取出 1 中的第 2i-1 和 2i 个位置,如果这两个位置拼出的数为 a,那么直接取出 2 中第 2a-1 和 2a 个位置,新置换第 i 个位置的数即为所求。这时如果您进行打表,将这个置换与每种可能的置换比较是否相等判断该置换的置换环个数,大概可以使用 120 到 160 步实现,获得 10 到 29 分。
接下来考虑一些性质:对于我们复合得到的置换,假设它是 (a,b,c,d),设 x=[a=1]+[b=2]+[c=3]+[d=4],可以发现当 x=4 时置换环一定有 4 个,不存在 x=3 的情况,x=2 时置换环个数一定为 3,x=1 时置换环个数一定为 2。考虑 x=0 的情况可能包含 1 或 2 个置换环的情况。我们发现置换环个数为 2 且 x=0 则这两个置换环的大小必定为 2,否则如果 x=0,置换环大小必定为 4,考虑将这个置换复合上自己得到 (a',b',c',d'),并计算 y=[a'=1]+[b'=2]+[c'=3]+[d'=4],如果 y=4 那么置换环个数为 2,否则个数为 1。这样大概需要 80 到 100 步,依据实现精细程度大概可以获得 45 到 66 分。
接下来考虑打表发现 x=0 且有 2 个置换环的置换只有 3 个,将这些特判处理可以只需 60 到 70 步,可得 77 到 89 分。
在以上结论上稍加卡常可以只用 50 多步,获得 94 分以上。获得满分还需继续优化做法。std 使用了以下一些做法:

  1. 将原置换中每个数存为 4 位而非 2 位可以避免在取出置换 2 中的数时进行额外的左移。
  2. 考虑特判 3 个置换环个数为 2 的置换时,因为他们和另一个有 4 个置换环的置换形式类似,所以一起判断并在最后加以特判,这样比单独判那 3 个置换要少几步。

如此即可通过本题。接下来附上一份 std 以供参考。

#include <bits/stdc++.h>
using namespace std;
using llt = long long;
using llf = long double;
using ull = unsigned long long;
#define endl '\n'
int aas = 0;
int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    vector<int> p(4); iota(p.begin(), p.end(), 0);
    do{
        int r = 0, j = 4;
        for(auto k : p) r += k * j, j <<= 4;
        cout << r << endl;
    }while(next_permutation(p.begin(), p.end()));

    cout << "49" << endl;
    cout << "9 AND 2 N 15" << endl, ++aas;
    cout << "4 RSH 1 9" << endl, ++aas; 
    cout << "4 AND 4 N 15" << endl, ++aas; 

    cout << "9 RSH 2 N 4" << endl, ++aas;
    cout << "9 AND 9 N 15" << endl, ++aas;
    cout << "5 RSH 1 9" << endl, ++aas;
    cout << "5 AND 5 N 15" << endl, ++aas;

    cout << "9 RSH 2 N 8" << endl, ++aas;
    cout << "9 AND 9 N 15" << endl, ++aas;
    cout << "6 RSH 1 9" << endl, ++aas;
    cout << "6 AND 6 N 15" << endl, ++aas;

    cout << "9 RSH 2 N 12" << endl, ++aas;
    cout << "9 AND 9 N 15" << endl, ++aas;
    cout << "7 RSH 1 9" << endl, ++aas;
    cout << "7 AND 7 N 15" << endl, ++aas; // get new

    cout << "8 RSH N 1 4" << endl, ++aas;
    cout << "9 RSH N 32 5" << endl, ++aas;
    cout << "8 OR 8 9" << endl, ++aas;
    cout << "9 RSH N 1024 6" << endl, ++aas;
    cout << "8 OR 8 9" << endl, ++aas;
    cout << "9 RSH N 32768 7" << endl, ++aas;
    cout << "8 OR 8 9" << endl, ++aas;
    cout << "8 AND N 15 8" << endl, ++aas;
    cout << "8 POPCNT 8" << endl, ++aas; // get cnt

    cout << "3 AND 8 N 4" << endl, ++aas;

    cout << "9 AND 8 N 2" << endl, ++aas;
    cout << "1 RSH 9 N 1" << endl, ++aas;
    cout << "9 OR 9 1" << endl, ++aas;

    cout << "3 OR 3 9" << endl, ++aas;
    cout << "9 AND 8 N 1" << endl, ++aas;
    cout << "9 LSH 9 N 1" << endl, ++aas;
    cout << "3 OR 3 9" << endl, ++aas; // normal check

    cout << "5 LSH 5 N 4" << endl, ++aas;
    cout << "6 LSH 6 N 8" << endl, ++aas;
    cout << "7 LSH 7 N 12" << endl, ++aas;
    cout << "4 OR 4 5" << endl, ++aas;
    cout << "4 OR 4 6" << endl, ++aas;
    cout << "4 OR 4 7" << endl, ++aas;

    cout << "9 RSH 4 N 4" << endl, ++aas;
    cout << "11 AND 9 N 2056" << endl, ++aas;
    cout << "11 AND 11 4" << endl, ++aas;
    cout << "10 POPCNT 11" << endl, ++aas;
    cout << "11 RSH 4 N 8" << endl, ++aas;
    cout << "11 AND 11 4" << endl, ++aas;
    cout << "11 POPCNT 11" << endl, ++aas;
    cout << "10 AND 10 11" << endl, ++aas; // special judge

    cout << "8 RSH N 1 8" << endl, ++aas;
    cout << "8 LSH 8 10" << endl, ++aas;
    cout << "3 OR 3 8" << endl, ++aas; // merge ans

    cerr << aas << endl;
}