题目链接: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)。
 
 
代码如下:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const double EPS = 1e-;
  15. const int INF = 2e9;
  16. const LL LNF = 9e18;
  17. const int MOD = 1e5;
  18. const int MAXN = (<<)+;
  19.  
  20. bool g[][];
  21. LL dp[MAXN][];
  22. int cnt[MAXN];
  23.  
  24. void init()
  25. {
  26. for(int s = ; s<MAXN; s++)
  27. {
  28. cnt[s] = ;
  29. for(int j = ; j<; j++)
  30. if(s&(<<j)) cnt[s]++;
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. init();
  37. int T, n, m, kase = ;
  38. scanf("%d", &T);
  39. while(T--)
  40. {
  41. scanf("%d%d", &n, &m);
  42. memset(g, false, sizeof(g));
  43. for(int i = ; i<=m; i++)
  44. {
  45. int u, v;
  46. scanf("%d%d", &u, &v);
  47. g[u][v] = true;
  48. }
  49.  
  50. memset(dp, , sizeof(dp));
  51. dp[][] = ;
  52. for(int s = ; s<(<<n); s++)
  53. {
  54. for(int i = ; i<; i++)
  55. {
  56. for(int j = ; j<n; j++)
  57. if(!(s&(<<j)))
  58. dp[s|(<<j)][i|g[cnt[s]+][j+]] += dp[s][i];
  59. }
  60. }
  61. printf("Case %d: %lld\n", ++kase, dp[(<<n)-][]);
  62. }
  63. }

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. 2016.7.12 去除mybatis-generator生成的class里的注释

    用mybatis-generator自动生成代码会出现很多没必要的注释.     在配置文件里加上: 是否去除所有自动生成的文件的时间戳: 是否去除所有自动生成文件的注释: <commentGe ...

  2. Android 开发程序员必备网站

    开发必备网站: Android 开发各种工具下载 Android 开发国内大牛集合 Android 开发技术博客周刊 Android 开发技术周报中文版 Android 优秀开源项目集合以及源码分析 ...

  3. Node.js学习笔记(4)——除了HTTP(服务器和客户端)部分

    很多node入门的书里面都会在介绍node特性的时候说:单线程,异步式I/O,事件驱动. Node不是一门语言,它是运行在服务器端的开发平台,官方指定语言为javascript. 阻塞和线程: 线程在 ...

  4. shell脚本实现定时重启任务并输出日志信息

    #!/bin/bash #当前日期 time=`date` pidno=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print ...

  5. Ubuntu下载、zsync、安装、常见问题

    下载-镜像地址 http://mirrors.ustc.edu.cn/ubuntu-releases/ http://mirrors.163.com/ubuntu-releases/ Ubuntu 更 ...

  6. wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql

    注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...

  7. iOS开发系列--让你的应用“动”起来【转载】

    概览 原文链接:http://www.cnblogs.com/kenshincui/p/3972100.html 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...

  8. jdbc 链接池的优化

    package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...

  9. 开源项目WebImageView载入图片

    项目地址:https://github.com/ZaBlanc/WebImageView 作者对载入图片,以及图片的内存缓存和磁盘缓存做了封装. 代码量不多.可是可以满足一般的载入图片. 先看下项目结 ...

  10. 【caffe】Caffe的Python接口-官方教程-01-learning-Lenet-详细说明(含代码)

    01-learning-Lenet, 主要讲的是 如何用python写一个Lenet,以及用来对手写体数据进行分类(Mnist).从此教程可以知道如何用python写prototxt,知道如何单步训练 ...