#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1100000;
const int MOD1=1000007;
const int MOD2=100000009;
const int MAX=1000000000;
const double EPS=0.5;
typedef long long ll;
const ll MOD=1000000007;
const ll INF=10000000010;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
struct Complex{
db r,i;
Complex() {}
Complex(db r,db i):r(r),i(i) {}
Complex operator + (const Complex &t) const {
return Complex(r+t.r,i+t.i);
}
Complex operator - (const Complex &t) const {
return Complex(r-t.r,i-t.i);
}
Complex operator * (const Complex &t) const {
return Complex(r*t.r-i*t.i,r*t.i+i*t.r);
}
}x[N],y[N];
void FFT(Complex x[],int n,int rev) {
int i,j,k,t,ds;
Complex w,u,wn;
for (i=1;i<n;i++) {
for (j=0,t=i,k=n>>1;k;k>>=1,t>>=1) j=j<<1|t&1;
if (i<j) swap(x[i],x[j]);
}
for (i=2,ds=1;i<=n;ds=i,i<<=1) {
w=Complex(1,0);wn=Complex(cos(rev*2*pi/i),sin(rev*2*pi/i));
for (j=0;j<ds;j++,w=w*wn)
for (k=j;k<n;k+=i) {
u=w*x[k+ds];x[k+ds]=x[k]-u;x[k]=x[k]+u;
}
}
if (rev==-1) for (i=0;i<n;i++) x[i].r/=n;
}
void solve(int a[],int &lea,int b[],int leb) {
int i,n=1;
while (n<lea+leb) n<<=1;
for (i=0;i<lea;i++) x[i]=Complex(a[i],0);
for (i=lea;i<n;i++) x[i]=Complex(0,0);
for (i=0;i<leb;i++) y[i]=Complex(b[i],0);
for (i=leb;i<n;i++) y[i]=Complex(0,0);
FFT(x,n,1);FFT(y,n,1);
for (i=0;i<n;i++) x[i]=x[i]*y[i];
FFT(x,n,-1);
for (i=0;i<n;i++)
if (x[i].r>=EPS) a[i]=1;
else a[i]=0;
lea+=leb;
}
int a[N],b[N];
int main()
{
int i,n,k,x,lea,leb;
scanf("%d%d", &n, &k);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));b[0]=1;
for (i=1;i<=n;i++) {
scanf("%d", &x);a[x]=1;
}
lea=1001;leb=1;
while (k) {
if (k&1) solve(b,leb,a,lea);
solve(a,lea,a,lea);
k>>=1;
}
for (i=1;i<=leb;i++)
if (b[i]>0) printf("%d ", i);
printf("\n");
return 0;
}
代码:(NTT,O(n*(logn+logk)))
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int mod=100000000;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
ll G=3,P=469762049,a[3*N],b[3*N],wn[25];
char s[N],t[N];
ll q_pow(ll a,ll b,ll M) {
ll ret=1;a%=M;
while (b) {
if (b&1) ret=ret*a%M;
a=a*a%M;
b>>=1;
}
return ret;
}
void getwn() {
for (int i=0;i<25;i++) wn[i]=q_pow(G,(P-1)/(1<<i),P);
}
void NTT(ll x[],int n,int rev) {
int i,j,k,t,ds,id=0;
ll w,u,v;
for (i=1,j=n>>1,k=n>>1;i<n-1;i++,k=n>>1) {//Rader
if (i<j) swap(x[i],x[j]);
while (j>=k) { j-=k;k>>=1; }
if (j<k) j+=k;
}
for (i=2,ds=1;i<=n;i<<=1,ds++)
for (j=0;j<n;j+=i) {
w=1;
for (k=j;k<j+i/2;k++) {
u=x[k]%P;v=w*x[k+i/2]%P;
x[k]=(u+v)%P;
x[k+i/2]=(u-v+P)%P;
w=w*wn[ds]%P;
}
}
if (rev==-1) {
for (i=1;i<n/2;i++) swap(x[i],x[n-i]);
w=q_pow(n,P-2,P);
for (i=0;i<n;i++) x[i]=x[i]*w%P;
}
}
void mul(ll x[],ll y[],int n) {
for (int i=0;i<n;i++) x[i]=(x[i]*y[i]%P+P)%P;
}
int main()
{
int i,n,k,x,mx=0;
scanf("%d%d", &n, &k);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (i=1;i<=n;i++) {
scanf("%d", &x);
a[x]=1;mx=max(x,mx);
}
getwn();n=1;b[0]=1;
while (n<mx*k) n<<=1;
NTT(a,n,1);NTT(b,n,1);
while (k) {
if (k&1) mul(b,a,n);
mul(a,a,n);
k>>=1;
}
NTT(b,n,-1);
for (i=1;i<=n;i++)
if (b[i]) printf("%d ", i);
printf("\n");
return 0;
}