[STOI2014]舞伴(dp)

STOI是汕头OI...无聊翻到了去年的比赛题目,就写然后自己测了一下。

其实我很想吐槽为什么题目名是perm,perm好像和舞伴完全无关..

dp(x,s)=∑dp(x-1,s-{i}))(0<=i<n,i∈s,i号女生和x号男生是朋友),dp(x,s)表示前x个男生已和女生配对,已配对女生用集合s表示。边界:第0号男生和第i号女生(0<=i<n),若是朋友则 dp(0,{i})=1;否则dp(0,{i})=0.计算过程中边算边mod,最后输出就OK了。

----------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int maxn=20+5;
int mod,n;
int ok[maxn][maxn];
int d[maxn][1<<20];
int dp(int x,int s) {
    int &ans=d[x][s];
    if(ans>=0) return ans;
    ans=0;
    rep(i,n) if(ok[x][i] && (s & (1<<i)))
        (ans+=dp(x-1,s^(1<<i)))%=mod;
    return ans;
}
int main()
{
    freopen("perm.in","r",stdin);
    freopen("perm.out","w",stdout);
    
    
    clr(ok,0); clr(d,-1);
    cin>>n>>mod;
    rep(i,n) rep(j,n) scanf("%d",&ok[i][j]);
    rep(i,n)
        if(ok[0][i]) d[0][1<<i]=1;
        else d[0][1<<i]=0;
    cout<<dp(n-1,(1<<n)-1)<<endl;
    
    return 0;
}

------------------------------------------------------------------------------------
 

 

posted @ 2015-03-14 18:20  JSZX11556  阅读(272)  评论(0编辑  收藏  举报