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. delphi 高版本可执行程序减小的办法

    选菜单里的 Project -> Options.. (Shift+Ctrl+F11)出现Project Options for Project1.exe窗口,在左边选 Packages出现如下 ...

  2. extjs4 树列表 添加子节点 刷新所有父节点数据

    itemclick:function(view, record, item,index){console.log(record.parentNode) for(pNode = record.paren ...

  3. Demonstrating One-Shot Execution TimerTask Timer

    Listing -. Demonstrating One-Shot Execution import java.util.Timer; import java.util.TimerTask; publ ...

  4. Bluetooth RFCOMM介绍

    目录 1. 介绍 2. 服务概述 2.1 RS-232控制信号 2.2 Null Modem Emulation 2.3 多串口仿真 3. 服务接口描述 4. RFCOMM帧类型 5. RFCOMM帧 ...

  5. NSArry的常见方法

    使用数组对象创建的数组功能非常强大,在Java语言或者C语言中定义的数组必须满足数组中的每一个元素必须是同样的类型.而Objective-C语言可以在Array数组中放任意类型的数据,值得注意的是只能 ...

  6. java jmx

    http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ...

  7. JDK核心包学习

    StringBuffer   线程安全.可变字符序列 StringBuilder   非线程安全.可变字符序列,比StringBuffer更快 Boolean 使用valueOf产生Boolean实例 ...

  8. Difference between _, __ and __xx__ in Python

    When learning Python many people don't really understand why so much underlines in the beginning of ...

  9. transform: translateY(-50%) 实现元素垂直居中效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 七步实现magento迁移

    很多朋友都在为magento搬家烦恼,要想把magento从一台服务器迁移到另一台服务器上并不难,下面给大家介绍一种简单方法就能轻松实现magento迁移. 范例:从http://magento.yo ...