AcWing 168. 生日蛋糕
原题链接
中等
作者:
黄亦玫
,
2020-06-28 07:47:57
,
所有人可见
,
阅读 446
#include <iostream>
#include <cmath>
using namespace std;
const int N = 25, INF = 1e9;
int h[N], r[N];
int n, m;
int minv[N], mins[N];
int res = INF;
void dfs(int u, int v, int s)
{
if(s + mins[u] >= res)return ;
if(v + minv[u] > n)return ;
if(s + 2 * (n - v) / r[u + 1] >= res)return ; // 踩坑了 因为有除法所以顺序不能颠倒s+2*/r[u+1]*(n - v);
if(!u)
{
if(v == n)res = s;
return ;
}
for(int R = min(r[u + 1] - 1, (int)sqrt(n - v)); R >= u; R --)
for(int H = min(h[u + 1] - 1, (n - v) / (R * R)); H >= u; H --)
{
int t = 0;
if(u == m)t = R * R;
h[u] = H, r[u] = R;
dfs(u - 1, v + R * R * H, s + 2 * R * H + t);
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
minv[i] = minv[i - 1] + i * i * i;
mins[i] = mins[i - 1] + 2 * i * i;
}
r[m + 1] = INF;
h[m + 1] = INF;
dfs(m, 0, 0);
if(res == INF)res = 0;
cout << res << endl;
return 0;
}