跳到正文

题解

题解:P1310 [NOIP 2011 普及组] 表达式的值

chloroethylene约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
获赞
1
题目
P1310

版本

  1. v1当前

题意分析

给定一个表达式,题目让我们求使得该表达式的值为 0 的方案数。

算法选取

由于我的注意力惊人这题涉及表达式和计数,所以我们可以先把表达式建树,再做一遍树形 DP 即可(好像有的题解比这简单的多)。

那如何建树呢?可以 BDFS 参考中缀表达式计算的过程,只不过把计算变成了创建节点。

然后就是树形 DP 了,这个式子很好想,只需要小学数学。

这里先把运算规则贴上:

运算符运算规则⊕0⊕0=00⊕1=11⊕0=11⊕1=1×0×0=00×1=01×0=01×1=1\begin{array}{|c|c|} \hline \qquad\qquad\quad\textsf{运算符}\qquad\qquad\quad & \qquad\qquad\quad\textsf{运算规则}\qquad\qquad\quad \\ \hline \oplus & \begin{aligned} 0 \oplus 0 &= 0 \\ 0 \oplus 1 &= 1 \\ 1 \oplus 0 &= 1 \\ 1 \oplus 1 &= 1 \\ \end{aligned} \\ \hline \times & \begin{aligned} 0 \times 0 &= 0 \\ 0 \times 1 &= 0 \\ 1 \times 0 &= 0 \\ 1 \times 1 &= 1 \\ \end{aligned} \\ \hline \end{array}

其实这里的 ⊕\oplus 是 + 而不是 ^。我们设 fif_i 是当前树上结点的子树运算结果为 0 的方案数,gig_i 是当前树上结点的子树运算结果为 1 的方案数,为了方便描述,我们后面称 lil_i 为 ii 的左儿子,rir_i 为 ii 的右儿子。

先看加法,该节点的运算结果为 0 时,当且仅当左右子树均为 0,即 fi=fli×frif_i=f_{l_i}\times f_{r_i};结果为 1 时,当且仅当左右子树有一个为 1,即 gi=gli×fri+fli×gri+gli×grig_i=g_{l_i}\times f_{r_i}+f_{l_i}\times g_{r_i}+g_{l_i}\times g_{r_i}。

再看乘法,该节点的运算结果为 0 时,当且仅当左右子树有一个 0,即 fi=gli×fri+fli×gri+fli×frif_i=g_{l_i}\times f_{r_i}+f_{l_i}\times g_{r_i}+f_{l_i}\times f_{r_i};结果为 1 时,当且仅当左右子树都为 1,即 gi=gli×grig_i=g_{l_i}\times g_{r_i}。

code

有点小压行,别介意。

C++
#include<bits/extc++.h>
using namespace std;
using namespace chrono;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define _FF(_Name,_Begin,_End) for(auto _Name=(_Begin);_Name<(_End);_Name++)
int n,cnt=0,opt[100005],dp[100005][2],r;
stack<int> st1,st2;
vector<int> g[100005];
string s;
function<void(void)> calc=[](){int r=st1.top();st1.pop();int l=st1.top();st1.pop();int op=st2.top();st2.pop();cnt++;g[cnt].push_back(l);g[cnt].push_back(r);opt[cnt]=op;st1.push(cnt);};
void dfs(int u){
	if(opt[u]==0){
		dp[u][0]=1;
		dp[u][1]=1;
		return;
	}
	int l=g[u][0],r=g[u][1];
	dfs(l);
	dfs(r);
	if(opt[u]==1){
		dp[u][0]=dp[l][0]*dp[r][0]%10007;
		dp[u][1]=((dp[l][0]*dp[r][1]%10007+dp[l][1]*dp[r][0])%10007+dp[l][1]*dp[r][1])%10007;
	}else{
		dp[u][1]=dp[l][1]*dp[r][1]%10007;
		dp[u][0]=((dp[l][0]*dp[r][1]%10007+dp[l][1]*dp[r][0])%10007+dp[l][0]*dp[r][0])%10007;
	}
}
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cout.tie(0);
	cin>>n>>s;
	s="("+s+")";
	_FF(i,0,s.length()){
		if(s[i]=='+'||s[i]=='*'){
			if(i>0&&s[i-1]=='('){
				cnt++;
				opt[cnt]=0;
				st1.push(cnt);
			}
			if(s[i]=='+'){
				while(!st2.empty()&&st2.top()>=1){
					calc();
				}
				st2.push(1);
				if(i+1<s.length()&&s[i+1]!='('){
					cnt++;
					opt[cnt]=0;
					st1.push(cnt);
				}
			}else if(s[i]=='*'){
				while(!st2.empty()&&st2.top()>=2){
					calc();
				}
				st2.push(2);
				if(i+1<s.length()&&s[i+1]!='('){
					cnt++;
					opt[cnt]=0;
					st1.push(cnt);
				}
			}
		}else if(s[i]=='('){
			st2.push(-1);
		}else if(s[i]==')'){
			while(st2.top()!=-1){
				calc();
			}
			st2.pop();
		}
	}
	while(!st2.empty()&&st2.top()!=-1){
		calc();
	}
	r=st1.top();
	dfs(r);
	cout<<dp[r][0];
	return 0;
}
原文