码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11806 组合数学+容斥

时间:2018-10-18 18:52:02      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:assign   display   tor   float   model   signed   family   des   and   

UVA: https://vjudge.net/problem/UVA-11806

题意:给你一个n×mn×m的矩阵网格和kk个人,问有多少种方法使得每一个格子只放一个人,并且第一行,最后一行,第一列,最后一列都有人。
直接枚举似乎有难度,我们考虑容斥。rrcc列放kk个人的方案数是(r×ck)(r×ck),那么由容斥原理,总方案为

ans=U?A?B?C?D+AB+AC+AD++ABCDans=U?A?B?C?D+AB+AC+AD+…+ABCD


其中UU表示没有限制的方案数,A,B,C,DA,B,C,D分别表示四个区域不放人的方案数。

AC代码

 

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
//const int maxn=1e4+10;
const ll mod=1e6+7;
const int maxn = 1010;
int C[maxn][maxn];
int n, m, k;

void init()
{
    C[0][0] = 1;
    for (int i = 1; i < maxn; i++)
    {
        C[i][0] = 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,kase=1;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&k);
        int ans=C[n*m][k];
        ans=(ans-2*C[(m-1)*n][k]%mod+mod)%mod;
        ans=(ans-2*C[(n-1)*m][k]%mod+mod)%mod;
        ans=(ans+4*C[(n-1)*(m-1)][k])%mod;
        ans=(ans+C[(n-2)*m][k])%mod;
        ans=(ans+C[(m-2)*n][k])%mod;
        ans=(ans-2*C[(n-2)*(m-1)][k]%mod+mod)%mod;
        ans=(ans-2*C[(m-2)*(n-1)][k]%mod+mod)%mod;
        ans=(ans+C[(m-2)*(n-2)][k])%mod;
        printf("Case %d: %d\n",kase++,ans);
    }
}

 

 

 

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 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 2 2 2 1 2 3 2 Sample Output Case 1: 0 Case 2: 2

UVA 11806 组合数学+容斥

标签:assign   display   tor   float   model   signed   family   des   and   

原文地址:https://www.cnblogs.com/stranger-/p/9812200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!