题目链接:http://codeforces.com/contest/598/problem/E

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 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.

Examples
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.

题意:给你一块n*m的巧克力,取出k单位面积最小需要花费的价值;

思路:%q的代码

   一直不知道该怎么写,然后看他dfs,记忆话搜索,果然好写多了;

   dp,dp[i][j][k]表示当前i行j列大小的巧克力,取出大小为k的最小花费;

   枚举切割的行或者列,两个不同面,所取的面积大小;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e7+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+;
/// 数组大小
int dp[][][];
int dfs(int n,int m,int k)
{
if(k>n*m)return inf;
if(dp[n][m][k]<inf)return dp[n][m][k];
if(k==||k==n*m)return dp[n][m][k]=;
for(int i=;i<=n/;i++)
{
for(int j=;j<=(i*m,k);j++)
{
int x=dfs(n-i,m,j);
int y=dfs(i,m,k-j);
dp[n][m][k]=min(dp[n][m][k],x+y+m*m);
}
}
for(int i=;i<=m/;i++)
{
for(int j=;j<=(i*n,k);j++)
{
int x=dfs(n,m-i,j);
int y=dfs(n,i,k-j);
dp[n][m][k]=min(dp[n][m][k],x+y+n*n);
}
}
return dp[n][m][k];
}
int main()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
for(int k=;k<=min(i*j,);k++)
dp[i][j][k]=inf;
}
}
int T;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
printf("%d\n",dfs(n,m,k));
}
return ;
}

最新文章

  1. MySQL 安装 + 精简 + 配置
  2. 需求:输入一个年份和月份 ,显示当前月日情况 ,星期数要对应准确 * 1.1900年1月1号开始 * 2.星期 : 直接用总天数对7求余数 31 28 59 / 7 = 5 * 3.以\t来个开
  3. Java接口中的方法
  4. 背水一战 Windows 10 (8) - 控件 UI: StateTrigger
  5. HDOJ(HDU) 2521 反素数(因子个数~)
  6. Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
  7. JavaScript中遍历数组 最好不要使用 for in 遍历
  8. Java设计模式之《组合模式》及应用场景
  9. 一种基于主板BIOS的身份认证方案及实现
  10. ANT不完全总结,包含各种命令,ant例子等,转自:http://lavasoft.blog.51cto.com/62575/87306
  11. Elasticsearch: 权威指南 &#187; 深入搜索 &#187; 多字段搜索 &#187; 多数字段 good
  12. 2.2Bind建立配置文件和实体的映射「深入浅出ASP.NET Core系列」
  13. 漫画 | Redis常见面试问题(一)
  14. Vue2.0 新手完全填坑攻略—从环境搭建到发布
  15. Haproxy+Heartbeat 高可用集群方案操作记录
  16. IDEA如何刷新pom文件
  17. 【BZOJ】【4152】【AMPZZ2014】The Captain
  18. Extjs 中callParent的作用
  19. 【BZOJ】4293: [PA2015]Siano 线段树上二分
  20. [实战]MVC5+EF6+MySql企业网盘实战(5)——页面模板

热门文章

  1. Gson的两种解析用法
  2. [py]flask动态展示主机内存图
  3. jquery.dataTables表格中的内容怎么设置让它不自动换行
  4. SSM请求的响应
  5. PAT A+B for Polynomials[简单]
  6. angular.element 动态添加和删除元素
  7. 记两个国外CTF的弱pwn
  8. 课堂练习Complex类
  9. 远程桌面时出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正
  10. java tar.gz文件生成