首页 > ACM题库 > UVA > Uva 11538-Chess Queen[组合数学]
2014
02-28

Uva 11538-Chess Queen[组合数学]

Chess Queen

You probably know how the game of chess is played and how chess queen operates. Two chess queens are in attacking position when they are on same row, column or diagonal of a chess board. Suppose two such chess queens (one black and the other white) are placed on (2×2) chess board. They can be in attacking positions in 12 ways, these are shown in the picture below:

Figure: in a (2×2) chessboard 2 queens can be in attacking position in 12 ways

Given an (NxM) board you will have to decide in how many ways 2 queens can be in attacking position in that.

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.

Input is terminated by a line containing two zeroes. These two zeroes need not be processed.

Output

For each line of input produce one line of output. This line contains an integer which denotes in how many ways two queens can be in attacking position in  an (MxN) board, where the values of M and N came from the input. All output values will fit in 64-bit signed integer.

 

Sample Input                              Output for Sample Input
2 2100 223

2300 1000

0 0

1210907100

11514134000

 

 

【题意】

给定一个棋盘,在棋盘上放两个皇后(一百一黑),求使得两个皇后相互攻击(在一行、一列或对角线)的方案数。

计数问题,分类:

1.在一行或一列:n*m(m-1),m*n*(n-1)

2.在对角线,假设n<m,则各对角线长度:1,2,3……n-1,n,n,……n,n-1,n-2,……1.

n-m+1个n。假设长度为l,则该对角线的方案数:l*(l-1)。将所有的加起来即可。

先先给出最初的版本,没有用公式优化。

//============================================================================
// Name        : uva-11538.cpp
// Author      : coder
// Version     :
// Copyright   : www.acmerblog.com
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdio.h>
using namespace std;

typedef long long ll;
ll ans,m,n;
int main() {
	//freopen("in.txt", "r", stdin);
	while(scanf("%lld%lld",&m,&n) && m && n){
		if(m > n) swap(m,n);
		ans = 0;
		ans += m*(m-1)*n + n*(n-1)*m; //横向和竖向
		ll x=0;//x为斜向
		//此处可用公式优化。
		for(ll i=2; i<m; i++){
			x += i*(i-1)*2;
		}
		x += (n-m+1) * m * (m-1);
		ans += x*2;//两个方向
		printf("%lld\n",ans);
	}
	return 0;
}

耗时较大,循环的部分可以用公式。

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    long long int n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF&&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;
}

  1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。