Solution
题目中给出的位比较少,甚至可能只能以四个数字的形式存下整个排列,因此我们考虑直接存储排列使用电路进行复合并判断置换环个数。
考虑从低位到高位依次存储,且 依次存为 ,每次依次取出 中的第 和 个位置,如果这两个位置拼出的数为 ,那么直接取出 中第 和 个位置,新置换第 个位置的数即为所求。这时如果您进行打表,将这个置换与每种可能的置换比较是否相等判断该置换的置换环个数,大概可以使用 到 步实现,获得 到 分。
接下来考虑一些性质:对于我们复合得到的置换,假设它是 ,设 ,可以发现当 时置换环一定有 个,不存在 的情况, 时置换环个数一定为 , 时置换环个数一定为 。考虑 的情况可能包含 或 个置换环的情况。我们发现置换环个数为 且 则这两个置换环的大小必定为 ,否则如果 ,置换环大小必定为 ,考虑将这个置换复合上自己得到 ,并计算 ,如果 那么置换环个数为 ,否则个数为 。这样大概需要 到 步,依据实现精细程度大概可以获得 到 分。
接下来考虑打表发现 且有 个置换环的置换只有 个,将这些特判处理可以只需 到 步,可得 到 分。
在以上结论上稍加卡常可以只用 多步,获得 分以上。获得满分还需继续优化做法。std 使用了以下一些做法:
- 将原置换中每个数存为 位而非 位可以避免在取出置换 中的数时进行额外的左移。
- 考虑特判 个置换环个数为 的置换时,因为他们和另一个有 个置换环的置换形式类似,所以一起判断并在最后加以特判,这样比单独判那 个置换要少几步。
如此即可通过本题。接下来附上一份 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;
}