Continuous postage
Time Limit: 1500 MS memory limit: 10000 KB
Total submit: 59 (25 users) accepted submit: 22 (16 users) Page View: 3748
Country g issues n stamps with different denominations and requires that at most M stamps can be pasted on each envelope. The continuous postage problem requires the best design of the stamp face value for the given n and M values so that the postage starts from 1 on one envelope, the maximum continuous postage interval with an increment of 1. For example, when n = 5 and M = 4, five stamps with a nominal value of (, 32) can be pasted with a maximum continuous postage interval of 1 to 70. Programming task: Calculate the optimal design of the stamp face value for the given positive integers m and n.
Input
Each row of the input data has two positive integers, M and N (1 <= n, m <= 9). Finally, 0 indicates the end of the file.
Output
For the positive integers m and n in each row in the input, the maximum continuous postage interval is output.
Sample Input
4 5
0 0
Sample output
70
Http://acm.nankai.edu.cn/p1428.html
Analysis: This is a deep search and DP pruning question. However, this is still inefficient. Of course, I submitted it here.
Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int n,m,f[1000000]={0},max,ma,s[100],limit,flag[1000000]={0};
5 int dp(int k){
6 int i,j;
7 for(j=1;j<=m*limit;j++)
8 f[j]=m+1;
9 for(j=1;j<=m*limit;j++)
10 {
11 for(i=1;i<=k;i++)
12 if(j>=s[i])
13 if(f[j]>f[j-s[i]]+1)f[j]=f[j-s[i]]+1;
14 if(f[j]>m)
15 {
16 if(k==n)ma=j-1;
17 return j;
18 }
19 }
20 return j;
21 }
22
23 void dfs(int x,int k){
24 int i,j,z;
25 if(k<=n)
26 for(i=2;i<=x;i++)
27 if(flag[i]!=1)
28 {
29 s[k]=i;
30 flag[i]=1;
31 if(i>limit)limit=i;
32 z=dp(k);
33 if(k==n&&max<ma)
34 max=ma;
35 dfs(z,k+1);
36 flag[i]=0;
37 }
38 }
39
40 int main(){
41 scanf("%d%d",&m,&n);
42 while(m+n!=0)
43 {
44 max=0;limit=1;
45 int i,j,x;
46 s[1]=1;flag[1]=1;
47 ds(m+1,2);
48 printf("%d\n",max);
49 scanf("%d%d",&m,&n);
50 }
51 return 0;
52 }