您的位置:首页 > 产品设计 > UI/UE

UVA11538 Chess Queen

2018-01-16 07:22 489 查看

Chess Queen

题目大意:给定一个n × m的棋盘,问有多少种方法放置黑、白两皇后使其相互攻击

同一行/列:nm * (n + m - 2)

不妨令m > n

对角线:不难发现对角线长度为1,2,3...n-1,n,n,n,.....,n,(m - n + 1)个n,n - 1,....,3,2,1

对角线方案 = 2 * Σ(i = 1 to n - 1)i(i - 1) + (m - n + 1) * n * (n - 1)

Σ(i = 1 to n - 1)i(i - 1) = Σ(i = 1 to n - 1)i^2 - Σ(i = 1 to n - 1)i = (n - 1) * n * (2n - 1) / 6 - n (n - 1)/2

(Σ(i = 1 to n)i^2 = n * (n + 1) * (2 * n + 1) / 6)

带入有对角线方案 = 2n * (n - 1) * (3 * m - n - 1)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(long long &a, long long &b)
{
long long tmp = a;a = b;b = tmp;
}
inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}

const long long INF = 0x3f3f3f3f;
long long n,m;
int main()
{
while(scanf("%lld %lld", &n, &m) != EOF && n * n + m * m)
{
if(n > m) swap(n, m);
printf("%lld\n", n * m * (n - 1) + n * m * (m - 1) + 2 * n * (n - 1) * (3 * m - n - 1) / 3);
}
return 0;
}


UVA11538
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: