http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533

Root

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的更多相关文章

  1. uva 11538 Chess Queen<计数>

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  2. 组合数学 UVa 11538 Chess Queen

    Problem A Chess Queen Input: Standard Input Output: Standard Output You probably know how the game o ...

  3. 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen

    题目链接 题意:给出m行n列的棋盘,当两皇后在同行同列或同对角线上时可以互相攻击,问共有多少种攻击方式. 分析:首先可以利用加法原理分情况讨论:①两皇后在同一行:②两皇后在同一列:③两皇后在同一对角线 ...

  4. 【组合计数】UVA - 11538 - Chess Queen

    考虑把皇后放在同一横排或者统一纵列,答案为nm(m-1)和nm(n-1),显然. 考虑同一对角线的情况不妨设,n<=m,对角线从左到右依次为1,2,3,...,n-1,n,n,n,...,n(m ...

  5. UVa 11538 Chess Queen (排列组合计数)

    题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...

  6. UVa11538 A Chess Queen

    A Chess Queen Problem A Chess Queen  Input: Standard Input Output: Standard Output You probably know ...

  7. 【计数原理】【UVA11538】 Chess Queen

    传送门 Description 给你一个n*m的棋盘,在棋盘上放置一黑一白两个皇后,求两个皇后能够互相攻击的方案个数 Input 多组数据,每组数据包括: 一行,为n和m 输入结束标志为n=m=0. ...

  8. 【策略】UVa 278 - Chess

    Chess  Almost everyone knows the problem of putting eight queens on an  chessboard such that no Quee ...

  9. UVA11538 - Chess Queen(数学组合)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. mysql 5.5 5.6配置日志

    5.5(linux) [mysqld]#loglog-output=FILEgeneral-log #开启普通日志general-log_file = /tmp/mysqllog/gen.log #设 ...

  2. QTSingleApplication使用笔记

    http://www.cnblogs.com/kevinzhwl/archive/2012/08/27/2658839.html QTSingleApplication,是Qt官方提供的,用于实现只启 ...

  3. jquerymobile动态添加元素之后不能正确渲染解决方法

    jquerymobile动态添加元素之后有些不能被正确渲染的解决方法: listview:               添加 jq(".detail").listview(&quo ...

  4. 使用SVN提示“工作副本已经锁定”的解决办法

    更新或者提交前执行一下clean up.如果在当前目录执行该命令后,仍然提示锁定,就到上一层目录再执行下...

  5. C/C++预处理和编译

    预处理器的作用 当我们对源代码进行编译时,第一个阶段就是进行预处理.以#开头的都是预处理指令,都会被预处理器处理掉. 也就是说:我们写的代码,不是直接被编译,而是先被预处理器进行修改.那么预处理器如何 ...

  6. 使用多种客户端消费WCF RestFul服务(二)——.net4.0篇

    .net 4.0篇 在.net 4.0下面微软并没有提供类似Net.Http的Rest访问组件,而是在codeplex上面提供的WCF REST Starter Kit Preview 2 里面可以找 ...

  7. jQuery获取自身HTML

    <html><head> <title>jQuery获取自身HTML</title> <meta http-equiv="Content ...

  8. C# winForm程序开机启动和托盘显示 (转http://blog.csdn.net/xinyue3054/article/details/6599508)

    这段时间一直进行cs项目,故整理下开机自动运行.托盘.显示.隐藏. (1).在窗口上点击关闭按钮或者最小化时将托盘显示: (2).双击托盘图标显示窗口: (3).右键点击托盘图标提供三个菜单选项,“退 ...

  9. CSS知识点补充

    一.css框模型概述 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外边距,外边距默认是透明的,因此不会遮挡其后的任何元素 1.css内边 ...

  10. ORA-01000:超出打开游标的最大数(C#)

    在做一个windows服务,通过查询文本不断的插入数据的功能.测试一直没有问题,到实际环境中跑起来后程序退出,查看日志发现报的这个错误 ORA-01000:超出打开游标的最大数 经过上网查询发现是由于 ...