https://www.luogu.com.cn/problem/P5752
https://codeforces.com/contest/598/problem/E
cf这个题考虑dp预处理,状态是三维的,转移是分割方案和所分块需要获得的巧克力数量。最后题目多次询问可以o(1)快速查询的
// Problem: E. Chocolate Bar
// Contest: Codeforces - Educational Codeforces Round 1
// URL: https://codeforces.com/contest/598/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
# define int long long
#define ull unsigned long long
#define pii pair<int,int>
#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl "\n"
#define debug1(x) cerr<<x<<" "
#define debug2(x) cerr<<x<<endl
const int N = 35;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m,k;
int dp[N][N][N*N];
int a[N];
//考虑区间dp,记忆化搜索
//提前预处理答案,O(1)查询
//30*30*900(状态)*(60*30)转移=48600000*30(1.2e8)
//不会跑满,加上记忆化和行列地位对等的剪枝
int dfs(int x,int y,int cnt){
if(cnt==0||x*y==cnt)return 0;
int &res=dp[x][y][cnt];
//res=1e18;
if(res!=-1)return res;
if(dp[y][x][cnt]!=-1)return res=dp[y][x][cnt];
res=1e18;
for(int i=1;i<=x-1;i++){
int now=cnt;
for(int c=0;c<=now;c++){
res=min(res,dfs(i,y,c)+dfs(x-i,y,now-c)+y*y);
}
}
for(int i=1;i<=y-1;i++){
int now=cnt;
for(int c=0;c<=now;c++){
res=min(res,dfs(x,i,c)+dfs(x,y-i,now-c)+x*x);
}
}
dp[y][x][cnt]=res;
return res;
}
void solve(){
cin>>n>>m>>k;
cout<<dfs(n,m,k)<<endl;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
memset(dp,-1,sizeof dp);
int t;
cin>>t;
// t=1;
while (t--) {
solve();
}
return 0;
}
标签:long,记忆,https,搜索,problem,com,dp,define
From: https://www.cnblogs.com/mathiter/p/18100543