UVA - 11795 状压DP

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const double eps = 1e-10;
typedef long long ll;
const int oo = 0x3f3f3f3f;
ll read(){
    ll x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll S[maxn],dp[maxn],U[maxn],st;
char str[maxn];
int main(){
    int T=read(),kase=0;
    while(T--){
        memset(S,0,sizeof S);st=0;
        memset(U,0,sizeof U);
        memset(dp,0,sizeof dp);dp[0]=1;
        int n=read();
        s0(str);rep(i,0,n-1)if(str[i]=='1')st|=(1<<i);
        rep(i,0,n-1){
            s0(str);
            rep(j,0,n-1) if(str[j]=='1') S[i]|=(1<<j);
        }
        rep(i,0,((1<<n)-1)){
            U[i]=st;
            rep(j,0,n-1){
                if((i>>j)&1) U[i]|=S[j];//dangqian wuqi jihe neng shasi de diren
            }
        }
        rep(i,1,(1<<n)-1){
            dp[i]=0;
            rep(j,0,n-1){
                if((U[i^(1<<j)]&i)&(1<<j)){
                    dp[i]+=dp[i^(1<<j)];
                }
            }
        }
        printf("Case %d: ",++kase);
        println(dp[(1<<n)-1]);
    }
    return 0;
}
posted @ 2018-03-18 15:58  Caturra  阅读(131)  评论(0编辑  收藏  举报