Coin Exchange Issues
Time limit:2000/1000ms (java/other) Memory limit:65536/32768k (java/other)
Total Submission (s): accepted submission (s): the
font:times New Roman | Verdana | Georgia
Font size:←→
Problem DescriptionIn a country there are only 1 cents, 2 cents, 3 cents, and there are many ways to exchange money n coins. Please write a procedure to figure out how many kinds of methods are in common.
InputOnly one positive integer per line n,n less than 32768.
OutputFor each input, the output conversion method number.
Sample Input
2934
12553
Sample Output
718831
13137761
Because it's a multiple knapsack problem, the maximum number of split numbers is removed, so you can use the parent function (which requires a table to close the timeout). )
Multi-backpack to do with the above dividing the problem is similar, only here can only use 1 2 3 of these three numbers to gather
Parent Function
#include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <queue > Using namespace std; int main () { long long int a1[55555]; Long long int a2[55555]; int n ; int i,j,k; a1[0]=1; a2[0]=0; for (i=1;i<=32800;i++) { A1[i]=1; a2[i]=0; &NBSP, for (i=2;i<=3;i++) { &N Bsp for (j=0;j<=32800;j++) for (k=0;k+j<=32800;k+=i) { a2[k+j]+=a1[j]; &NBSP ; for (int s=0;s<=32800;s++) { a1[s]=a2[s]; a2[s]=0; } &NBSP} & nbsp while (cin>>n) { cout<<a1[n]< <endl; return 0; }
Backpack
#include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <queue > Using namespace std; int main () {long long int dp[35555]; Dp[0]=1; int i,j; int n; for (i=1;i<=3;i++) {for (j=1;j<=32800;j++) dp[j]+=dp[j-i]; while (cin>>n) {cout<<dp[n]<<endl; return 0; }