天天看点

CF 598 E. Chocolate Bar dp题的要害是什么呢?

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.

今天组队赛,策略完全搞错了,我把队友的计算几何给偷了,然后貌似精度被卡住了,wa了半天,结果队友没发现,他卡死在另一道求面积的题上,等他发现已经来不及了,我的DP丢在一边,被另一队友当成搜索做了,结果3人全wa,惨败而归。

回来,此题1A。貌似也没花多长时间,思考时间大概15分钟,而且觉得此题不难,甚至还有点水,可惜比赛时看到题目,没有立刻想出解法,反而觉得有一定难度,

跑去做别的题目。 这说明啊,我解决DP问题的能力还是有所欠缺--------很简单的一点:表示不出来就加维。 这证明我思考dp题并没有抓住要害。

题意:给定n*m块连在一起的巧克力,现在要取出k块,已知每次可以水平或竖直的分割(切到底),代价是分割线的平方。问取出k块巧克力需要的最少代价。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=  30  ;


int dp[maxn+4][maxn+4][53];

int n,m,k;

void update(int &ans,int x)
{
    if(x<ans)  ans=x;

}


int DP(int x,int y,int num)
{
    if(~dp[x][y][num])  return dp[x][y][num];

    if(num==0||x*y==num)  return dp[x][y][num]=0;

    dp[x][y][num]=INF;


    for(int x1=1;x1<x;x1++)
    {
        int x2=x-x1;
       for(int v1=0;v1<=num;v1++)
       {
           int v2=num-v1;

           if(x1*y>=v1&&x2*y>=v2)
           update(dp[x][y][num], y*y+  DP(x1,y,v1)+DP(x2,y,v2)   );
       }
    }
    for(int y1=1;y1<y;y1++)
    {
         int y2=y-y1;
          for(int v1=0;v1<=num;v1++)
       {
           int v2=num-v1;

           if(x*y1>=v1&&x*y2>=v2)
            update(dp[x][y][num], x*x+  DP(x,y1,v1)+DP(x,y2,v2)   );
       }
    }

    return dp[x][y][num];

}

int main()
{
    memset(dp,-1,sizeof dp);
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);

        printf("%d\n",DP(n,m,k));

    }

   return 0;
}