跳到正文

题解

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

Pursuing_OIer约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年6月29日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 7 分钟
获赞
8
题目
P13086

版本

  1. v1当前

Solution

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

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

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

C++
#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;
}
原文
存档评论1 条抓取于