Chess Queen
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:
Given an (NxM) board would have to decide in how many ways 2 queens can is in attacking position in T Hat. 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, n£106) respectively.
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, 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 100 223 2300 1000 0 0 |
12 10907100 11514134000 |
Meaning
Given a chessboard, put two queens (110 black) on the chessboard, asking the number of programs that make two Queens attack each other (on one line, column or diagonal).
1. In one row or column: N*m (m-1), m*n* (n-1)
2. On the diagonal, assuming n<m, then each diagonal length: 1,2,3......n-1,n,n,...... n,n-1,n-2,...... 1.
N-m+1 a N. Assuming the length is L, the number of scenarios for that diagonal line: l* (L-1). Add up all of them.
#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;
}