UVA 11538:chess Queen__uva

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.