Luogu P1064 Jin Ming's Budget Plan [DP/01 Backpack-Number of Plans]

Background of the title
After uim Shenben got the ra (radium card) of uoi, he immediately took his friend Xiao A to a... restaurant, a very low-end kind.

uim pointed to the price list on the wall (too low-level without a menu) and said "whatever you want".

Title description
However, because uim bought some supplementary (e) supplementary (ro) books, there is only M yuan left in his pocket (M<=10000).

Although the restaurant is low-end, there are many kinds of dishes, there are N kinds (N<=100), and the i-th kind sells ai yuan (ai<=1000). Since it is a very low-end restaurant, there is only one serving of each dish.

Xiao A adheres to the principle of "don't give up until you run out of money", so his order must be just right to spend all the money on uim. He wondered how many ways to order food.

Because Xiao A is too hungry, he can only wait for at most 1 second.

Input and output format
Input format:
The first line is two numbers, representing N and M.

N positive numbers ai from the second line (can have the same number, each number is within 1000).

Output format:
a positive integer, indicating the number of a la carte options, and the range of the answer is guaranteed to be within int.

Input Output Sample
Input Sample #1:
4 4
1 1 2 2
Output Sample #1:
3

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 11000;
const int  mod  = 1000000007;

int n,m;
int a[N];
ll dp[N];//dp[i]代表多少种点菜方法
int main()
{
    while(cin >> n >> m ){
        for(int i=1; i<=n; i++){
            cin >> a[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1; i<=n; i++){
            for(int j=m; j>=a[i]; j--){
                dp[j] = (dp[j] + dp[j-a[i]]);
            }
        }
        cout<<dp[m]<<endl;
    }
    return 0;
}

Guess you like

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