51nod 1101 for change [complete backpack deformation/unlimited pieces available]

Base Time Limit: 1 second Space Limit: 131072 KB Score: 20Difficulty  : Level 3 Algorithm Questions
 collect
 focus on
How many different ways are there to exchange N dollars for change? Currency values ​​include 1 2 5 cents, 1 2 5 cents, and 1 2 5 10 20 50 100 yuan.
 
For example: 5 cents for change, there are the following 4 exchange methods:
1, 5 1 point
2. 1 2 points 3 1 points
3, 2 2 points and 1 1 point
4, 1 5 points
(As the result can be large, output the result of Mod 10^9 + 7)
Input
Enter a number N, N = 100 means 1 yuan. (1 <= N <= 100000)
Output
Output the result of Mod 10^9 + 7
Input example
5
Output example
4
【Code】:
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<stack>
#include<algorithm>
#define maxn 105
#define maxm 50005
#define INF 0x3f3f3f3f
#define ll long long
#define MOD 1000000007
using namespace std;
/*
Unlimited per item!
Its practical significance: take out each coin in the available coin set in turn, for example, the coin currently taken out is a 3-yuan coin, let val(i) be the number of ways to make up i-dollar, then val(i) will naturally increase val (i-3), because of the existence of the 3-dollar coin, all methods that can make up (i-3) yuan can be made by adding a 3-dollar coin to make i-dollar.
* / 
int w [ 13 ] = { 1 , 2 , 5 , 10 , 20 , 50 , 100 , 200 , 500 , 1000 , 2000 , 5000 , 10000 };
int v [ 7 ] = { 1 , 2 , 5 , 10 , 20 , 50 };
int d [ 110000 ];

intmain ()
{
    int n,c;
    scanf("%d",&n);
    d[0]=1;
    for(int i=0;i<13;i++)
        for(int j=w[i];j<=n;j++)
            d[j]=(d[j]+d[j-w[i]])%MOD;
    printf("%d\n",d[n]);
}
/*
dp[i][j]:
1. Keep the amount the same, reduce the number of currencies
2. Keep the number of currencies and reduce the amount of money
if(i>=j)    dp[i][j] = dp[i][j-1] + dp[i-j][j]
if(i<j)     dp[i][j] = dp[i][i];
There are two variables that can affect the number of exchange types,
The first is the number of currencies, and the second is the amount, so the recursion is carried out in two aspects,
1. Keep the amount the same, reduce the number of currencies
2. Keep the number of currencies and reduce the amount of money.
So a binary tree is formed.
This is the discrete case, in calculus we can also see examples,
For a function with 2 variables to differentiate, when doing partial differentiation,
Keep the first variable first, take the derivative of the second variable,
 Keep the second variable and take the derivative of the first variable.


dp[i] (i is the currency value of the coin, i.e. 1 <= i <= N) The sum of the two scenarios when coins[j] ( 1 <= j <= 13) (put in + not put in)
For example, coins[] = {1, 2, 5} dp[2] has two schemes,
dp[2][0] = dp[1][0] = 1;
dp[2][1] = dp[0][2] (result of directly selecting a 2-cent coin) + dp[1][0] (result of 2 1-cent coins) = 2
dp[2][2] = 0 + dp[2][1] = 2 (because the coin with the value of 5 cannot be put in, so you can only choose not to put it)
PS: MOD = (10^9 + 7) MOD the result of each step>.<
*/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324929093&siteId=291194637