n种物品,每个无限个,价值ai,让你选k个,所有可以获得的价值输出
构造一个多项式c,价值是指数,如果出现过,系数就是1,否则是0
然后再搞一个多项式d,里面0的系数设为1
然后快速幂,c每次乘d,d每次乘自己
最后看下c里面有多少项,系数不是0的
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100005
#define MAXN 1000005
#define maxnode 105
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL mod = 313;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const double PI = acos(-1.0);
struct Complex {
double real, image;
Complex(double _real, double _image) {
real = _real;
image = _image;
}
Complex(){}
};
Complex operator + (const Complex &c1, const Complex &c2) {
return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator - (const Complex &c1, const Complex &c2) {
return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator * (const Complex &c1, const Complex &c2) {
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}
int rev(int id, int len) {
int ret = 0;
for(int i = 0; (1 << i) < len; i++) {
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}
Complex A[(1<<21)+5]; /大小要修改,不要忘记
void FFT(Complex* a, int len, int DFT){//对a进行DFT或者逆DFT, 结果存在a当中
//Complex* A = new Complex[len]; 这么写会爆栈
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++) {
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m) {
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++) {
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}
Complex a[(1<<21)+5], b[(1<<21)+5];//对应的乘积的长度不会超过100000, 也就是不超过(1 << 17) = 131072
int c[(1<<21)+5];
int d[(1<<21)+5];
int len1,len2;
void solve(int *x,int &n,int *y,int &m){
int len=1;
while(len<=n+m) len<<=1;
for(int i=0;i<len;i++){
a[i]=Complex(x[i],0);
b[i]=Complex(y[i],0);
}
FFT(a,len,1);
FFT(b,len,1);
for(int i=0;i<len;i++) a[i]=a[i]*b[i];
FFT(a,len,-1);
for(int i=0;i<len;i++){
if(a[i].real>0.5) x[i]=1;
else x[i]=0;
}
n+=m;
}
int main(){
//freopen("in.txt","r",stdin);
int n,k;
while(cin>>n>>k){
mem(c,0);
for(int i=0;i<n;i++){
int x;
scanf("%d",&x);
c[x]=1;
}
mem(d,0);
d[0]=1;
len1=1;
len2=1001;
while(k){
if(k&1) solve(d,len1,c,len2);
solve(c,len2,c,len2);
k>>=1;
}
vector<int> ans;
for(int i=0;i<len1;i++){
if(d[i]) ans.push_back(i);
}
for(int i=0;i<ans.size();i++){
printf("%d ",ans[i]);
}
cout<<endl;
}
return 0;
}