题目:点击打开链接
题意:一个长度为n的01字符串,第i个和第i+1个经过^或者|或者&运算后合并,问最终01串变成1的方法有多少种。
分析:直接暴力用stl模拟,或者dfs+剪枝,减掉全为0的情况,时间和空间复杂度都有点玄学。或者使用预处理,暴力的复杂度是3^n的,只需要再优化一点点! 如果剩下了16个变量,可能性只有65536个。 预处理他们,得到一个3^(n-4)的做法。
暴力代码:
#pragma comment(linker, "/STACK:102400000,102400000")///手动扩栈
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#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 all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int n;
string s,t;
map<string,int>ma[20];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while(cin>>n>>s) {
ma[0][s]=1;
for(int i=1;i<=n;i++) {
for(map<string,int>::iterator it=ma[i-1].begin();it!=ma[i-1].end();it++) {
s=it->fi;
t="";
int l=s.length();
for(int j=0;j<l;j+=2) t+=((s[j]-'0')&(s[j+1]-'0'))+'0';
ma[i][t]+=it->se,t="";
for(int j=0;j<l;j+=2) t+=((s[j]-'0')|(s[j+1]-'0'))+'0';
ma[i][t]+=it->se,t="";
for(int j=0;j<l;j+=2) t+=((s[j]-'0')^(s[j+1]-'0'))+'0';
ma[i][t]+=it->se,t="";
}
}
cout<<ma[n]["1"]<<endl;
for(int i=0;i<n;i++) ma[i].clear();
}
return 0;
}
dfs代码:
#pragma comment(linker, "/STACK:102400000,102400000")///手动扩栈
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#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 all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int n,a[20][N];
string s;
int gn(int id,int x,int y) {
if(id==1) return x&y;
else if(id==2) return x|y;
else return x^y;
}
int dfs(int x) {
int sum=0;
for(int i=0;i<(1<<x);i++) sum+=a[x][i];
if(sum==0) return 0;
if(x==0) return 1;
int ans=0;
for(int i=1;i<=3;i++) {
for(int j=0;j<(1<<(x-1));j++)
a[x-1][j]=gn(i,a[x][2*j],a[x][2*j+1]);
ans+=dfs(x-1);
}
return ans;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while(cin>>n>>s) {
for(int i=0;i<s.size();i++) a[n][i]=s[i]-'0';
cout<<dfs(n)<<endl;
}
return 0;
}
预处理代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb emplace_back
#define pf emplace_front
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
//head
const int N = (1<<16) + 10, M = (1<<20) + 10;
bool t[20][M];
char s[M];
int dp[5][N], ans = 0;
void init(int n) {
int up = 1;
for (int i = 0; i < n; i++) up = up * 3;
for (int i = 0; i < (1<<(1<<n)); i++) {
for (int j = 0; j < (1<<n); j++) {
if(i & (1<<j)) t[n][j] = 1;
else t[n][j] = 0;
}
for (int j = 0; j < up; j++) {
int tmp = j;
for (int k = n-1; k >= 0; k--) {
for (int l = 0; l < (1<<k); l++) {
if(tmp%3 == 0) t[k][l] = t[k+1][l*2]^t[k+1][l*2+1];
else if(tmp%3 == 1) t[k][l] = t[k+1][l*2]&t[k+1][l*2+1];
else t[k][l] = t[k+1][l*2]|t[k+1][l*2+1];
}
tmp /= 3;
}
dp[n][i] += t[0][0];
}
}
}
void dfs(int n) {
for (int i = 0; i < (1<<n); i++) t[n][i] = t[n+1][i*2]^t[n+1][i*2+1];
if(n == 3) {
int st = 0;
for (int i = 0; i < (1<<n); i++) st = st*2 + t[n][i];
ans += dp[n][st];
}
else dfs(n-1);
for (int i = 0; i < (1<<n); i++) t[n][i] = t[n+1][i*2]&t[n+1][i*2+1];
if(n == 3) {
int st = 0;
for (int i = 0; i < (1<<n); i++) st = st*2 + t[n][i];
ans += dp[n][st];
}
else dfs(n-1);
for (int i = 0; i < (1<<n); i++) t[n][i] = t[n+1][i*2]|t[n+1][i*2+1];
if(n == 3) {
int st = 0;
for (int i = 0; i < (1<<n); i++) st = st*2 + t[n][i];
ans += dp[n][st];
return ;
}
else dfs(n-1);
}
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
init(min(n, 3));
if(n <= 3) {
int st = 0;
for (int i = 0; i < (1<<n); i++) st = st*2 + (s[i] == '1');
printf("%d\n", dp[n][st]);
}
else {
for (int i = 0; i < (1<<n); i++) t[n][i] = s[i] == '1';
dfs(n-1);
printf("%d\n", ans);
}
return 0;
}