题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2533
思路:同一行中放置皇后即为n*A(m,2),同一列中放置皇后即为m*A(n,2)。对角线上放置皇后:设n不大于m,则所有主对角线格子个数为1,2,3....n-1,n,n....n(m-n个),n-1,n-2,....3,2,1。即主对角线上方案数为2*sigma(i*(i-1))+(m-n+1)*n*(n-1) (i=1...n-1) 副对角线方案数与主对角线方案数相同。则对角线上方案数为2*n*(n-1)*(3*m-n-1)/3。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
LL x,y;
while(scanf("%lld%lld",&x,&y)!=EOF&&(x+y))
{
LL n=min(x,y),m=max(x,y);
LL tmp1=n*(m-1)*m;
LL tmp2=m*(n-1)*n;
LL tmp3=(2*n+1)*(n+1)*n/6-(n+1)*n/2;
LL tmp4=(m-n)*(n*n-n);
LL tmp5=(2*n-1)*n*(n-1)/6-n*(n-1)/2;
LL ans=tmp1+tmp2+(tmp3+tmp4+tmp5)*2;
printf("%lld\n", ans);
}
return 0;
}