E. Chocolate Bar

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/598/problem/E

Description

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 nm 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 nm and k to process.

Each of the next t lines contains three integers nm 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 nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

Sample Input

4
2 2 1
2 2 3
2 2 2
2 2 4

Sample Output

5
5
4
0

HINT

题意

给你n*m的巧克力,然后你想吃面积恰好为k的巧克力,问你怎么样才能花费最少吃到他

每次花费是你切的这刀的长度的平方

题解:

记忆化搜索,然后中间直接暴力dfs就好了

代码

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int dp[][][]; int dfs(int n,int m,int k)
{
if(!k||n*m==k)return ;
if(dp[n][m][k]!=-)return dp[n][m][k];
int& minn = dp[n][m][k];
minn = 9999999LL;
for(int i=;i<n;i++)
for(int j=;j<=k;j++)
minn = min(minn,dfs(i,m,j)+dfs(n-i,m,k-j)+m*m);
for(int i=;i<m;i++)
for(int j=;j<=k;j++)
minn = min(minn,dfs(n,i,j)+dfs(n,m-i,k-j)+n*n);
return minn;
}
int main()
{
int t;scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
printf("%d\n",dfs(n,m,k));
} }

最新文章

  1. jquery_DOM笔记4
  2. 中文编程语言Z语言开源正式开源!!!
  3. mysql replace into用法与坑
  4. PyCharm 134 单元测试输出大量空行解决方案
  5. C#winform项目添加引用编译文件
  6. xUtils框架介绍(三)
  7. oracle表空间建立与用户创建删除
  8. backbone初次使用及hello world
  9. Git 和 SVN之间的五个基本区别
  10. android取得所在位置的经纬度
  11. ruby正则表带式对象使用备忘
  12. RMQ区间最大值与最小值查询
  13. Sql语句基础练习(一)
  14. #7 找出数组中第k小的数
  15. CSS文本(Text)属性-----letter-spacing和text-align
  16. L327 找灵魂伴侣
  17. C#窗体模拟键盘按键(组合键)产生事件 ---- 通过keybd_event()函数
  18. day21-类的组合
  19. 【MYSQL】语法复习
  20. Kafka下的生产消费者模式与订阅发布模式

热门文章

  1. linux面试题3
  2. android中ViewHolder通用简洁写法
  3. cocos2d-x 详解之 CCAction(动作)
  4. MorningSale 介绍
  5. 如何将自定义RPM包加入YUM
  6. 在虚拟机中安装windows
  7. Windows 8.1及Windows8 JDK环境变量配置
  8. css 超出部分显示省略号
  9. 【Spark学习】Apache Spark for 第三方Hadoop分发版
  10. 使用SignalR实现比特币价格实时刷新