跳到正文

题解

题解:P4106 [HEOI2014] 逻辑翻译

wjwWeiwei约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月15日
收录
2026年10月18日
更新
2026年11月13日
篇幅
约 3 分钟
获赞
4
题目
P4106

版本

  1. v1当前

板子题。

对于多项式的系数,我们可以很容易列出 2n2^n 个方程。举个例子:

[1−1−11r011−1−1r11−11−1r21111r3]\begin{bmatrix}1 & -1 & -1 & 1 & r_0\\1 & 1 & -1 & -1 & r_1\\1 & -1 & 1 & -1 & r_2\\1 & 1 & 1 & 1 & r_3\end{bmatrix}

以上每一列对应一个系数变量,右侧 rr 的下标是将原串的 ++ 视作 11,−- 视作 00 表示的二进制数。

考虑手动对其进行消元。可以发现对于一个变量,将它这一列的 11 对应的行减去 −1-1 对应的行就可以成功消元。

形式化的说,我们只用求出

fS=∑T(−1)popcount(S&⁡(¬T))gTf_{S}=\sum_{T}(-1)^{popcount(S\operatorname{\&}(\neg T))}g_T

将读入数组翻转后做异或 FWT 即可。

还是放个代码:

C++
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1<<20;
int n,m;
ll f[N];
inline void pout(ll x){
	ll t1=x,t2=m*100;
	ll gc=__gcd(abs(t1),t2);
	t1/=gc,t2/=gc;
	if(t2==1)cout<<t1<<" ";
	else cout<<t1<<"/"<<t2<<" ";
}
void tak(int pt){
	if(!f[pt])return ;
	pout(f[pt]);
	for(int i=n-1;i>=0;i--){
		if((pt>>i)&1)cout<<"x"<<(n-i);
	}
	cout<<"\n";
}
void out(int x,int pt,int las){
	if(x==-1){
		if(las)tak(pt);
		return ;
	}
	if(las==1){
		tak(pt);
		out(x-1,pt|(1<<x),1);
		out(x-1,pt,0);
	}
	else{
		out(x-1,pt|(1<<x),1);
		out(x-1,pt,0);
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	m=1<<n;
	for(int i=0;i<m;i++){
		string s;
		cin>>s;
		int pt=0;
		for(int j=0;j<n;j++)pt+=(1<<j)*(s[n-1-j]=='+');
		double x;
		cin>>x;f[m-1-pt]=round(x*100);
	}
	for(int i=1;i<m;i<<=1){
		for(int j=0;j<m;j+=(i<<1)){
			for(int k=0;k<i;k++){
				ll x=f[j+k],y=f[j+k+i];
				f[j+k]=x+y,f[j+k+i]=x-y;
			}
		}
	}
	out(n-1,0,1);
	return 0;
}

感觉此题最大的难点在于输出。

原文
存档评论2 条抓取于