Codeforces 598E E. Chocolate Bar (interval dp)

Source: Internet
Author: User

Topic Links:

E. Chocolate Bar

Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You had a rectangular chocolate bar consisting of n x m single squares. You want to eat exactly k -squares, so-may need-break the chocolate bar.

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

For example, if you had a chocolate bar consisting Of 2x3 unit squares then you can Break it horizontally and get Two 1x3 pieces (the cost of such breaking Is 32 = 9), or you can break it vertically in both ways and get, pieces: 2x1 and 2x2  (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 is 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 c4>n, m and K to process.

Each of the next t lines contains three integers n, m and K (1≤ n, m ≤ 30, 1≤ kmin(n· M, ())-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.

Examplesinput
4
2 2 1
2 2 3
2 2 2
2 2 4
Output
5
5
4
0
Note

In the first query for the sample one needs to perform, breaks:

    • To split 2×2 bar into the pieces of 2x1 (cost is 22 = 4),
    • To split the resulting 2x1 into a 1x1 pieces (Cost is 12 = 1).

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.

AC Code:

#include <bits/stdc++.h>using namespacestd;Const intinf=0x7f7f7f7f;intdp[ +][ +][ -];intn,m,k;intGet_ans () {memset (Dp,inf,sizeof(DP));  for(intI=0; i<= -; i++)    {         for(intj=0; j<= -; j + +) {dp[i][j][0]=0; dp[0][i][j]=0; dp[i][0][j]=0; }    }     for(intI=1; i<= -; i++)    {         for(intj=1; j<= -; j + +)        {            intX=min (I*j, -); if(i*j<= -) dp[i][j][i*j]=0;  for(intk=1; k<=x;k++)            {                 for(intv=1; v<=i/2; v++)                {                    intNum=min (k,v*K);  for(intu=0; u<=num;u++) {Dp[i][j][k]=min (dp[i][j][k],dp[v][j][u]+dp[i-v][j][k-u]+j*K); }                }                 for(intv=1; v<=j/2; v++)                {                    intNum=min (k,v*i);  for(intu=0; u<=num;u++) {Dp[i][j][k]=min (dp[i][j][k],dp[i][v][u]+dp[i][j-v][k-u]+i*H); }                }            }        }    }}intMain () {intT; scanf ("%d",&t);    Get_ans ();  while(t--) {scanf ("%d%d%d",&n,&m,&k); printf ("%d\n", Dp[n][m][k]); }    return 0;}

Codeforces 598E E. Chocolate Bar (interval dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.