您的位置:首页 > 产品设计 > UI/UE

Uva - 11538 - Chess Queen(数学推导)

2013-03-18 11:37 344 查看
题意:在一个N*M的棋盘上放两个(一黑一白)互相攻击的皇后有几种放法。

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=469&problem=2533

——>>数学呢……

当两个皇后横向攻击时:有N*M*(M-1)种;

当两个皇后纵向攻击时:有N*M*(N-1)种;

当两个皇后斜向攻击时:设N <= M,有1*0 + 2*1 + 3*2 + ... + (N-1)*(N-2) + N*(N-1)*(M-N+1) + (N-1)*(N-2) + (N-2)*(N-3) + ... + 2*1 + 1*0 = 2*N*(N-1)*(3*M-N-1)/3种;

+ 加法原理。

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
long long M, N;
while(cin>>M>>N)
{
if(!M && !N) return 0;
if(N > M) swap(N, M);
cout<<N*M*(M+N-2) + N*(N-1)*(3*M-N-1)/3*2<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: