[Problem description]
There are m kinds of stamps with known denominations, each of which has n. Q: How many stamps can be combined consecutively with a total of no more than N stamps?
(1 <= m <= 225, 1 <= n <=, 1 <= stamp denomination <=)
Input: the value of the first line: N and M, separated by a space.
Row 2: A [1. m] (denomination), separated by a space.
Output: Maximum number of consecutive denominations
[Sample input]
4 31 2 4
[Sample output]
14
[Problem Analysis]
Programs I write
VaR A: array [0 .. 100] of integer; money, F: array [0 .. 255] of integer; n, m: integer; maxlong: integer; Procedure qsort (L, R: integer); // copy the VaR I from the FPC folder, j, X, Y: integer; begin I: = L; J: = r; X: = A [(L + r)> 1]; repeat while a [I] <X do Inc (I); While x <A [J] Do Dec (j); if not (I> J) then begin Y: = A [I]; A [I]: = A [J]; A [J]: = y; Inc (I); Dec (j); end; until I> J; if l <j then qsort (L, J); If I <r then qsort (I, R); end; Procedure Init; var I: integer; begin readln (n, m); for I: = 1 to M do read (A [I]); qsort (1, m); end; procedure search (k, n, x: integer); // K: the k-th known denomination, N: the maximum number of available known denominations, and X: The currently formed denomination value var I: integer; begin if (n = 0) or (k = 0) Then exit; for I: = n downto 0 do begin Inc (X, a [k] * I ); dec (n, I); Inc (money [x]); search (K-1, n, x); Inc (n, I); Dec (X, A [k] * I); end; Procedure count; var I, Max: integer; begin f [0]: = 0; max: = 0; for I: = 1 to 255 do if money [I]> 0 then begin f [I]: = f [I-1] + 1; if f [I]> MAX then Max: = f [I]; end else f [I]: = 0; writeln (max); end; begin Init; search (m, n, 0); count; end.
Standard procedure:
var a:array [1..100] of integer; money:array [1..2550] of integer; total,n,m,i:integer;procedure search(k,n,x:integer);var i:integer;begin if (n=0)or(k=0) then exit; for i:=n downto 0 do begin x:=x+a[k]*i; n:=n-i; money[x]:=money[x]+1; search(k-1,n,x); x:=x-a[k]*i; n:=n+i; endend;function maxlong:integer;var j,total,max:integer;begin j:=n*a[m]; max:=0; repeat while (money[j]=0)and(j>0) do j:=j-1; total:=0; while (money[j]>0)and(j>0) do begin total:=total+1;j:=j-1 end; if max<total then max:=total ; until j<=0; maxlong:=maxend;begin assign(input,‘word.in‘); reset(input); assign(output,‘word.out‘); rewrite(output); readln(m,n); for i:=1 to m do read(a[i]); total:=0; search(m,n,0); writeln(maxlong)end.
I really feel that this standard procedure is very good, and there are also dynamic planning practices for this question. Since I am learning the Backtracking Method, I will not introduce the DP Method for the moment.