P1474 monetary system

This is USACO of a DP problem, the difficulty is to improve -.

This question is to tell us currency, ask you then use the money to form a face value of maximum number of kinds of programs. At first glance I would like to use the memory of dfs, and then find out that this problem is very similar to the full backpack, radio pieces can take, but he's different from ordinary transfer equation. And I did not stop to think about the beginning, did not want to come out. Finally concluded dp [j] = dp [j] + dp [j-coin [i]]. Finally, the output dp [T] to.

1. Serious derive the state transition equation, do not dwell apply the formula, you must be deduced few examples, not to fear difficulties, not to bother

2. Note that the loop variable i to j Do not be confused, especially in +

3. Identify different depending on the meaning of the questions dp [] represents the meaning

4. To initialize for dp [], such as the title dp [0] = 1; to be thoughtful

Code

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#define N 1000001
#define ll long long
using namespace std;
int T,n;
int coin[N];
ll ans=0;
ll dp[N];//当前面额存在最大方案数 
int main(){
    cin>>n>>T;
    for(int i=1;i<=n;i++){
        scanf("%d",&coin[i]);
    }    
    dp[0]=1;
    for(int i=1;i<=n;i++){
        for(int j=coin[i];j<=T;j++){
            dp[j]=dp[j]+dp[j-coin[i]];
        }
    }
    cout<<dp[T];
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/china-mjr/p/11247380.html