zoukankan      html  css  js  c++  java
  • HDU 3811 Permutation 状压dp

    题目链接:

    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
    ",++kase,ans);
        }
        return 0;
    }
    
    //end-----------------------------------------------------------------------
  • 相关阅读:
    centos7安装nginx
    linux经常使用的命令
    linux 安装 VNC
    linux配置yum源
    docker服务器、以及容器设置自动启动
    docker初步学习以及常用命令
    Docker命令详解(run篇)
    Linux scp命令
    Linux常用命令学习31个
    Linux下解压tar.xz文件
  • 原文地址:https://www.cnblogs.com/fenice/p/6006301.html
Copyright © 2011-2022 走看看