Chess Queen
Probably know how the game of chess was played and how Chess Queen operates. Chess Queens is in attacking position when they is on same row, column or diagonal of a chess board. Suppose such chess queens (one black and the other white) is placed on (2x2) chess board. They can in attacking positions in ways, these is shown in the picture below:
Given an (NxM) board you'll have to decide on how many ways 2 queens can is in attacking position in T Hat.
Input
Input file can contain up to lines of inputs. Each line contains non-negative integers which denote the value of M and N (0< M, n£106) respectively.
Input is terminated by a line containing the zeroes. These zeroes need not being processed.
Output
For each line of input produce one line of output. This line contains a integer which denotes in how many ways the queens can be in attacking position in an (MxN) Board, where the values of M and N came from the input. All output values would fit in 64-bit signed integer.
Sample input Output for sample input
2 2 223 2300 0 0 |
10907100 11514134000 &NBSP; |
Test instructions
Given a chessboard, put two queens (110 black) on the chessboard, and ask the two queens to attack each other (in a row, column, or diagonal) of the number of schemes.
1. In one row or column: N*m (m-1), m*n* (n-1)
2. On the diagonal, assuming n<m, the diagonal length: 1,2,3......n-1,n,n,...... n,n-1,n-2,...... 1.
N-m+1 a N. If the length is L, the number of scenarios for the diagonal is: l* (L-1). Add all of them together.
#include <iostream> #include <cstdio>using namespace Std;int main () { long long int n,m; while (~SCANF ("%lld%lld", &n,&m) &&n&&m) { if (n>m) swap (n,m); Long Long int ans= (n-1) *n*m+ (m-1) *m*n+2*n* (n-1) * (m-n+1); ans+=2*2* (n-2) * (n-1) * (n)/3; printf ("%lld\n", ans); } return 0;}
UVA 11538:chess Queen