CF1033F Boolean Computer

#include <bits/stdc++.h>
int p[15],ct[550005],a[30005],w,n,T,x,c[550005];
long long ans;
char s[15];
int cal(int x){
	int ret=0;
	for (int i=0;i<w;i++)
		if (x&(1<<i)) ret+=p[i];
	return ret;
}
void dfs(int t,int x){
	if (t==w){
		ans+=ct[x];
		return;
	}
	if (s[t]=='A'){
		dfs(t+1,x);
		dfs(t+1,x+p[t]);
	}else if (s[t]=='O'){
		dfs(t+1,x);
	}else if (s[t]=='X'){
		dfs(t+1,x);
		dfs(t+1,x+2*p[t]);
	}else if (s[t]=='a'){
		dfs(t+1,x+2*p[t]);
	}else if (s[t]=='o'){
		dfs(t+1,x+p[t]);
		dfs(t+1,x+2*p[t]);
	}else{
		dfs(t+1,x+p[t]);
	}
}
int main(){
	scanf("%d%d%d",&w,&n,&T);
	p[0]=1;
	for (int i=1;i<=w;i++) p[i]=p[i-1]*3;
	for (int i=1;i<=n;i++)
		scanf("%d",&x),a[x]++;
	for (int i=1;i<1<<w;i++) c[i]=cal(i); 
	for (int i=0;i<1<<w;i++)
		for (int j=0;j<1<<w;j++)
			ct[c[i]+c[j]]+=a[i]*a[j];
	while (T--){
		scanf("%s",s);
		std::reverse(s,s+w);
		ans=0; 
		dfs(0,0);
		printf("%lld
",ans);
	} 
}
* 生而自由 爱而无畏 *
原文地址:https://www.cnblogs.com/flyfeather6/p/14381055.html