Uva 11538 - Chess Queen
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533
|
11538 - Chess Queen Time limit: 2.000 seconds |
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
分析;
公式: n*m*(m+n-2)+2*n*(n-1)*(3*m-n-1)/3 。
AC代码:
// UVa11538 Chess Queen
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
unsigned long long n, m; // 最大可以保存2^64-1>1.8*10^19
while(cin >> n >> m) {
if(!n && !m) break;
if(n > m) swap(n, m); // 这样就避免了对n<=m和n>m两种情况分类讨论
cout << n*m*(m+n-)+*n*(n-)*(*m-n-)/ << endl;
}
return ;
}
吐槽一下:uva 上面看不了自己的代码,只能自己把每个代码都保存下来咯,,,Orz
Uva 11538 - Chess Queen的更多相关文章
- uva 11538 Chess Queen<计数>
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- 组合数学 UVa 11538 Chess Queen
Problem A Chess Queen Input: Standard Input Output: Standard Output You probably know how the game o ...
- 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen
题目链接 题意:给出m行n列的棋盘,当两皇后在同行同列或同对角线上时可以互相攻击,问共有多少种攻击方式. 分析:首先可以利用加法原理分情况讨论:①两皇后在同一行:②两皇后在同一列:③两皇后在同一对角线 ...
- 【组合计数】UVA - 11538 - Chess Queen
考虑把皇后放在同一横排或者统一纵列,答案为nm(m-1)和nm(n-1),显然. 考虑同一对角线的情况不妨设,n<=m,对角线从左到右依次为1,2,3,...,n-1,n,n,n,...,n(m ...
- UVa 11538 Chess Queen (排列组合计数)
题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...
- UVa11538 A Chess Queen
A Chess Queen Problem A Chess Queen Input: Standard Input Output: Standard Output You probably know ...
- 【计数原理】【UVA11538】 Chess Queen
传送门 Description 给你一个n*m的棋盘,在棋盘上放置一黑一白两个皇后,求两个皇后能够互相攻击的方案个数 Input 多组数据,每组数据包括: 一行,为n和m 输入结束标志为n=m=0. ...
- 【策略】UVa 278 - Chess
Chess Almost everyone knows the problem of putting eight queens on an chessboard such that no Quee ...
- UVA11538 - Chess Queen(数学组合)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- toast 防止一直不停弹出,累积显示
private Toast mToast = null; public void showTextToast(String msg) { if (mToast == null) { mToast = ...
- HW 研发体系机构的几个术语
PDT(product development team)产品开发团队 类似于产品经理 程序员 -- PL -- PM --开发代表 -- PDT LEADER --------------- ...
- 蓝牙—服务发现协议(SDP)
服务搜索协议(SDP)提供了应用发现可用服务以及确定可用服务特点的方法.SDP发现协议提供下面的能力 <1>为客户提供搜索所需要服务的能力. <2>允许基于服务类型搜索服务 & ...
- FastMM、FastCode、FastMove的使用(图文并茂)
FastMM是一个替换Embarcadero Delphi Win32应用程序的快速内存管理器,以及可以在多线程下使用,不容易产生内存碎片,并且无需使用外部DLL文件就可以支持共享内存. 使用方法:1 ...
- zepto源码--fragment--学习笔记
文档片段fragment函数默认传递三个参数: html文档片段字符串 name标签 properties额外添加的属性 函数内部实现过程: var dom, nodes, container; 中间 ...
- sort,ksort,asort的区别
sort--对数组的val进行排序 ksort--对数组的key值进行排序 asort--对数组进行排序,键与值的对应关系不变 1.sort对数组排序 格式如下:bool sort(array &am ...
- iOS开发教程之:iPhone开发环境搭建
安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...
- 内存分配、C++变量的生命周期和作用域
1.内存分配 程序的内存分配有以下几个区域:堆区.栈区.全局区.程序代码区,另外还有文字常量区. 栈区 ——存放局部变量,即由auto修饰的变量,一般auto省略.由编译器自动分配释放.局部变量定义在 ...
- How to Move Magento to a New Server or Domain
Magento is one of the fastest growing eCommerce platforms on the internet and with its recent acquis ...
- 实现 UISegmentControl 与 UIScrollView的上下级联(分别在相应的方法中加入级联代码)
实现 UISegmentControl 与 UIScrollView的上下级联,需要在 [segmentCtr addTarget:self action:@selector(segmentedCon ...