这个我们利用FFT很容易就可以想到一个n*logn*k的算法,,,显然不足以通过此题
然而,卷积运算具有结合律,所以我们可以用快速幂加速计算。。。复杂度为 n*logn*log(k)
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define FIN freopen("input.txt","r",stdin)
#define fuck(x) cout<<x<<endl
const double eps=1e-7;
const int MX=1111*1111*2;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
typedef long long LL;
typedef pair<LL,LL> PLL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int n,m;
const double pi = acos(-1.0);
int len,res[MX],mx;//开大4倍
struct Complex
{
double r,i;
Complex(double r=0,double i=0):r(r),i(i) {};
Complex operator+(const Complex &rhs)
{
return Complex(r + rhs.r,i + rhs.i);
}
Complex operator-(const Complex &rhs)
{
return Complex(r - rhs.r,i - rhs.i);
}
Complex operator*(const Complex &rhs)
{
return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
}
} va[MX],vb[MX],ve[MX],vc[MX];
void rader(Complex F[],int len) //len = 2^M,reverse F[i] with F[j] j为i二进制反转
{
int j = len >> 1;
for(int i = 1; i < len - 1; ++i)
{
if(i < j) swap(F[i],F[j]); // reverse
int k = len>>1;
while(j>=k)
{
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
}
void FFT(Complex F[],int len,int t)
{
rader(F,len);
for(int h=2; h<=len; h<<=1)
{
Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
for(int j=0; j<len; j+=h)
{
Complex E(1,0); //旋转因子
for(int k=j; k<j+h/2; ++k)
{
Complex u = F[k];
Complex v = E*F[k+h/2];
F[k] = u+v;
F[k+h/2] = u-v;
E=E*wn;
}
}
}
if(t==-1) //IDFT
for(int i=0; i<len; ++i)
F[i].r/=len;
}
void Conv(Complex a[],Complex b[],int len) //求卷积
{
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) a[i].r=(a[i].r>0.5);
}
void init(Complex *a,Complex *b)
{
for(int i=0; i<len; i++)a[i].i=0,a[i].r=b[i].r;
}
void _init(Complex *a)
{
for(int i=0; i<len; i++)a[i].i=0;
}
void Out(int a) //输出外挂
{
if(a>9)Out(a/10);
putchar(a%10+'0');
}
void print(Complex *a)
{
for(int i=0; i<len; i++)printf("%d ",(int)(a[i].r+0.5));
cout<<endl;
}
void gao()
{
len=1;
while(len<mx)len<<=1;
// cout<<len<<endl;
ve[0].r=1;
while(m)
{
//cout<<m<<endl;
if(m&1)
{
init(vc,va);
Conv(ve,va,len);
_init(ve);
//cout<<"ve"<<endl;
//print(ve);
init(va,vc);
if(m==1)break;
}
init(vb,va);
//cout<<"va1"<<endl;
// print(va);
// cout<<"vb"<<endl;
//print(vb);
Conv(va,vb,len);
_init(va);
//cout<<"va2"<<endl;
// print(va);
m>>=1;
}
for(int i=0; i<len; ++i)res[i]=ve[i].r + 0.5;
for(int i=0; i<len; i++)if(res[i]) Out(i),putchar(' ');
cout<<endl;
}
int main()
{
while(cin>>n>>m)
{
int maxn=0;
for(int i=0; i<n; i++)
{
int x;
scanf("%d",&x);
maxn=max(maxn,x);
va[x].r=1;
va[x].i=0;
}
mx=maxn*m+1;
gao();
}
return 0;
}