3 10 1 2 5
10
妥妥的母函数水过。。有点慢 跑了190MS
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cctype> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> #include <list> #define maxn 10010 #define ll long long #define INF 0x3f3f3f3f #define pp pair<int,int> using namespace std; ll a[maxn],b[maxn],v[26],p,n; void solve() { memset(a,0,sizeof(a)); a[0]=1; for(int i=0;i<n;i++) { memset(b,0,sizeof(b)); for(int j=0;+j*v[i]<=p;j++) for(int k=0;k+j*v[i]<=p;k++) b[k+j*v[i]]+=a[k]; memcpy(a,b,sizeof(b)); } printf("%lld\n",a[p]); } int main() { while(~scanf("%lld%lld",&n,&p)) { for(int i=0;i<n;i++) scanf("%lld",v+i); solve(); } return 0; }
原文地址:http://blog.csdn.net/qq_16255321/article/details/41015031