Black and White
Submissions: 1357   Accuracy:

59.45%

  Difficulty: Medium   Marks: 4

How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.

Input:
The first line contains the number of test cases T. Each of the next T lines contains two integers N and M which is size of matrix.

Output:
For each testcase, print the required answer, i.e, number of possible ways to place knights.

Constraints:
1 <= T <= 100
1 <= N, M <= 105

Example:
Input:
3
2 2
2 3
4 5

Output:
12
26
312

Explanation:
Testcase 1:
We can place a black and a white knight  in 12 possible ways such that none of them attracts each other.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Shikhar


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.