HDUOJ-------2844Coins

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5936    Accepted Submission(s): 2458


Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
 

 

Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
 

 

Output
For each test case output the answer on a single line.
 

 

Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
 

 

Sample Output
8 4
 

 

Source

 代码: 简单的多重背包,但是恶心的地方是m居然可以为负数....是不是很伤不起呀...哈哈

 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<stdlib.h>

 4 const int maxn=105;

 5 struct price

 6 {

 7     int val;

 8     int num;

 9 };

10 price sta[maxn];

11 int dp[100010];

12 int main()

13  {

14     int n,m,i,j,ans;

15     while(scanf("%d%d",&n,&m),n+m)

16     {

17         

18         for(i=0;i<n;i++)

19             scanf("%d",&sta[i].val);

20         for(ans=i=0;i<n ;i++)

21         {

22           scanf("%d",&sta[i].num);

23             ans+=sta[i].val*sta[i].num ;

24         }

25         int tem=ans<m?ans:m;

26         if(tem<0)

27         {

28             printf("0\n");

29         continue;

30         }

31         memset(dp,-1,sizeof(dp[0])*(tem+1));

32         dp[0]=0;

33         for(i=0;i<n;i++)

34         {

35             if(sta[i].num*sta[i].val>=tem)  //完全背包

36             {

37                 for(j=sta[i].val ;j<=tem;j++)

38                 {

39                     if(dp[j-sta[i].val]>-1&&dp[j]<dp[j-sta[i].val]+sta[i].num)

40                         dp[j]=dp[j-sta[i].val]+sta[i].num;

41                 }

42             }

43             else

44             {

45                 int k=1;

46                 while(sta[i].num>=k)

47                 {

48                     for(j=tem ;j>=k*sta[i].val ; j--)

49                     {

50                       if(dp[j-k*sta[i].val]>-1&&dp[j]<dp[j-k*sta[i].val]+k)

51                         dp[j]=dp[j-k*sta[i].val]+k;

52                     }

53                     sta[i].num-=k;

54                     k<<=1;

55                 }

56                    

57                  for(j=tem ;j>=sta[i].num ;j--)

58                  {

59                     if(j<sta[i].num*sta[i].val) break;

60                     if(dp[j-sta[i].num*sta[i].val]>-1&&dp[j]<dp[j-sta[i].num*sta[i].val]+sta[i].num)

61                         dp[j]=dp[j-sta[i].num*sta[i].val]+sta[i].num;

62                  }

63 

64             }

65         }

66         ans=0;

67         for(i=1;i<=tem;i++)

68         {

69             if(dp[i]!=-1)ans++;

70         }

71         printf("%d\n",ans);

72     }

73     return 0;

74  }
View Code

 

你可能感兴趣的:(HDU)