题意:在一个N*M的棋盘上放两个(一黑一白)互相攻击的皇后有几种放法。
——>>数学呢……
当两个皇后横向攻击时:有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;
}