题目链接:https://vjudge.net/problem/HDU-3811

Permutation

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 496    Accepted Submission(s): 238

Problem Description
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.
 
Input
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

 
Output
For each test case, output the case number first. Then output the number of beautiful permutations in a line.
 
Sample Input
3
3 2
1 1
2 1
3 2
1 1
2 2
4 3
1 1
1 2
1 3
 
Sample Output
Case 1: 4
Case 2: 3
Case 3: 18
 
Author
hanshuai
 
Source
 
Recommend
lcy
 
 
题意:
给出m个(A,B),问n的全排列中有多少个满足:至少存在一个i,使得第Ai位为Bi?
 
 
题解:
1.状压DP,设dp[status][has]为:状态为status(前面含有哪几个数),且是否已经满足要求(has)的情况下有多少种。
2.剩下的就是类似TSP的状态转移了(感觉又像是TSP,又像是数位DP)。
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = (<<)+; bool g[][];
LL dp[MAXN][];
int cnt[MAXN]; void init()
{
for(int s = ; s<MAXN; s++)
{
cnt[s] = ;
for(int j = ; j<; j++)
if(s&(<<j)) cnt[s]++;
}
} int main()
{
init();
int T, n, m, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
memset(g, false, sizeof(g));
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
g[u][v] = true;
} memset(dp, , sizeof(dp));
dp[][] = ;
for(int s = ; s<(<<n); s++)
{
for(int i = ; i<; i++)
{
for(int j = ; j<n; j++)
if(!(s&(<<j)))
dp[s|(<<j)][i|g[cnt[s]+][j+]] += dp[s][i];
}
}
printf("Case %d: %lld\n", ++kase, dp[(<<n)-][]);
}
}

HDU3811 Permutation —— 状压DP的更多相关文章

  1. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  2. HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)

    题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...

  3. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  4. CodeForces 327E Axis Walking(状压DP+卡常技巧)

    Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...

  5. ZOJ - 3777(状压dp)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  6. zoj3777 Problem Arrangement(状压dp,思路赞)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  7. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  8. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  9. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

随机推荐

  1. hadoop datanode节点超时时间设置

    datanode进程死亡或者网络故障造成datanode无法与namenode通信,namenode不会立即把该节点判定为死亡,要经过一段时间,这段时间暂称作超时时长. HDFS默认的超时时长为10分 ...

  2. centos 7 安装五笔输入法

    centos 7 安装五笔输入法 [a@endv ~]$ yum search wubi 已加载插件:fastestmirror, langpacks Loading mirror speeds fr ...

  3. 编程算法 - 两个链表的第一个公共结点 代码(C)

    两个链表的第一个公共结点 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入两个链表, 找出它们的第一个公共结点. 计算链表的长度, 然后移动 ...

  4. UNP学习笔记(第四章 基本TCP套接字编程)

    本章讲解编写一个完整的TCP客户/服务器程序所需要的基本套接字函数. socket函数 #include <sys/socket.h> int socket(int family,int ...

  5. 在非主线程中更新UI

    在非主线程中调用了showMessage方法,结果报错:Can't create handler inside thread that has not called Looper.prepare() ...

  6. javascript回调函数,闭包作用域,call,apply函数解决this的作用域问题

    在JavaScrip中,function是内置的类对象,也就是说它是一种类型的对象,可以和其它String.Array.Number.Object类的对象一样用于内置对象的管理.因为function实 ...

  7. Hadoop2.2.0-64位编译

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 实验环境:Ubunt ...

  8. VueJS循环语句:v-for

    v-for 指令需要以 item in items 形式的特殊语法, items 是源数据数组并且 item 是数组元素迭代的别名. HTML <!DOCTYPE html> <ht ...

  9. Linux Sed命令具体解释+怎样替换换行符&quot;\n&quot;(非常多面试问道)

    Sed Sed是一个强大的文本处理工具 能够採用正则匹配.对文本进行插入删除改动等操作 Sed处理的时候,一次处理一行,每一次把当前处理的存放在暂时缓冲区.处理完后输出缓冲区内容到屏幕,然后把下一行读 ...

  10. Buck电路匹配和二极管仿真模式

    Buck带同步整流,关闭二极管仿真模式会使空载损耗大 利用二极管仿真模式提高降压转换器轻负载效率 Buck电路工作原理以及三种工作模式分析   一.Buck电路原理图 Buck电路,又称降压电路,其基 ...