Black and white painting

Time Limit: 1000MS Memory limit: 65536K

题目描述

You are visiting the Centre Pompidou which contains a lot of modern paintings. In particular you notice one painting which consists solely of black and white squares, arranged in rows and columns like in a chess board (no two adjacent squares have the same colour). By the way, the artist did not use the tool of problem A to create the painting.

Since you are bored, you wonder how many 8 × 8 chess boards are embedded within this painting. The bottom right corner of a chess board must always be white.

输入

The input contains several test cases. Each test case consists of one line with three integers nm and c. (8 ≤ n, m ≤ 40000), where n is the number of rows of the painting, and m is the number of columns of the painting. c is always 0 or 1, where 0 indicates that the bottom right corner of the painting is black, and 1 indicates that this corner is white.

The last test case is followed by a line containing three zeros.

输出

For each test case, print the number of chess boards embedded within the given painting.

示例输入

8 8 0

8 8 1

9 9 1

40000 39999 0

0 0 0

示例输出

0

1

2

799700028

来源

2007~2008 University of Ulm Local Contest

解题报告:这道题就是让求在一个黑白矩阵(相邻的方格颜色不同)中有多少个8*8的并且右下角是白色的正方形的个数,下面用图的方式理解一下

无论右下角是白色还是黑色,所能构成8 * 8的且右下角是白色的只能在黄颜色中的方格中找,黄色区域的放个数是(n- 7)*(m- 7)个,若m-7和n-7有一个是偶数则黑白方格是相同的,只是他们两个都是奇数时,当所给的矩形右下角是黑色时,黑色的方格比白色的方格多一(黄色区域中),反之一样,

代码如下:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n, m, c, ans, p, q, flag1, flag2;
while (scanf("%d%d%d", &n, &m, &c) != EOF && n && m)
{
p = n - 7;
q = m - 7;
flag1 = p % 2;
flag2 = q % 2;
ans = p * q / 2;
if (flag1 && flag2)//都为奇数时
{
ans = ans + 1;
}
if (c == 0)//若右下角是黑色
{
ans = p * q - ans;
}
printf("%d\n", ans);
}
return 0;
}



posted on 2012-03-11 15:32  Stephen Li  阅读(325)  评论(0)    收藏  举报