过样例,我现在还没打算剪枝优化,现在只想过 Testcase #1,样例过了:
// Problem: P1731 [NOI1999] 生日蛋糕
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1731
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Coding by 2044_space_elevator
#include <iostream>
#include <cmath>
using namespace std;
int N, M, ans = 114514;
void dfs(int nowS, int nowN, int gs, int H, int R) {
if (gs == M && nowN == N) {
cout << nowS << ' ' <<gs <<' ' << H <<' ' << R <<endl;
ans = min(ans, nowS);
return;
}
if (gs == M) {
return;
}
if (nowS > ans) {
return;
}
if (nowN > N) {
return;
}
for (int i = H + 1; i <= N; i++) {
for (int j = R + 1; j <= N; j++) {
if (gs == 0) {
dfs(
nowS + i * j * 2 + i * j * j,
nowN + i * j * j,
gs + 1,
i,
j
);
} else {
dfs(
nowS + i * j * 2 + j * j - R * R,
nowN + i * j * j,
gs + 1,
i,
j
);
}
}
}
}
int main() {
cin >> N >> M;
dfs(0, 0, 0, 0, 0);
cout << ans;
}
(我是从上往下算的)。