DP (positive solution full backpack + tolerance)

Source: Internet
Author: User

Dp

Time limit:10000MS Memory Limit:165888KB 64bit IO Format:%lld &%llu

Submit Status

Description

Coin shopping A total of 4 kinds of coins. The denominations were c1,c2,c3,c4. Someone went to the store to buy something and went to tot. Each time with a di ci coin, buy s
I have something of value. How many methods of payment do you have each time?

Input

The first line C1,c2,c3,c4,tot below the tot line d1,d2,d3,d4,s, where di,s<=100000,tot<=1000

Output

Number of methods per time

Sample Input

1 2 5 10 23 2 3 1 101000 2 2 2 900

Sample Output

 427 


//knapsack problem, repulsion principle
//have to say it's a good question, the knapsack problem should always be, This is mainly the principle of the repulsion, to understand, to give an example of the daily routine encountered this theorem

an examination, A class has 15 people mathematics to get full marks, there are 12 people in Chinese get full marks, and 4 people language, the number is full score, then this class at least a full score of the students how many people?

The idea is that you don't limit the number of 4 coins first, and see how many of the schemes that make up a price are in total
Then, by using the tolerant theorem minus 4 coins exceeding the number limit, there is not much more to find the answer

 dp[i "means | no Limit | The maximum number of payment methods, how the transfer should be
  so just
 dp[res "
 -d1 exceeded limit-D2 over ...-D3 ...-d4 ...
 -(d1,d2,d3)
 + (d1+d2+d3+d4)
Span style= "font-family: Song body; font-size:16px; " >  on the line.

If you don't understand, keep looking.
This problem of the use of the theorem, read a blog, example written very well, otherwise I really do not understand, especially d[i]++ is why
Let us understand the meaning of x=dp[s]-dp[s-(d1+1) *C1]:
X indicates that the number of C1 coins does not exceed D1 and the number of other three coins does not limit the number of schemes to be spelled as S.
We cite examples to illustrate the assumption that there are now two types of coins, with denominations of 1 and 2 respectively,
Then we find the dp:dp[0]=1,dp[1]=1,dp[2]=2,dp[3]=2,dp[4]=3,dp[5]=3,dp[6]=4.
of which dp[3] of the two, respectively, 3=1+1+1=1+2,dp[6] Four kinds are: 6=1+1+1+1+1+1=1+1+1+1+2=1+1+2+2=2+2+2.
Join us now ask for the first coin to use a maximum of two, the second type of Coin unlimited scheme number, according to our said x = dp[6]-dp[6--(2+1) *1]=dp[6]-dp[3]=2.
That is 6=1+1+2+2=2+2+2 two kinds. We found that we removed two species of 1+1+1+1+1+1 and 1+1+1+1+2,
Why is it possible to delete these two types by subtracting dp[3]? Let's see dp[3],3=1+1+1=1+2.
We found that two of the 6 deletions were obtained by adding 3 1 to this f[3].

First, easy to understand the code used for the repulsion
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5typedefLong LongLL;6 #defineMAX 1000057 8LL c[5],d[5];9 LL Dp[max];Ten LL ans; One  A intMain () - { -      for(intI=1; i<=4; i++) thescanf"%lld",&c[i]); -     inttot; -scanf"%lld",&tot); -dp[0]=1; +      for(intI=1; i<=4; i++) -          for(intj=c[i];j<=100000; j + +) +dp[j]+=dp[j-C[i]]; A      while(tot--) at     { - LL Res; -          for(intI=1; i<=4; i++) -         { -scanf"%lld",&d[i]); -d[i]++; in         } -  toscanf"%lld",&res); +ans=Dp[res]; -          the         inti,j,k; *          for(i=1; i<=4; i++) $             if(res>=c[i]*D[i])Panax Notoginsengans-=dp[res-c[i]*D[i]]; -              the          for(i=1; i<=3; i++) +              for(j=i+1; j<=4; j + +) A             if(res>=c[i]*d[i]+c[j]*D[j]) theans+=dp[res-c[i]*d[i]-c[j]*D[j]]; +          for(i=1; i<=2; i++) -              for(j=i+1; j<=3; j + +) $                  for(k=j+1; k<=4; k++) $                 if(res>=c[i]*d[i]+c[j]*d[j]+c[k]*D[k]) -ans-=dp[res-c[i]*d[i]-c[j]*d[j]-c[k]*D[k]]; -         if(res>=c[1]*d[1]+c[2]*d[2]+c[3]*d[3]+c[4]*d[4]) theans+=dp[res-c[1]*d[1]-c[2]*d[2]-c[3]*d[3]-c[4]*d[4]]; - Wuyiprintf"%lld\n", ans); the  -     } Wu     return 0; -}
View Code

This is DFS to deal with the repulsion, concise and clear, the time is the same

1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5typedefLong LongLL;6 #defineMAX 1000057 8LL c[5],d[5];9 LL Dp[max];Ten LL ans; One  A voidDfsintXintk,ll s) - { -     if(s<0)return; the     if(x==5) -     { -         if(k%2) ans-=Dp[s]; -         Elseans+=Dp[s]; +         return; -     } +DFS (x+1, K +1, S (d[x]+1)*c[x]); ADFS (x+1, k,s); at } - intMain () - { -      for(intI=1; i<=4; i++) -scanf"%lld",&c[i]); -     inttot; inscanf"%lld",&tot); -dp[0]=1; to      for(intI=1; i<=4; i++) +          for(intj=c[i];j<=100000; j + +) -dp[j]+=dp[j-C[i]]; the              *      while(tot--) $     {Panax Notoginseng LL Res; -          for(intI=1; i<=4; i++) thescanf"%lld",&d[i]); +  Ascanf"%lld",&res); theans=0; +Dfs1,0, res); -printf"%lld\n", ans); $     } $     return 0; -}
View Code





DP (positive solution full backpack + tolerance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.