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
Analysis:
Constant violence enumerates the results of each type of face value, the range of the face value, which can have a range of i-1, A[i] is an array of constant changes to the face value of the enumeration, so a res[i] array is required to record the correct answer
Dp[i] indicates that the result of reaching I requires a minimum number of sheets.
Explanation of the Great God
/*This topic know is deep search, but the upper bound of the value of the stamp is not OK in the deep search, only know the Nether is a > before, here is the best in using DP to solve the upper bound of deep search, and the current stamp can be taken to the continuous maximum value*//*① Search. For each step, enumerate the value of the stamp, and then search for the next stamp value and update the optimal solution. ② full backpack determine the search scope. Assuming that the value of the stamp is now enumerated to the value of i-1, the value of the stamp is a[i-1], and the maximum continuous of the former i-1 stamp is x, then the value of the stamp I stamps is [a[i-1]+1,x+1]; Suppose there are now N stamps, how can we get the maximum continuous value? Use F[i] to record the minimum number of stamps required to reach the value I, initialized to a maximum value. Then use the full backpack to calculate the value of F[i], starting from 0, the first f[i]>n, then the i-1 is the maximum continuous value. */#defineN 50#include<iostream>using namespacestd;#defineINF 500#include<cstdio>#include<cstring>intb[n],ans=0, A[n],f[inf];intn,k;voidDfsintm) {memset (F,0x3f,sizeof(f)); f[0]=0; inti; for(i=1; i<=inf;++i) { for(intj=1; j<=m&&a[j]<=i;++j) F[i]=min (f[i],f[i-a[j]]+1);/*A complete backpack is a place where you can swap the contents of an item's space inside and out, anyway .*/ if(f[i]>n)/*the maximum value that the current m stamps can fetch*/{i--; if(i>ans) {ans=i; for(intL=1; l<=m;++l) b[l]=A[l]; } Break; } } if(m==k)return; for(intj=i+1; j>a[m];--j) {/*the scope of the next stamp*/a[m+1]=J; DFS (M+1); }} intMain () {scanf ("%d%d",&n,&k); a[1]=1; DFS (1); for(intI=1; i<=k;++i) printf ("%d", B[i]); printf ("\ n"); printf ("max=%d\n", ans); return 0;}View Code
A positive knock.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intmaxn=2000000+ -;Const intinf=0x3f3f3f3f;intDP[MAXN];inta[ -],res[ -];intans=0;intn,k;voidWork () {dp[0]=0; intI=0; while(dp[i]<=N) {i++; Dp[i]=INF; for(intj=1; J<=k && a[j]<=i; J + +) Dp[i]=min (dp[i],dp[i-a[j]]+1); } if(I-1>ans) {ans=i-1; for(intI=1; I<=k; i++) {Res[i]=A[i]; } }}voidDfsintm) { if(m==k+1) {work (); return ; } for(intj=a[m-1]+1; j<=a[m-1]*n+1; J + +) {A[m]=J; DFS (M+1); }}intMain () {scanf ("%d%d",&n,&k); a[1]=1; DFS (1); for(intI=1; I<=k; i++) printf ("%d", Res[i]); printf ("\nmax=%d\n", ans);}View Code
1047 Stamp value Design (DFS+DP)