跳到正文

题解

P13088 题解

lmh_qwq约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年7月5日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 5 分钟
获赞
13
题目
P13088

版本

  1. v1当前

五次操作几乎什么都干不了,所以必须在预处理的时候把东西全部弄好。

现在考虑 pqip_{q_i} 的置换环数量怎么统计。显然只需要考虑长度 ≤2\le 2 的置换环。

计算 {qi}\{q_i\} 的逆排列 {qi′}\{q'_i\}。对于 pqip_{q_i} 的每个长度为 11 的置换环,都有 pi=qi′p_i=q'_i;对于每个 pqip_{q_i} 长度为 22 的置换环 (i,j)(i,j),都有 (pi,pj)=(qj′,qi′)(p_i,p_j)=(q'_j,q'_i)。

发现 (pi,pj)=(qj′,qi′)(p_i,p_j)=(q'_j,q'_i) 不好处理,但是 {pi,pj}={qi′,qj′}\{p_i,p_j\}=\{q'_i,q'_j\} 很好处理。如果两个位置 (i,j)(i,j) 满足 {pi,pj}={qi′,qj′}\{p_i,p_j\}=\{q'_i,q'_j\},那么另外两个位置 (k,l)(k,l) 也满足 {pk,pl}={qk′,ql′}\{p_k,p_l\}=\{q'_k,q'_l\},这样又可以减少一半状态。

对于一个排列,用 1616 个 bit 记录对于每个 (i,j)(i,j) 是否有 pi=jp_i=j;然后记录 (0,1),(0,2),(0,3)(0,1),(0,2),(0,3) 的 {pi,pj}\{p_i,p_j\}——这需要 1818 个 bit,总共是 3434 个。

把逆排列也这样做一次,总共恰好 6868 个 bit。

现在对于任意一个输入,先把 x2x_2 右移 3434 位拿到逆排列的信息,然后和 x1x_1 按位与得到 xx。

xx 中的每一个 11 要么对应一个 pi=qj′p_i=q'_j,要么对应一个 {pi,pj}={qi′,qj′},{pk,pl}={qk′,ql′}\{p_i,p_j\}=\{q'_i,q'_j\},\{p_k,p_l\}=\{q'_k,q'_l\},设 cc 为这些 11 的总数,aa 为期望输出的答案。

现在考虑每一种可能的置换环大小可重集。

{1,1,1,1}:c=4+3=7,a=4{1,1,2}:c=2+1=3,a=3{1,3}:c=1,a=2{2,2}:c=0+1=1,a=2{4}:c=0,a=1\begin{aligned} \{1,1,1,1\}&:c=4+3=7,a=4\\ \{1,1,2\}&:c=2+1=3,a=3\\ \{1,3\}&:c=1,a=2\\ \{2,2\}&:c=0+1=1,a=2\\ \{4\}&:c=0,a=1 \end{aligned}

显然 a=popcount(c)+1a=\mathrm{popcount}(c)+1,将 cc 随便按位或上一个玩意再求 popcount\mathrm{popcount} 就可以做到五步。

C++
#include<bits/stdc++.h>
using namespace std;
#define uint unsigned int
#define ull __uint128_t
enum{NOT,AND,OR,XOR,LSH,RSH,POPC};
const uint N=1<<16;
ostream& operator <<(ostream& out,ull x){
	int stk[50],top=0;
	while(x){stk[++top]=x%10;x/=10;}
	if (!top) top=1;
	while(top) out<<stk[top--];
	return out;
}
ull popc(ull x){
	ull s=0;
	for (int i=0;i<68;++i) s+=(x>>i)&1;
	return s;
}
struct node{
	int type;//NOT AND OR XOR LSH RSH POPCNT
	bool fl,fr;
	ull to,L,R;
	void get(bool& fl,ull& 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(ull to,int type,bool fl,ull L,bool fr,ull R){ans.emplace_back((node){type,fl,fr,to,L,R});}
void solve(){
	push(2,RSH,0,2,1,34);
	push(1,AND,0,1,0,2);
	push(1,POPC,0,1,0,0);
	push(1,OR,0,1,1,8);
	push(3,POPC,0,1,0,0);
}
uint id[4][4];
ull get(uint a[]){
	ull S=0;
	for (int i=0;i<3;++i) S|=(1<<i*6+id[a[i]][a[3]]);
	S<<=16;
	for (int i=0;i<4;++i) S|=1<<(i*4+a[i]);
	return S;
}
void build(){
	for (int s=0,i=0;i<4;++i) for (int j=i+1;j<4;++j) id[i][j]=id[j][i]=s++;
	uint p[4],a[4];
	for (int i=0;i<4;++i) p[i]=i;
	for (int _=0;_<24;++_){
		ull A,B;
		for (int i=0;i<4;++i) a[i]=p[i];
		A=get(a);
		for (int i=0;i<4;++i) a[p[i]]=i;
		B=get(a);
		cout<<((B<<34)|A)<<'\n';
		next_permutation(p,p+4);
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	build();
	solve();
	cout<<ans.size()<<endl;
	for (auto x:ans) x.output();
	return 0;
}
原文
存档评论2 条抓取于