UVA11538 Chess Queen

题意

给一个\(n \times m\)的棋盘,输出有多少种方法放置两个互相攻击的皇后。

\(n,m \leq 10^6\)

分析

参照刘汝佳的题解。

横、竖、斜三种情况互不相干,加法原理统计。

横竖都好计算,斜着需要推一推。

然后注意溢出问题。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
    rg T data=0;
    rg int w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<class T>T read(T&x)
{
    return x=read<T>();
}
using namespace std;
typedef unsigned long long ull;

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ull n,m;
    while(read(n)|read(m))
    {
        if(n>m)
            swap(n,m);
        printf("%llu\n",n*m*(m+n-2)+2*n*(n-1)*(3*m-n-1)/3);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/autoint/p/10025534.html