hdu3811 Permutation (状态压缩DP)
来源:互联网 发布:linux执行shell脚本 编辑:程序博客网 时间:2024/06/06 15:59
题目点我点我点我
题目大意:题目给出m对 a b,表示a位置放b,问你满足其中至少一对关系的总排列数。
思路:表面上看似容斥定理,至于行不行我也没试过,用状态压缩DP就奇快,dp[i]记录的是第i种状态不符合a位置放b的情况的数目,dp[(1<
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define inf 0x3f3f3f3flong long dp[1<<18];long long per[18]={0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000};//当长度为N时有N!种排列情况,把N!用数组a存int flag[18][18];int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t,n,m,a,b,cas=1; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(flag,0,sizeof(flag)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); a--;b--; flag[a][b]=1; //用flag记录a位置不能选择b } dp[0]=1; for(int i=0;i<n;i++) { for(int j=(1<<n)-1;j>=0;j--) { if(!dp[j])continue; for(int k=0;k<n;k++) { if((j&(1<<k))!=0)continue; //j状态已有数字k了 if(flag[i][k]==1)continue; //k位置不能放i dp[j|(1<<k)]+=dp[j]; } } } printf("Case %d: %lld\n",cas++,per[n]-dp[(1<<n)-1]); } return 0;}
0 0
- hdu3811 Permutation (状态压缩DP)
- hdu 4917Permutation(状态压缩DP)
- HDU4917 Permutation(状态压缩dp,图论)
- Permutation HUD-3811 (状态压缩)
- hdu3811
- hdu1565(状态压缩dp)
- 状态压缩DP(二)
- 状态压缩dp(hdu3406)
- poj2411(状态压缩dp)
- poj1185(状态压缩dp)
- poj3311(状态压缩dp)
- poj3254(状态压缩dp)
- poj2288(状态压缩dp)
- hdu5045(状态压缩dp)
- nefu1109(状态压缩dp)
- hdu5045(压缩状态DP)
- hdu3001(状态压缩dp)
- bzoj1087(状态压缩dp)
- ERP维护收费,不收费谁维护?
- IOS UIGestureRecognizer
- 面试之汇总在线编程题
- 最短路径
- SVN的环境搭建
- hdu3811 Permutation (状态压缩DP)
- JS产生随机数的几个用法!
- JS 回车!=提交
- 面试之C题目汇总1
- 终端命令行编译生成二维码
- sizeof()和strlen()的不同
- 求数组相邻元素差值的最大值快速算法(C++版)
- 面试之C题目汇总2
- Ubuntu 下载 & 编译 Android5.1 源码