#9TLE求调

P1731 [NOI1999] 生日蛋糕

需剪枝: ```cpp #include<bits/stdc++.h> using namespace std; int n, m; const int manx = 1e6; int ans = 987654321; int mins[manx],minv[manx]; inline void dfs(int c,int v, int s,int h,int r) { if(c == 0){ if(v == n) ans = min(ans ,s); return; } if(v + minv[c] > n) return; if(s + mins[c] > ans) return; if(s + 2 *(n-v) / r > ans ) return; for (int i = r - 1; i >= c; --i) { if(c == m) s = i * i; int Maxh = min(h - 1,(n - v - minv[c - 1])/(i * i)); for (int j = Maxh; j >= c; --j) dfs(c - 1, v + i * i * j, s + 2 * i * j,j,i); }} int main() { scanf("%d%d", &n, &m); int MaxR = sqrt(n); for(int i = 1;i <= n; i++){ minv[i] = minv[i-1] + i * i * i; mins[i] = mins[i-1] + 2 * i * i; } dfs(m,0,0,n,MaxR); if(ans == 987654321) cout<<0<<endl; else cout<<ans<<endl; return 0; }
by dream_dad @ 2024-08-30 19:16:24


@[Vlang](/user/1054383)
by dream_dad @ 2024-08-30 19:16:38


没剪枝,包TLE的
by ZhouZhaoYuan0620 @ 2024-09-22 15:36:57


|