題目大意:
給定n(=7)和s,以及乙個表示式(僅有ai,
<,&
,∣,(
,)
a_i,<,\&,|,(,)
ai,
<,&
,∣,(
,)組成,並且先算括號,再算與,再算或,邏輯運算兩端一定是真假值,小於號兩端一定是數字,並且小於運算結果是真假值,並且括號內一定是真假值),問有多少
\滿足a i∈
[0,s
)a_i\in [0,s)
ai∈[0
,s),使得表示式的值是tru
e\mathrm
true
。題解:
將a
a<
b看做乙個真假值(只不過到底是真是假待定)。
列舉排列p
pp以及中間的符號,例如其中一種情況可以是:
a p1
≤ap2
3≤ap
4≤ap
5⋯
na_\le a_ap
1≤
ap2
3≤
ap4
≤ap
5⋯
n(注意去重的問題)。
計算在這種情況下有多少種方案。顯然只取決於其是否合法,以及等號數量。
前者由於已經可以確定任意兩個數的大小關係,所以所有表示式中的a
a<
b都可以被確定為tru
e\mathrm
true
或者f al
se
\mathrm
fals
e,然後表示式求值一下即可。
關於表示式求值,先將中綴表示式轉為字尾表示式。
轉的方法是,若其為運算元,直接放隊尾;若其為左括號,放入運算子棧;若其為右括號,則彈出棧頂並放入隊尾直到左括號;否則若其優先順序小於等於棧頂,則將棧頂彈出並放入隊尾,直到棧空或者棧頂運算子為括號或者優先順序小於當前運算子,並將該運算子放入棧中。最終若棧不空,則依次退棧並放入隊尾即可。
表示式求值就是維護數值棧,每次遇到乙個運算子就把棧頂和棧頂的下乙個元素合併(並彈出),再放回棧頂。最後棧頂就是答案。
#include
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define lint long long
#define mod 1000000007
#define mp make_pair
#define fir first
#define sec second
#define gc getchar()
using namespace std;
const
int n=
100,m=9;
typedef pair<
int,
int> pii;
inline
intcti
(char c)
pii q[n]
;int rp,top,stc[n]
,lst[n]
,ps[m]
,g[m]
[m],p[m]
,u[m]
,a[m]
,sz[n]
,c[m]
;char str[n]
;inline
intfast_pow
(int x,
int k,
int ans=1)
inline
intcalc
(int n,
int s)
inline
intok
(int
*p,int n)
top=0;
int x,y;
rep(i,
1,rp) q[i]
.fir?((
(x=ps[q[i]
.fir]
)>
(y=ps[q[i]
.sec]))
?lst[i]=0
:(lst[i]
=(g[x]
[y]==0)
)):(lst[i]
=q[i]
.sec)
;rep
(i,1
,rp)
(lst[i]
<=1)
?stc[
++top]
=lst[i]
:(stc[top-1]
=(lst[i]
=='&'
?(stc[top]
&stc[top-1]
):(stc[top]
|stc[top-1]
)),top--);
return stc[1]
;}intdfs
(int x,
int n,
int ans=0)
intmain()
else
if(c==
'&')
else
if(c==
'|')
else
if(c>=
'0'&&c<=
'9')
else
continue
;while
(top) q[
++rp]=mp
(0,stc[top]
),top--
;for
(int c;c=gc;s=s*10+
(c-'0'))
if(c<
'0'||c>
'9')
break
;rep
(i,1
,n) c[i]
=calc
(i,s)
;lint ans=0;
rep(i,
0,all)
return
!printf
("%d\n",(
int)
(ans%mod));
}
表示式 表示式樹 表示式求值
總時間限制 1000ms 記憶體限制 65535kb 描述 眾所周知,任何乙個表示式,都可以用一棵表示式樹來表示。例如,表示式a b c,可以表示為如下的表示式樹 a b c 現在,給你乙個中綴表示式,這個中綴表示式用變數來表示 不含數字 請你將這個中綴表示式用表示式二叉樹的形式輸出出來。輸入輸入分...
表示式求值
程式的說明見清華大學出版社 資料結構 c語言版 include include define stack init size 40 define stackincrement 20 define ok 1 define false 0 typedef structs stack typedef st...
表示式求值
既然是表示式求值,自然需要在記憶體中儲存計算結果以及中間值。在 用c語言寫直譯器 一 中提過 變數要求是若型別,而 c 語言中的 view plaincopy to clipboardprint?in basic io.h define memery size 26 typedef enum var...