luogu1164小A点菜

uim由于买了一些辅(e)辅(ro)书,口袋里只剩M(M≤10000

餐馆虽低端,但是菜品种类不少,有N(N≤100,第i种卖ai(ai≤1000)。由于是很低端的餐馆,所以每种菜只有一份。

小A奉行“不把钱吃光不罢休”,所以他点单一定刚好吧uim身上所有钱花完。他想知道有多少种点菜方法。

由于小A肚子太饿,所以最多只能等待1秒。

输入输出格式

输入格式:

第一行是两个数字,表示NM

第二行起N个正数ai(可以有相同的数字,每个数字均在1000以内)。

输出格式:

一个正整数,表示点菜方案数,保证答案的范围在int之内。

递推计数

f(i,j)=f(i-1,j-v[i])+f(i-1,j);

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e6+5;
 4 const int INF=1e9+7;
 5 int n,m,v[maxn],f[maxn];
 6 template <class t>void red(t &x)
 7 {
 8     x=0;
 9     int w=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-')
14             w=-1;
15         ch=getchar();
16     }
17     while(ch>='0'&&ch<='9')
18     {
19         x=(x<<3)+(x<<1)+ch-'0';
20         ch=getchar();
21     }
22     x*=w;
23 }
24 void input()
25 {
26     freopen("input.txt","r",stdin);
27 }
28 void read()
29 {
30     red(n);
31     red(m);
32     for(int i=1;i<=n;++i)
33         red(v[i]);
34 }
35 void work()
36 {
37     f[0]=1;
38     for(int i=1;i<=n;++i)
39         for(int j=m;j>=v[i];--j)
40             f[j]=f[j-v[i]]+f[j];
41     printf("%d",f[m]);
42 }
43 int main()
44 {
45     //input();
46     read();
47     work();
48     return 0;
49 }
View Code

猜你喜欢

转载自www.cnblogs.com/Achensy/p/10775599.html