标签:
UVa Online Judge
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533
【题意】
给定一个棋盘,在棋盘上放两个皇后(一白一黑),求使得两个皇后相互攻击(在一行、一列、对角线均可相互攻击),求方案数。
计数问题,分类:
1.在一行或一列:n*m(m-1),m*n*(n-1)
2.在对角线,假设n<m,则各对角线长度:1,2,3……n-1,n,n,……n,n-1,n-2,……1.
D(n,m)=2*(2*[sum(i*(i-1)]+(m-n+1)n(n-1)) 条件i=(1~n-1)
其中:
sum(i^2)=n(n-1)(2n-1)/6;
sum(i)=n(n-1)/2;
D(n,m)=2[2*sum(i^2)-2*sum(i)+(m-n+1)n(n-1)]=2n(n-1)(3m-n-1)/3;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
int main()
{
LL m,n;
while(scanf("%lld%lld",&n,&m),(m||n))
{
if(n>m)
swap(n,m);
LL x1=n*m*(m-1);//同一行相同n*C(2,m)
LL x2=m*n*(n-1);//同一列相同m*C(2,n)
LL x3=2*n*(n-1)*(3*m-n-1)/3;//对角线上
LL count=x1+x2+x3;
cout<< count<< endl;//由于数比较大 应该用ll
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lh__huahuan/article/details/47304277