A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.
The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack.
The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.
The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.
Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.
3 2
1 2 3
2 3 4 5 6
5 5
1 1 1 1 1
5
3 3
3 5 11
9 11 13 15 17 19 21 25 27 33
如果给出n个数, 每个数为xi, 那么a[xi]++, 然后对a做k次fft就可以了。
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; struct complex { double r,i; complex(double _r = 0.0,double _i = 0.0) { r = _r; i = _i; } complex operator +(const complex &b) { return complex(r+b.r,i+b.i); } complex operator -(const complex &b) { return complex(r-b.r,i-b.i); } complex operator *(const complex &b) { return complex(r*b.r-i*b.i,r*b.i+i*b.r); } }; void change(complex y[],int len) { int i,j,k; for(i = 1, j = len/2;i < len-1; i++) { if(i < j)swap(y[i],y[j]); k = len/2; while( j >= k) { j -= k; k /= 2; } if(j < k) j += k; } } void fft(complex y[],int len,int on) { change(y,len); for(int h = 2; h <= len; h <<= 1) { complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h)); for(int j = 0;j < len;j+=h) { complex w(1,0); for(int k = j;k < j+h/2;k++) { complex u = y[k]; complex t = w*y[k+h/2]; y[k] = u+t; y[k+h/2] = u-t; w = w*wn; } } } if(on == -1) for(int i = 0;i < len;i++) y[i].r /= len; } const int maxn = 2e6+5; complex x1[maxn], x2[maxn]; int a[maxn], b[maxn]; void cal(int *a, int *b, int &lena, int &lenb) { int len = 1; while(len<lena+lenb) len<<=1; for(int i = 0; i<=lenb; i++) { x1[i] = complex(b[i], 0); } for(int i = lenb+1; i<len; i++) x1[i] = complex(0, 0); for(int i = 0; i<=lena; i++) { x2[i] = complex(a[i], 0); } for(int i = lena+1; i<len; i++) x2[i] = complex(0, 0); fft(x1, len, 1); fft(x2, len, 1); for(int i = 0; i<len; i++) x1[i] = x1[i]*x2[i]; fft(x1, len, -1); for(int i = 0; i<=lena+lenb; i++) b[i] = (int)(x1[i].r+0.5); for(int i = 0; i<=lena+lenb; i++) if(b[i]>0) b[i] = 1; lenb += lena; } int main() { int n, k, x; cin>>n>>k; for(int i = 0; i<n; i++) { scanf("%d", &x); a[x]++; } b[0] = 1; int lena = 1000, lenb = 0; while(k) { if(k&1) { cal(a, b, lena, lenb); } if(k>1) { cal(a, a, lena, lena); } k>>=1; } for(int i = 0; i<=lena+lenb; i++) { if(b[i]) { printf("%d ", i); } } cout<<endl; return 0; }