当前位置:编程学习 > C/C++ >>

uva 11806 Cheerleaders

n 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 field. 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 first 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 first 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
Sample Output
2
2 2 1
2 3 2
Case 1: 0
Case 2: 2
 
题目大意:m*n的网格里放k个石子,每个格子放一个,石头全放完,第一行,第一列,最后一行,最后一列都有石头,有多少种放法?
解题方法:容斥
 
#include <iostream>  
#include <cstdio>  
using namespace std;  
  
const int mod=1000007;  
int c[410][510],m,n,k;  
  
void ini(){  
    c[0][0]=1;  
    for(int i=0;i<=400;i++){  
        c[i][0]=c[i][i]=1;  
        for(int j=1;j<i;j++){  
            c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;  
        }  
    }  
}  
  
int main(){  
    int t;  
    ini();  
    scanf("%d",&t);  
    for(int i=1;i<=t;i++){  
        scanf("%d%d%d",&m,&n,&k);  
        int sum=0;  
        for(int t=0;t<16;t++){  
            int cnt=0,r0=m,c0=n;  
            if(t&1){r0--,cnt++;}  
            if(t&2){r0--,cnt++;}  
            if(t&4){c0--,cnt++;}  
            if(t&8){c0--,cnt++;}  
            if(cnt&1) sum=(sum-c[r0*c0][k]+mod)%mod;  
            else sum=(sum+c[r0*c0][k])%mod;  
        }  
        printf("Case %d: %d\n",i,sum);  
    }  
    return 0;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,