# 题目二 长方形

1 ≤ T ≤ 100

0 ≤ K ≤ N * M

## 样例输入

33 3 84 5 137 14 86

## 样例输出

Case #1: 5Case #2: 18Case #3: 1398

## 解题思路

$\sum\limits_{i = 1}^a {\sum\limits_{j = 1}^b {(a - i)(b - j)} }$

$\sum\limits_{i = 1}^a {\sum\limits_{j = 1}^b {(a - i)(b - j)} } = \sum\limits_{i = 1}^a {(a - i)} \sum\limits_{j = 1}^b {(b - j)} = \frac{{a(a - 1)}}{2}\frac{{b(b - 1)}}{2} = \frac{{ab(a - 1)(b - 1)}}{4}$

## 代码

#include <stdio.h>
#include <math.h>
// 计算 a*b 的石子中包含的长方形数
long long calRects(long long a, long long  b) {
return a*b*(a-1)*(b-1)/4;
}
// 计算剩余的 r 个石子可以包含的长方形个数
long long calExtRects(long long t, long long r) {
return t*r*(r-1)/2;
}

int main() {
int T, n, m, k;
scanf("%d ", &T);
for (int caseIdx = 1;caseIdx <= T;caseIdx++) {
scanf("%d%d%d", &n, &m, &k);
// 保证 n <= m
if (n > m) {
int t = n;
n = m;
m = t;
}
long long max = 0;
int maxa = (int)ceil(sqrt((double)k));
if (maxa > n) maxa = n;
for (int a = 2;a <= maxa;a++){
int r = k % a;
int b = (k - r) / a;
if (b > m) continue;
long long cnt = calRects(a, b);
if (r >= 2) {
// 添加剩余的 r 个石子为新一列
int t = b;
if (b == m || (a < n && a > b && r < b)) {
// 需要将石子添加为新一行的情况
t = a;
}
cnt += calExtRects(t, r);
}
if (cnt > max) max = cnt;
}
printf("Case #%d: %lld\n", caseIdx, max);
}
return 0;
}
posted @ 2013-04-09 00:20 CYJB 阅读(...) 评论(...) 编辑 收藏