UVa11538

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 ?le 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 ?t in 64-bit signed integer.
Sample Input
2 2
100 223
2300 1000
0 0
Sample Output
12
10907100
11514134000

题意:

给一块nxm的国际象棋棋盘,在上面摆放两个不同色皇后,问使得这两个皇后可以相互攻击的摆放方案数。

分析:

分类讨论,设A(n,m)是同一行放置两个皇后的方案数。那么A(n,m)=n*m*(m-1)。

设B(n,m) 是同一列放置两个皇后的方案数,B(n,m)=A(m,n)=n*m*(n-1)。D(n,m)是同一对角线放置两个皇后的方案数。不妨设n<=m,所有/向的对角线从左至右依次长度为1、2、…、n-1、n、n、…(一共m-n+1个n)、n-1、n-2、…、1。所以D(n,m)=2(2sigma(i from 1 to n-1)i(i-1)+(m-n+1)n(n-1))。总方案数为A(n,m)+B(n,m)+D(n,m)。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>                //用于cin/cout,因为这样可以与平台无关的读写64bit整数,更为方便
 4 #include <algorithm>            //为了使用swap
 5 #define ULL unsigned long long         //最大可以保存到2的64次方减1,如果是long long则是到2的63次方减1
 6 using namespace std;
 7
 8 int main()
 9 {
10     ULL n, m;
11     while(cin >> n >> m)
12     {
13         if(!n && !m) break;
14         if(n > m) swap(n, m);        //避免分情况讨论
15         cout << n * m * (m + n - 2) + 2 * n * (n-1) *(3 * m - n - 1) / 3 << endl;
16     }
17     return 0;
18 }

时间: 2024-08-04 17:58:23

UVa11538的相关文章

Uva11538 排列组合水题

画个图就很容易推出公式: 设mn=min(m,n),mx=max(m,n) 对角线上: 横向:m*C(n,2) 纵向:n*C(m,2) 因为所有的C函数都是只拿了两个,所以可以优化下.不过不优化也过了= = 1 #include <iostream> 2 using namespace std; 3 #define LL long long 4 int n,m; 5 6 LL P(long n,long m) 7 { 8 long p=1; 9 while(m!=0) 10 { 11 p*=n

uva11538 Chess Queen

题目大意: chess中的皇后问题, 在一个n*m的范围内, 两个皇后能够相互攻击的摆放方式有多少种. /* 有三种相对摆放方式: 水平, 竖直, 对角线. 根据加法原理即可, 并且没有交集. 水平和竖直是一样的, 只要n*m矩形旋转90度. 所以结果是: n*m*(m-1)+n*m*(n-1); 对角线复杂些, 先来确定对角线的长度: 1,2,3,...,n-2,n-1,n,n,n,...,n,n,n-1,n-2,...,2,1; 其中n的个数是m-n+1 (其中假设m>n); 结果: 2*(

UVA11538 - Chess Queen(数学组合)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533 题意:在n*m的棋盘上放两个(黑和白)相互攻击的皇后,求有多少种方法?  0<=(n,m)<=10e6; 下图是2*2的方案数12: 很明显要按行列还有对角三种来考虑,每种的方案数相加即可: 每一行我们要从m个格子中选择2个进行放所以方案数是m*(m-1),共有

例题2.1 象棋中的皇后 UVa11538

1.题目描述:点击打开链接 2.解题思路:本题利用加法原理解决.本题要求统计有多少种放置方法,可以使两个皇后相互攻击.因为只有2个皇后,而能够相互攻击的情况只有3种:同一行,同一列,同一对角线.这3种情况都没有交集,因此可以使用加法原理.设三种情况对应的方案数分别为A(n,m),B(n,m),D(n,m).下面讨论如何计算这三个值. A(n,m)的计算可以利用乘法原理:首先选择一格,一共有nm种选法,接下来再选一行中的其他位置,一共只有m-1个位置,因此A(n,m)=nm(m-1). B(n,m

【计数原理】【UVA11538】 Chess Queen

传送门 Description 给你一个n*m的棋盘,在棋盘上放置一黑一白两个皇后,求两个皇后能够互相攻击的方案个数 Input 多组数据,每组数据包括: 一行,为n和m 输入结束标志为n=m=0. Output 对于每组数据,输出: 对应的放置方案数 Sample Input 2 2 100 223 2300 1000 0 0 Sample Output 12 10907100 11514134000 Hint n,m≤1e6,n和m不全为1.保证最终答案在long long int范围之内

UVA计数方法练习[3]

UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 // // Created by Candy on 24/10/2016. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <cst

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

《训练指南》——6.6

第二章:数学基础 基本计数方法: 在这一章节中有关基本的计数原理.容斥原理.二项式系数恒等式这里不再体现,这个专栏主要通过具体的题目来提升对理论知识的应用. 下面我们来看两个基本计数问题: ex1:可重复选择的组合:有n个不同元素,每个元素可以选多次,一共选k个元素有多少方法? 分析:设第i种元素选取的数量是xi,则我们可以将问题转化成x1+x2+…xn = k的非负整数解(x1,x2,….,xn)的组数.其实此时我们就容易想到插入隔板的方法,我们赋予这个等式一个组合含义,即像k个1形成的k-1

三道组合题

uva11538 题意:问一个n*m的棋盘 有多少种方法可以放置两个可以相互攻击的皇后. 讨论下三种情况 横竖斜.  前n项平方和公式: n*(n+1)*(2*n+1)/6 #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map&