11538 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 (N × M) 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 (M × N) board, where the values of M and
N came from the input. All output values will fit in 64-bit signed integer.
Sample Input
2 2
100 223
2300 1000
0 0
Sample Output
12
10907100
11514134000
思路:
其实这道题含义是只要在同一行或同一列或同一对角线都可以攻击。行和列都很好想。每
1.行有m个,可以放m个,另外一个就只能放m-1个。一共n列就是mn(m-1)。
2.然后列的话也相同就是把行换成列mn(n-1)
3.下面是对角线形的,设m>n,看左下方向来说。设对角线长度为len。len就是1至m。对于每个len都有len*(len-1)的情况。然后有两次所以*2.下面考虑长度为m的有多少个。长度为m的主要又最长边决定,大家可以写写试试。然后就有(m-n+1)个,每个都有n个空,另外一个皇后就是n-1个空。然后因为还有右上所以乘二。就推出下面公式化简就行


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
ios::sync_with_stdio(false);
ll a, b;
while (cin >> a >> b && a&&b)
{
ll ans = 0;
if (a > b) swap(a,b);
cout << a * b*(a + b - 2) + 2 * a*(a - 1)*(3 * b - a - 1) / 3 << endl;
}
return 0;
}

本文探讨了在N×M的棋盘上,两个皇后(一黑一白)处于攻击位置的不同方式数量。通过分析行、列和对角线的攻击模式,提供了一个计算公式,并附带了一个C++代码实现。
2万+

被折叠的 条评论
为什么被折叠?



