Title Description
Description
Given an envelope, a maximum of only N stamps can be pasted, calculated in the case of a given K (N+K≤40) stamp (assuming that all stamps are sufficient), how to design the face value of the stamp, the maximum value max, so that each postage value between the 1~max can be obtained.
For example, n=3,k=2, if the face value is 1 points, 4 points, then the 1 cent between the points of each postage value can be obtained (of course, 8, 9 and 12 points), if the face value of 1 points, 3 points, the 1 points between the value of each postage can be obtained. It can be verified that when n=3,k=2, 7 points is the maximum number of consecutive postage that can be obtained, so max=7, the face value is 1 points, 3 points respectively.
Enter a description
Input Description
N and K
Output description
Output Description
The face value of each type of stamp, the maximum number of denominations that can be reached consecutively. The data guarantees that the answer is unique.
Sample input
Sample Input
3 2
Sample output
Sample Output
1 3
Max=7
Dynamic planning to deepen the search, each enumeration of a postage stamp value of a[i] stamps, the maximum possible value between a[i]+1 to A[i]*k+1. The idea is to enumerate all possible combinations of stamps, that is, the enumeration face value, each combination can be obtained by the number of consecutive maximum value, and record the current combination of methods, you can constantly update the maximum face value.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intN,K,MAXN;inta[ $];intnum[1605];intans[ $];voiddp () {intI=0; num[0]=0; while(num[i]<=N) {i++; Num[i]=99999; for(intj=0; j<k&&i>=a[j];j++) { if(num[i-a[j]]+1<Num[i]) num[i]=num[i-a[j]]+1; } } if(I-1>MAXN) {MAXN=i-1; for(intj=0; j<k;j++) Ans[j]=A[j]; }}voidDfsintStep) { if(step==k) {DP (); return; } for(inti=a[step-1]+1; i<=a[step-1]*n+1; i++) {A[step]=i; DFS (step+1); }}intMain () {scanf ("%d%d",&n,&k); MAXN=0; DFS (0); for(intI=0; i<k;i++) printf ("%d", Ans[i]); printf ("\nmax=%d", MAXN); return 0;}View Code
Detailed sticking here
CODEVS1047 Stamp Denomination Design