方案数背包
方案数背包
当凑面值为0时,有且只有一种方案,初始化,f[0]=1
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define For(i,a,b) for(register long long i=a;i<=b;i++) 13 #define p(a) putchar(a) 14 #define g() getchar() 15 //by war 16 //2017.20.28 17 using namespace std; 18 long long v,n; 19 long long f[1000010];//80M 20 long long w[3000]; 21 22 void in(long long &x) 23 { 24 long long y=1; 25 char c=g();x=0; 26 while(c<'0'||c>'9') 27 { 28 if(c=='-') 29 y=-1; 30 c=g(); 31 } 32 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 33 x*=y; 34 } 35 void o(long long x) 36 { 37 if(x<0) 38 { 39 p('-'); 40 x=-x; 41 } 42 if(x>9)o(x/10); 43 p(x%10+'0'); 44 } 45 int main() 46 { 47 in(v),in(n); 48 For(i,1,v) 49 in(w[i]); 50 f[0]=1; 51 For(i,1,v) 52 For(j,w[i],n) 53 f[j]+=f[j-w[i]]; 54 o(f[n]); 55 return 0; 56 }