[STOI2014]舞伴(dp)
2015-03-14 18:20
155 查看
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;}------------------------------------------------------------------------------------
其实我很想吐槽为什么题目名是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;}------------------------------------------------------------------------------------
相关文章推荐
- 汕头市选 2014 舞伴___状压dp
- [bzoj3717][PA2014]Pakowanie_动态规划_状压dp
- bzoj 3875: [Ahoi2014&Jsoi2014]骑士游戏【dp+spfa】
- BZOJ3672:[Noi2014]购票 (斜率优化DP+二分+(树上CDQ分治/树链剖分))
- ZOJ 3824 Fiber-optic Network 树形dp 2014牡丹江现场赛F
- HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)
- UOJ #2. 【NOI2014】起床困难综合症 数位DP
- SPFA维护dp——【NOI2014模拟7.11】挖宝藏
- BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
- bzoj3672 [ NOI2014 ] -- 树上CDQ分治 + 斜率优化DP
- 2013-2014集训之DP
- 2014上海网络赛1004||hdu5045 contest【状态压缩dp】
- 【树形期望DP】BZOJ3566- [SHOI2014]概率充电器
- BZOJ 3566: [SHOI2014]概率充电器( 树形dp )
- Good Bye 2014 D. New Year Santa Network 树形dp
- 【bzoj3831】[Poi2014]Little Bird(单调队列优化dp)
- HDU 5000 Clone 规律+dp 2014 ACM/ICPC Asia Regional Anshan Online
- 【XSY1536】【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并
- 【codeforces】2014-2015ACM-ICPC CERC 14 Problem G: Virus synthesis 【Palindromic Tree+DP】
- [HEOI2014][bzoj3611] 大工程 [虚树+dp]