P1021 postage stamp design
Violent search of each denomination.
- Pruning 1: nominal value is incremented, a new denomination \ (\ in [G_ {i -1} +1, n \ cdot sum] \) Why not on the boundary. \ (N-\ CDOT G_ {I-1} + 1'd \) it?
- Pruning 2: \ (= G_1. 1 \) .
dp seeking the maximum nominal value.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, k, f[5005], G[17], ans[17], MAX;
int calc(int dep, int sum) {
memset(f, 0x3f, sizeof f);
f[0]=0;
for (int i=1; i<=dep; ++i) for (int j=G[i]; j<=sum*n; ++j)
f[j]=min(f[j], f[j-G[i]]+1);
for (int i=1; i<=sum*n; ++i) if (f[i]>n) return i-1;
return sum*n;
}
void dfs(int dep, int m, int sum) {
if (dep>k) {
if (MAX<m) {MAX=m; for (int i=1; i<=k; ++i) ans[i]=G[i]; }
return;
}
for (int i=G[dep-1]+1; i<=m+1; ++i) // *1
G[dep]=i, dfs(dep+1, calc(dep, sum+i), sum+i);
}
int main() {
scanf("%d%d", &n, &k);
G[1]=1, dfs(2, n, 1); // *2
for (int i=1; i<=k; ++i) printf("%d ", ans[i]);
printf("\nMAX=%d\n", MAX);
return 0;
}