###### 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本
E. Chocolate Bar
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process.

Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

Output

For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

Sample test(s)
input
4
2 2 1
2 2 3
2 2 2
2 2 4

output
5
5
4
0

Note

In the first query of the sample one needs to perform two breaks:

• to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
• to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).

In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int f[32][32][52];
void DP()
{
//初始化
MS(f,63);
for(int i=0;i<=30;i++)
{
for(int j=0;j<=30;j++)f[i][j][0]=0;
}
for(int i=1;i<=30;i++)
{
for(int j=1;j<=30;j++)if(i*j<=50)f[i][j][i*j]=0;
}
//状态转移
for(int i=1;i<=30;i++)
{
for(int j=1;j<=30;j++)
{
int top=min(i*j,50);
for(int k=1;k<=top;k++)//总共要切的份数
{
for(int u=1;u<=i/2;u++)//横着切
{
int topp=min(u*j,k);
for(int kk=0;kk<=topp;kk++)
{
gmin(f[i][j][k],f[u][j][kk]+f[i-u][j][k-kk]+j*j);
}
}
for(int v=1;v<=j/2;v++)//竖着切
{
int topp=min(v*i,k);
for(int kk=0;kk<=topp;kk++)
{
gmin(f[i][j][k],f[i][v][kk]+f[i][j-v][k-kk]+i*i);
}
}
}
}
}
}
int main()
{
DP();int n,m,k;
scanf("%d",&casenum);
for(casei=1;casei<=casenum;casei++)
{
scanf("%d%d%d",&n,&m,&k);
printf("%d\n",f[n][m][k]);
}
return 0;
}
/*
【trick&&吐槽】
1，不要让自己太懒惰呀，这道水题就是差1分钟AC >_<
2，没有AC的原因是我把数组给爆了！初始化的边界问题也要多多注意呀！RE时返回的可能并不是RE

【题意】

【类型】
DP

【分析】

1，大巧克力的长
2，大巧克力的宽
3，吃巧克力的大小
4，横着还是竖着切，在哪里切
5，一侧吃的巧克力的大小
6，另一侧吃的巧克力的大小

AC完全没有压力！这样就AC啦！

gamx(f[i][j][k],f[u][j][kk]+f[i-u][j][k-kk]+j*j);u∈[1,j/2],u表示横着切的位置，我们只需要枚举小的那一半即可，成本自然是j*j
gamx(f[i][j][k],f[i][v][kk]+f[i][j-v][k-kk]+i*i);v∈[1,i/2],v表示竖着切的位置，我们只需要枚举小的的一半即可，成本自然是i*i
kk表示在这一半吃的巧克力的数量，显然kk∈[0,min(u*j,k)]或kk∈[0,min(i*v,k)];

【时间复杂度&&优化】
O(n*m*k*(n+m)*k)

【数据】
10 10 9

*/

#### Codeforces 598E：Chocolate Bar

2015-11-16 12:52:41

2014-08-06 21:51:05

#### CodeForces 617B B. Chocolate【计数+累乘】

2016-03-07 12:53:44

#### bzoj 2016: [Usaco2010]Chocolate Eating （二分）

2016-11-11 10:58:26

#### UVa 1099 分巧克力 ACM解题报告（状压dp）

2015-02-12 22:49:55

#### 解题报告：Educational Codeforces Round 24 D,E,F

2017-07-01 14:07:43

#### Educational Codeforces Round 36 (Rated for Div. 2) 题解

2018-01-13 23:16:24

#### Educational Codeforces Round #41 (D&E)

2018-04-05 10:38:15

#### Educational Codeforces Round 42 E 贪心

2018-04-11 11:57:41

#### [BZOJ2016][Usaco2010]Chocolate Eating（二分）

2016-11-11 23:06:03

## 不良信息举报

【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本