题目链接: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 ;
}

Educational Codeforces Round 1 E. Chocolate Bar dp的更多相关文章

  1. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  2. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  3. Educational Codeforces Round 51 D. Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...

  4. Educational Codeforces Round 9 D. Longest Subsequence dp

    D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...

  5. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  6. Educational Codeforces Round 39

    Educational Codeforces Round 39  D. Timetable 令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间 转移就是枚举第\ ...

  7. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  8. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. [py]django第三方分页器django-pure-pagination实战

    第三方分页模块: django-pure-pagination 是基于django的pagination做的一款更好用的分页器 参考 配置django-pure-pagination模块 安装 pip ...

  2. PAT Spell It Right [非常简单]

    1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...

  3. liferay中如何实现自己定义的方法

    大家看到这篇文章是不是很开心啊,我感觉是很开心,我们终于可以按照自己的意愿来写一次代码,在liferay中一些基本的增删改查的代码是自动生成的,然而我们想要实现自己的方法的话,恐怕要费一点劲,你要知道 ...

  4. linux系统安装 dig和nslookup命令

    Fedora / Centos:1.yum install bind-utils Ubuntu: 1.sudo apt-get install dnsutils Debian: 1.2 apt-get ...

  5. 论文笔记:语音情感识别(五)语音特征集之eGeMAPS,ComParE,09IS,BoAW

    一:LLDs特征和HSFs特征 (1)首先区分一下frame和utterance,frame就是一帧语音.utterance是一段语音,是比帧高一级的语音单位,通常指一句话,一个语音样本.uttera ...

  6. 数据仓库基础(十三)Informatica workflow

    本文转载自:http://www.cnblogs.com/evencao/p/3154715.html 看了几天的Informatica ,关于infor的资料也比较少,主要的<商业智能深入浅出 ...

  7. 浅谈为什么一个java源文件中只能有一个public类?

    声明,本篇文章为转载 转载 http://blog.csdn.net/bareheadzzq/article/details/6562211 最近在一个java文件中实现了几个类,其中一个声明为pub ...

  8. php CI 实战教程第一季百度经验杂志

    phpCI实战教程第一季_百度经验杂志_百度经验http://jingyan.baidu.com/magazine/16428 杂志为本人php CI实战教程系列经验 从实际项目使用中写系列实战经验, ...

  9. web应用下的安全问题以及tomcat/nginx对应解决方法(持续更新、亲测可解决问题)

    最近一券商那边扫描反馈了下面几个非业务型安全漏洞,要求解决,如下: XSS 自己写个脚本response的时候对特殊字符进行了处理,或者网上搜下一堆(不要忘了回车.换行). HTML form wit ...

  10. CF#338D. GCD Table

    传送门 简单的中国剩余定理练习. 首先行数一定是$lcm$,然后只要确定最小的列数就能判定解合不合法了. 我们可以得到线性模方程组: $y \equiv 0 \pmod{a_1}$ $y+1 \equ ...