题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3811

Permutation

Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> In combinatorics a permutation of a set S with N elements is a listing of the elements of S in some order (each element occurring exactly once). There are N! permutations of a set which has N elements. For example, there are six permutations of the set {1,2,3}, namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
> But Bob think that some permutations are more beautiful than others. Bob write some pairs of integers(Ai, Bi) to distinguish beautiful permutations from ordinary ones. A permutation is considered beautiful if and only if for some i the Ai-th element of it is Bi. We want to know how many permutations of set {1, 2, ...., N} are beautiful.

输入

The first line contains an integer T indicating the number of test cases.

There are two integers N and M in the first line of each test case. M lines follow, the i-th line contains two integers Ai and Bi.

Technical Specification

  1. 1 <= T <= 50
  2. 1 <= N <= 17
  3. 1 <= M <= N*N
  4. 1 <= Ai, Bi <= N

输出

For each test case, output the case number first. Then output the number of beautiful permutations in a line.

样例输入

3

3 2

1 1

2 1

3 2

1 1

2 2

4 3

1 1

1 2

1 3

样例输出

Case 1: 4

Case 2: 3

Case 3: 18

题意

给你一些条件:(a,b),在a这个位置要放b,求至少满足其中一个条件的1到n的排列有多少种。

题解

首先,从正面很明显可以看出是容斥,考虑条件间的容斥,但是发现条件有17*17个,因此不可行。

然后可以从反面出发,要求至少满足一个就是求所有的-一个都不满足的,所以我们只要求出一个都不满足的就可以了。

这个可以用dp做,dp[i][j]表示前i位放的状态为j的情况。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9; const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=17; LL dp[maxn][1<<maxn];
int n,m; int mp[maxn][maxn]; int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d%d",&n,&m);
clr(mp,0);
rep(i,0,m){
int u,v;
scf("%d%d",&u,&v); u--,v--;
mp[u][v]=true;
} ///初始化
clr(dp,0);
for(int i=0;i<n;i++){
if(mp[0][i]==0){
dp[0][1<<i]=1;
}
} ///递推
for(int i=1;i<n;i++){
for(int j=0;j<(1<<n);j++){
for(int k=0;k<n;k++){
if(!(j&(1<<k))&&!mp[i][k]){
dp[i][j^(1<<k)]+=dp[i-1][j];
}
}
}
} LL ans=1;
for(int i=2;i<=n;i++) ans*=i;
ans-=dp[n-1][(1<<n)-1];
prf("Case %d: %lld\n",++kase,ans);
}
return 0;
} //end-----------------------------------------------------------------------

HDU 3811 Permutation 状压dp的更多相关文章

  1. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  2. HDU3811 Permutation —— 状压DP

    题目链接:https://vjudge.net/problem/HDU-3811 Permutation Time Limit: 6000/3000 MS (Java/Others)    Memor ...

  3. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  4. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  5. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  6. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  7. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  9. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

随机推荐

  1. OO学习体会与阶段总结(多线程程序)

    前言 在最近一个月的面向对象编程学习中,我们进入了编写多线程程序的阶段.线程的创建.调度和信息传递,共享对象的处理,线程安全类的编写,各种有关于线程的操作在一定程度上增加了近三次作业的复杂度与难度,带 ...

  2. UOJ#34. 多项式乘法(NTT)

    这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项 ...

  3. 《锋利的JQ》摘抄(一) jq基础篇

    前言:第一次写博客有点紧张233333,我会在博客里放一下在赌这本书过程中遇到的一些有用的知识点,希望等帮助到大家.好了正题开始(只要是我不知道该说啥了= =)  一,资源(在w3cfuns资源中可以 ...

  4. mfc 函数模板

    函数模板的使用 一. 函数模板的使用 使用函数模板可以简化 形参个数相同,而类型不同的函数. template<typename T> //可以用class替换typename int m ...

  5. 22-[jQuery]-选择器, js jQuery对象转换

    1.基础选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. linux下centos7中mysql崩溃问题的解决

    ---恢复内容开始--- 出现错误: 尝试解决: 错误解释是说系统运行过程中丢失了pid:我最先想到是 可能磁盘满了:于是 df -h 检查了一下:磁盘并没有满! 于是我对/etc/my.cnf [m ...

  7. 当使用了相对路径 <base href="<%= basePath %>" /> 后,全局都只能使用相对路径

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  8. Java类的加载的一个小问题

    前言 之前写了一篇文章专门介绍了一下类的加载和对象的创建流程,然后收到了一个博友的疑问,觉得蛮好的,在这里和大家分享下. 博文地址:[Java基础]Java类的加载和对象创建流程的分析 疑问 类在加载 ...

  9. (webapp)微信和safri 对于html5 部分功能不兼容,多选或单选下拉框去除边框无效果。

    1 appearance:none; 2 -moz-appearance:none; /* Firefox */ 3 -webkit-appearance:none; /* Safari 和 Chro ...

  10. 【日常训练】Help Victoria the Wise(Codeforces 99C)

    题意与分析 这题意思是这样的:在正方体的六面镶嵌给定颜色的宝石(相同颜色不区分),然后问最多有几种彼此不等价(即各种旋转过后看起来一致)的方案. 其实可以乱搞,因为范围只有720.求出全排列,然后每个 ...