UVa 11538 - Chess Queen

来源:互联网 发布:js循环遍历对象 编辑:程序博客网 时间:2024/06/11 22:52

题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533


思路:

在同一行:n*m*(m-1)

在同一列:m*n*(n-1)

对角线:2*(2*(A(2,2)+A(3,2)+A(4,2)+…+A(m-1,2))+A(m,2)*(n-m+1))

n小于m时交换,这样就避免分类

因为两条对角线对称,所以直接乘以2

对角线上面的个数:2,3,4,…m-1,m…m,m-1,…4,3,2.

其中m的个数为n-m+1个


代码如下:

#include<iostream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>using namespace std;const int N=100010;typedef long long LL;LL n,m;int main(){    while(scanf("%lld%lld",&n,&m))    {        if((n+m)==0)            break;        if(n<m)            swap(n,m);        LL sum=n*m*(m-1)+m*n*(n-1);        LL temp=0;        for(LL i=2;i<=m-1;i++)//这个地方i要为longlong类型            temp+=i*(i-1);        temp=temp*2+(n-m+1)*m*(m-1);        temp=temp*2;        printf("%lld\n",sum+temp);    }    return 0;}