A binary string s of length N = 2 n is given. You will perform the following operation n times :
- Choose one of the operators AND (&), OR (|) or XOR (^). Suppose the current string is S = s 1 s 2 ...s k . Then,
for all , replace s 2i-1 s 2i with the result obtained by applying the operator to s 2i-1 and s 2i . For
example, if we apply XOR to {1101} we get {01}.
After n operations, the string will have length 1.
There are 3 n ways to choose the n operations in total. How many of these ways will give 1 as the only
character of the final string.
输入描述:
The first line of input contains a single integer n (1 ≤ n ≤ 18).
The next line of input contains a single binary string s (|s| = 2
n ). All characters of s are either 0 or 1.
输出描述:
Output a single integer, the answer to the problem.
示例1:
输入
2
1001
输出
4
说明
The sequences (XOR, OR), (XOR, AND), (OR, OR), (OR, AND) works.
题意 给定字符串 求出进行xor & | 操作后,有多少种能是最后的结果是1。
题解 如果是暴力的话,需要3的18次,会超时,稍微剪下枝,就能过了 ,于是发现剩下的数字全为零时 三个操作都无法得到1,dfs+剪枝就过了。
#include<bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define clr(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
const int maxn=1e6+5;
char s[maxn];
bool a[20][maxn];
int n;
int ans;
void dfs(int pos,int len,int op){
int k=len>>1;
int sum0=0;
if(op==1)
rep(i,1,k+1){
a[pos+1][i]=a[pos][i<<1]|a[pos][i<<1-1];
if(a[pos+1][i]==0){
sum0++;
}
}
if(op==2)
rep(i,1,k+1){
a[pos+1][i]=a[pos][i<<1]&a[pos][i<<1-1];
if(a[pos+1][i]==0){
sum0++;
}
}
if(op==3)
rep(i,1,k+1){
a[pos+1][i]=a[pos][i<<1]^a[pos][i<<1-1];
if(a[pos+1][i]==0){
sum0++;
}
}
if(len==2){
ans+=a[pos+1][1];
return ;
}
if(sum0==k)
return;
dfs(pos+1,k,1);
dfs(pos+1,k,2);
dfs(pos+1,k,3);
}
int main(){
//本地测试
#ifdef ONLINE_JUDGE
#else
freopen("C://Users//yan//Desktop//in.txt","r",stdin);
#endif
ios::sync_with_stdio(false);//取消同步
std::cin.tie(0);//解除cin与cout的绑定,进一步加快执行效率。
cin>>n;
n=pow(2,n);
cin>>s;
rep(i,1,n+1){
a[0][i]=s[i-1]-'0';
}
ans=0;
dfs(0,n,1);
dfs(0,n,2);
dfs(0,n,3);
cout<<ans;
}