zoukankan      html  css  js  c++  java
  • UVA11806-Cheerleaders(容斥原理+二进制)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. Their
    roles are substantial during breaks and prior to start of play. The world cup soccer is no exception.
    Usually the cheerleaders form a group and perform at the centre of the eld. In addition to this group,
    some of them are placed outside the side line so they are closer to the spectators. The organizers would
    like to ensure that at least one cheerleader is located on each of the four sides. For this problem, we
    will model the playing ground as an M  N rectangular grid. The constraints for placing cheerleaders
    are described below:
     There should be at least one cheerleader on each of the four sides. Note that, placing a cheerleader
    on a corner cell would cover two sides simultaneously.
     There can be at most one cheerleader in a cell.
     All the cheerleaders available must be assigned to a cell. That is, none of them can be left out.
    The organizers would like to know, how many ways they can place the cheerleaders while maintaining
    the above constraints. Two placements are different, if there is at least one cell which contains a
    cheerleader in one of the placement but not in the other.
    Input
    The rst line of input contains a positive integer T  50, which denotes the number of test cases. T
    lines then follow each describing one test case. Each case consists of three nonnegative integers, 2  M,
    N  20 and K  500. Here M is the number of rows and N is the number of columns in the grid. K
    denotes the number of cheerleaders that must be assigned to the cells in the grid.
    Output
    For each case of input, there will be one line of output. It will rst contain the case number followed by
    the number of ways to place the cheerleaders as described earlier. Look at the sample output for exact
    formatting. Note that, the numbers can be arbitrarily large. Therefore you must output the answers
    modulo 1000007.
    Sample Input
    2
    2 2 1
    2 3 2
    Sample Output
    Case 1: 0
    Case 2: 2

    【题意】

    n行m列网格放k个石子。有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子。

    【题解】

    利用容斥原理。可以转到求“第一行、第一列、最后一行、最后一列没有石子”的方案数。

    枚举各个集合的组合时可以借助二进制进行枚举

    1.第一种二进制枚举

    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    using namespace std;  
    int n,m,sec,k;  
    int C[510][510];  
    const int mod=1000007;  
    void pre()  
    {  
        memset(C,0,sizeof(C));  
        for(int i=0;i<=500;i++)  
        C[i][0]=1;  
      
        for(int i=1;i<=500;i++)  
         for(int j=1;j<=i;j++)  
         C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;  
    }  
    int main()  
    {  
        pre();  
        scanf("%d",&sec);  
        for(int z=1;z<=sec;z++)  
        {  
            scanf("%d%d%d",&n,&m,&k);  
            int ans=0;  
            for(int i=0;i<16;i++)  
            {  
                int b=0,r=n,c=m;  
                if(i&1){r--;b++;}  
                if(i&2){r--;b++;}  
                if(i&4){c--;b++;}  
                if(i&8){c--;b++;}  
      
                if(b%2==0)ans=(ans+C[r*c][k])%mod;  
                else ans=(ans+mod-C[r*c][k])%mod;  
            }  
      
            printf("Case %d: %d
    ",z,ans);  
        }  
        return 0;  
    }  

     2.第二种二进制枚举

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int mod=1e6+7; //记得加等号
    int c[505][505];
    void get() //求组合数模板 注意细节问题
    {
        memset(c,0,sizeof(c));
        for(int i=0;i<=500;i++)
        c[i][0]=1;
        for(int i=1;i<=500;i++)
        for(int j=1;j<=i;j++)
        c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }
    int main()
    {
        get();
        int t,cas=1;
        cin>>t;
        while(t--)
        {
            int n,m,k;
            cin>>n>>m>>k;//输入别忘了
            int sum=0;
            for(int i=0;i<(1<<4);i++)
            {
                int flag=0,r=n,h=m;
                for(int j=0;j<4;j++)
                {
                    if(i&(1<<j))
                    {
                        flag++;
                        if(j==0||j==1)
                        r--;
                        else
                        h--;
                    }
                }
                if(flag&1)
                sum=(sum-c[r*h][k]+mod)%mod; //c用过了 用h
                else
                sum=(sum+c[r*h][k]+mod)%mod;
            }
            printf("Case %d: %d
    ",cas++,sum);
        }
    }
  • 相关阅读:
    python中map函数
    python中的or,and运算符
    输入一个字符串, 返回倒序排列的结果 如: abcdef, 返回 fedcba
    centos7启用iptables
    centos7 shell脚本批量上传文件
    Deployment 中尝试声明一个 Volum
    cpu很高,但是看不到是哪个应用或进程
    从进程角度看docker容器
    02一条update的sql的内部执行流程
    01基础架构,一条SQL查询语句是如何执行的?
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5350103.html
Copyright © 2011-2022 走看看