日子过得太舒服,给自己找点罪受

· · 题解

书接上文,这里给出一个 8bit[v] 的限制条件下的解法。

首先发现前面那篇题解中除了一开始用来记录信息的 24 个东西以外没有任何需要 16 个比特的玩意,都可以压进一个字节。所以这里只记录原排列(也就是 \overline{p_3p_2p_1p_0p_3p_2p_1p_0}),尝试对它求逆。

这次就必须用右操作数不是字面量的位移了。大概就是,首先 p_0 直接扔掉,其它三个 p_i 的做法就是把高位低位拆出来,低位放到最右边那一位,高位放到它左边,然后用 i 左移即可。

这里加一个优化:因为最后要拆成高位低位,所以可以在求逆的时候直接拆开。具体地,把原来用 i 左移改成用 1 左移,然后按位或到高位低位上即可。

这样任何时刻都不会出现 \ge 2^8 的数。

可以做到 43 步,懒得优化了。

#include<bits/stdc++.h>
using namespace std;
#define uint unsigned int
enum{NOT,AND,OR,XOR,LSH,RSH,POPC};
const uint N=1<<16,K=15;
struct node{
    int type;//NOT AND OR XOR LSH RSH POPCNT
    bool fl,fr;
    uint to,L,R;
    void get(bool& fl,uint& x){
        if (fl) cout<<"N ";cout<<x;
    }
    void output(){
        cout<<to<<' ';
        if (type==NOT) cout<<"NOT";
        if (type==AND) cout<<"AND";
        if (type==OR) cout<<"OR";
        if (type==XOR) cout<<"XOR";
        if (type==LSH) cout<<"LSH";
        if (type==RSH) cout<<"RSH";
        if (type==POPC) cout<<"POPCNT";
        cout<<' ';get(fl,L);
        if (type&&type<6){
            cout<<' ';
            get(fr,R);
        }
        cout<<'\n';
    }
};
vector<node> ans;
void push(uint to,int type,bool fl,uint L,bool fr,uint R){ans.emplace_back((node){type,fl,fr,to,L,R});}
void build(){
    int p[4]={0,1,2,3},iv[4];
    for (int i=0;i<24;++i){
        uint s=0;
        for (int j=0;j<4;++j){
            s|=(p[j]&1)<<j;
            s|=(p[j]>>1)<<j+4;
        }
        cout<<s<<'\n';
        next_permutation(p,p+4);
    }
}
uint P=1,Q=2,X=5,Y=13,A=1,B=11,C=4,D=12,a[2]={6,7},b[3]={8,9,10};
void getinv(){
    push(X,AND,0,Q,1,1<<1);
    push(X,RSH,0,X,1,1);
    push(Y,LSH,1,1,0,X);
    push(X,AND,0,Q,1,1<<5);
    push(X,RSH,0,X,1,4);
    push(Y,LSH,0,Y,0,X);
    push(B,OR,0,B,0,Y);

    push(X,AND,0,Q,1,1<<2);
    push(X,RSH,0,X,1,2);
    push(Y,LSH,1,1,0,X);
    push(X,AND,0,Q,1,1<<6);
    push(X,RSH,0,X,1,5);
    push(Y,LSH,0,Y,0,X);
    push(D,OR,0,D,0,Y);

    push(X,AND,0,Q,1,1<<3);
    push(X,RSH,0,X,1,3);
    push(Y,LSH,1,1,0,X);
    push(X,AND,0,Q,1,1<<7);
    push(X,RSH,0,X,1,6);
    push(Y,LSH,0,Y,0,X);
    push(B,OR,0,B,0,Y);
    push(D,OR,0,D,0,Y);
}
void solve(){
    push(C,RSH,0,A,1,4);

    push(99,XOR,0,A,0,C);
    push(100,XOR,0,B,0,D);
    push(b[2],AND,0,99,0,100);

    push(a[0],XOR,0,C,0,D);
    push(a[1],XOR,0,A,0,B);
    push(b[0],AND,0,C,0,D);
    push(b[1],AND,0,A,0,B);

    for (int i=0;i<3;++i) push(b[i],POPC,0,b[i],0,0);
}
void calc(){
    push(3,OR,0,a[0],0,a[1]);
    push(3,XOR,0,3,1,K);
    push(3,AND,0,3,1,K);
    push(3,LSH,0,3,1,4);
    for (int i=0;i<3;++i) push(3,OR,0,3,0,b[i]);
    push(3,OR,0,3,1,13);
    push(3,POPC,0,3,0,0);
    push(3,RSH,0,3,1,1);
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    getinv();
    build();
    solve();
    calc();
    cout<<ans.size()<<endl;
    for (auto x:ans) x.output();
    return 0;
}