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);
        }
    }
  • 相关阅读:
    直击JDD | 京东技术全景图首次展示 四大重磅智能技术驱动产业未来!
    干货|上云了,如何保障云数据库的高可用?
    直击JDD | 共建智能新城 京东云让城市生活变得简单美好
    2019京东全球科技探索者大会议程抢先曝光!
    京东云入选2019年度TOP100全球软件案例 新一代服务治理框架加速行业落地
    剁手季我做过最牛的事情
    干货|混沌工程落地的六个阶段
    Jenkins 插件中心国内镜像源发布
    list
    queue
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5350103.html
Copyright © 2011-2022 走看看