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

k个石子,m*n的方格中让你在第一列、最后一列、第一行、最后一行都放置至少一个石子,求方案数;

思路: 容斥+组合数

通过第一行、最后一行、第一列、最后一列都没有的方案数求得

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
#define mod 1000007
const int MAXN = 505; // 组合上限
ll c[MAXN][MAXN]; // 组合数 void GetGroup()
{
c[0][0] = c[1][0] = c[1][1] = 1;
for (int i = 2; i < MAXN; i++)
{
c[i][0] = 1;
for (int j=1; j<=i; ++j)
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod; // 求模,防止结果过大
}
return ;
} int main()
{
int n,m,t,cas=0,k;
GetGroup();
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&m,&n,&k);
ll ans=0;
for(int i=0;i<16;i++)
{
int cnt=0,l=n,r=m;
if(i&1)
{
cnt++;
l--;
}
if(i&2)
{
cnt++;
r--;
}
if(i&4)
{
cnt++;
l--;
}
if(i&8)
{
cnt++;
r--;
}
if(cnt&1)
{
ans=(ans-c[l*r][k]+mod)%mod;
}
else
ans=(ans+c[l*r][k])%mod;
}
printf("Case %d: %lld\n",++cas,ans); }
return 0;
}

  

uva_11806_Cheerleaders的更多相关文章

随机推荐

  1. AngularJS实现原理

    个人觉得,要很好的理解AngularJS的运行机制,才能尽可能避免掉到坑里面去.在这篇文章中,我将根据网上的资料和自己的理解对AngularJS的在启动后,每一步都做了些什么,做一个比较清楚详细的解析 ...

  2. 阿里云更懂你的数据库,免费提供DBA服务

    阿里云更懂你的数据库,免费提供DBA服务   阿里云云数据库(RDS)管理控制台近期将全面升级为云数据库管家.云数据库管家的使命是提供便捷的操作.贴心的服务.专业的处理建议,帮助用户管理好云数据库. ...

  3. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  4. webstorm javascript IDE调试

    webstorm是我见过的前端开发调试最好用的IDE工具了,它不仅具有强大的编辑,代码查阅引用功能,更有强大的js调试功能,这是任何通过firebug,chrome devtool,console.l ...

  5. css中attribute selector及pseudo class

    https://developer.mozilla.org/en-US/docs/Web/CSS/Reference#Selectors 在css3规范中,定义了以下几种类型的selector: Ba ...

  6. 【深入理解JAVA虚拟机】第5部分.高效并发.2.线程安全和锁优化

    1 概述 对于这部分的主题“高效并发”来讲,首先需要保证并发的正确性,然后在此基础上实现高效. 2 线程安全 <Java Concurrency In Practice> 的作者Brian ...

  7. [原]Linux 命令行浏览器

    真是没有做不到只有想不到! Linux下竟然有命令行式的浏览器:W3m SPC向下翻页 b向上翻页 J 向下滚动一行 K 向上滚动一行 > 右移一屏 < 左移一屏 TAB 转到下个超链接 ...

  8. 巧用padding生成正方形DIV

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. [转]How to Leak a Context: Handlers & Inner Classes

    Consider the following code: public class SampleActivity extends Activity { private final Handler mL ...

  10. 理解Underscore中的uniq函数

    uniq函数,是Underscore中的一个数组去重函数,给它传递一个数组,它将会返回该数组的去重副本. 1 ES6版本去重 在ES6版本中,引入了一个新的数据结构——set,这是一种类似数组的数据结 ...