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.

Input

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.

Output

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.

Examples

Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33

题意:给定N个数a[],让你选择K个数,可以重复选,求其组合成的和有哪些。N、K、a[]<=1000;

思路:看成1000000项的多项式,如果存在a[]=x,则x的系数为1,然后多项式自乘K次,系数不为0的部分表示可以有K个数构成,可以用FFT+快速幂,为了避免精度误差,每次快速幂后把非0的改为1,免得变得很大后产生误差,复杂度O(1000000*log1000000*logK),有点大,稍微优化下常数可以卡过。

这里尝试 用NTT,由于系数可以达到1000^1000,所以需要除Mod,但是避免除一个Mod恰好变为0,所以我们取两个Mod避免hack。

快速幂+NTT   4398ms:

#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
#define MOD Mod
#define ll long long
const int G=;
const int maxn=;
int Mod;
int qpow(int v,int p)
{
int ans=;
for(;p;p>>=,v=1ll*v*v%Mod)
if(p&)ans=1ll*ans*v%Mod;
return ans;
}
void rader(int y[], int len) {
for(int i=,j=len/;i<len-;i++) {
if(i<j) swap(y[i],y[j]);
int k=len/;
while(j>=k) j-=k,k/=;
if(j<k) j+=k;
}
}
void NTT(int y[],int len,int opt) {
rader(y,len);
for(int h=;h<=len;h<<=) {
int wn=qpow(G,(MOD-)/h);
if(opt==-) wn=qpow(wn,Mod-);
for(int j=;j<len;j+=h) {
int w=;
for(int k=j;k<j+h/;k++) {
int u=y[k];
int t=(ll)w*y[k+h/]%MOD;
y[k]=(u+t)%MOD;
y[k+h/]=(u-t+MOD)%MOD;
w=(ll)w*wn%MOD;
}
}
}
if(opt==-) {
int t=qpow(len,MOD-);
for(int i=;i<len;i++) y[i]=(ll)y[i]*t%MOD;
}
}
void powNTT(int ans[],int a[],int x)
{
ans[]=;int len=;
while(x){
len<<=;
if(x&){
NTT(ans,len,); NTT(a,len,);
rep(i,,len-) ans[i]=(ll)ans[i]*a[i]%Mod;
NTT(ans,len,-); NTT(a,len,-);
}
NTT(a,len,);
rep(i,,len-) a[i]=(ll)a[i]*a[i]%Mod;
NTT(a,len,-);
x>>=;
}
}
int A[maxn],B[maxn],ans1[maxn],ans2[maxn];
int main()
{
int N,K,x;
scanf("%d%d",&N,&K);
rep(i,,N) scanf("%d",&x),A[x]=,B[x]=;
Mod=; powNTT(ans1,A,K);
Mod=; powNTT(ans2,B,K);
rep(i,,) if(ans1[i]||ans2[i]) printf("%d ",i);
return ;
}

洛谷给出的代码,https://www.luogu.org/problemnew/solution/CF632E ,只一次NTT,在DFT后把每个数单自求pow(K),就得到了正确答案。

(暂时不理解其解法的正确性,如果是正确的,其NTT的写法里可能也有玄机(因为把这个NTT板子套其他题,样例过不了),尚待解决。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int G=;
const int maxn=;
int mod,n,k,rev[maxn],lim,ilim,s,wn[maxn+];
std::vector<int> v;
inline int pow(int x, int y) {
int ans=;
for(;y;y>>=,x=(ll)x*x%mod)
if(y&) ans=(ll)ans*x%mod;
return ans;
}
inline int& up(int& x, int y) { if ((x+=y)>=mod) x-=mod; return x; }
inline void NTT(int* A, int typ) {
rep(i,,lim-) if (i<rev[i]) swap(A[i], A[rev[i]]);
for (int i=;i<lim;i+=i) {
const int t=lim/i/;
for (int j=;j<lim;j+=i+i) {
for (int k=;k<i; k++) {
int w=typ?wn[t*k]:wn[lim-t*k];
int x=A[k+j],y=(ll)w*A[k+j+i]%mod;
up(A[k+j],y),up(A[k+j+i]=x,mod-y);
}
}
}
if (!typ) rep(i,,lim-) A[i]=(ll)ilim*A[i]%mod;
}
inline void init(int len,int tmod) {
mod=tmod; lim=; s=-;
while(lim<len) lim+=lim,s++; ilim=pow(lim,mod-);
rep(i,,lim-) rev[i]=rev[i>>]>>|(i&)<<s;
int w=pow(G,(mod-)/len);
wn[]=;
rep(i,,lim) wn[i]=(ll)(wn[i-])*w%mod;
}
int A[maxn], B[maxn];
int main() {
scanf("%d%d",&n,&k);
int x; rep(i,,n) scanf("%d",&x), A[x]=B[x]=;
init(, );
NTT(A, );
rep(i,,lim-) A[i]=pow(A[i],k);
NTT(A, );
rep(i,,) if (A[i]) v.push_back(i);
init(, );
NTT(B, );
for (int i = ; i < lim; i++) B[i] = pow(B[i], k);
NTT(B, );
rep(i,,) if (B[i]) v.push_back(i);
sort(v.begin(), v.end()); int tot=unique(v.begin(), v.end())-v.begin();
v.resize(tot);
for (int i : v) printf("%d ",i);
return ;
}

最新文章

  1. Sublime Text 3 Emmet插件安装
  2. MySQL 5.7 安装教程
  3. 如何使用eclipse打开已有工程
  4. 第8章 用户模式下的线程同步(1)_Interlocked系列函数
  5. Java中的字符串常量池
  6. laravel项目return back()-&gt;withErrors($validator)或return back()-&gt;with(&#39;errors&#39;,&#39;原密码错误!&#39;)在前台原密码错误的情况下不能正确显示错误信息,变成报错!
  7. centos6.3安装nginx
  8. iOS CoreMotion框架(传感器)
  9. 【JS】识别浏览器版本及操作平台
  10. python判断一个数字是整数还是浮点数
  11. JVM调优之jstack找出最耗cpu的线程并定位代码
  12. react-native 入门资源合集
  13. 如果用float实现居中
  14. ASP.NET 5 Web 项目
  15. 写给Android App开发人员看的Android底层知识(5)
  16. javascript今生前世
  17. php 的优化
  18. 开放数据接口 API 简介与使用场景、调用方法
  19. set nocount on/off的作用,可配合存储过程使用
  20. 【转】RTP学习笔记

热门文章

  1. HDU 6351 (Beautiful Now) 2018 Multi-University Training Contest 5
  2. iOS 多线程安全 与 可变字典
  3. svn如何给新文件加锁
  4. linux 基本命令___0003 字符串处理和yum安装软件的路径
  5. LeetCode——Construct the Rectangle
  6. @RequestMapping映射请求,@PathVariable,@RequestParam,@RequestHeader的使用
  7. springBean获取的几种方法
  8. 安装pysqlite2
  9. StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
  10. yii2:doajax(post)会报500错误