UVa 11538 Chess Queen: Combinations & Classifications

Source: Internet
Author: User
Tags time limit

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;  
}

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.