11538-chess Queen
Time limit:2.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_ problem&problem=2533
You are probably know how the game of chess are played and how chess queen. Two chess queens are in attacking if position they on are row, column or same of a diagonal chess. Suppose two such chess queens (one black and the other white) are placed on (2x2) chess board. They can be in attacking positions in ways, this are shown in the picture below:
|
Figure:in A (2x2) Chessboard 2 queens can be to attacking position in ways |
Given an (NXM) board for you have to decide in and how many ways 2 queens can is in attacking position in.
Input
Input file can contain up to 5000 lines of inputs. Each line contains two non-negative integers which denote the value of M and N (0< m, N106) respectively.
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45375.htm
The Input is terminated by a line containing two zeroes. These two zeroes need are not processed.
Output
For each line of the input produce one line of output. This line contains a integer which denotes in how many ways two queens can is in attacking position at an (MxN) board, W Here the values of M. and N came from the input. All output values would fit in 64-bit signed integer.
2 2 100 223 2300 1000 0 0 |
12 10907100 11514134000 |
The question on the P106 of the training guide.
Consider three kinds of conditions: the first two are NM (m-1) and NM (n-1), and the latter should consider the length of all diagonal lines.
Complete code:
/*0.019s*/
#include <cstdio>
#include <algorithm>
using namespace std;
int main ()
{
long long n, m;
while (scanf ("%lld%lld", &n, &m), N)
{
if (n > M) swap (n, m);
printf ("%lld\n", N * M * (M + n-2) + 2 * n * (n-1) * (3 * m-n-1)/3);
return 0;
}