E. Thief in a Shop
 

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
 
 
题意:
  给你n个数,你可以选数选k次,每次可以选相同的数,问你有多少种和
题解:
  将k分解
  进行log(k)次FFT
  FFT精度可能不够,bool好像能过
  NTT,换一个合适 的 费马素数和原格就能过
   复杂度:O(n*log(n)*log(k))
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, M = 1e3+,inf = 2e9,mod = 1e9+;
const long long P=23068673LL;
const int G=; LL mul(LL x,LL y){
return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
}
LL qpow(LL x,LL k,LL p){
LL ret=;
while(k){
if(k&) ret=mul(ret,x);
k>>=;
x=mul(x,x);
}
return ret;
} LL wn[];
void getwn(){
for(int i=; i<=; ++i){
int t=<<i;
wn[i]=qpow(G,(P-)/t,P);
}
} int len;
void NTT(LL y[],int op){
for(int i=,j=len>>,k; i<len-; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>;
while(j>=k){
j-=k;
k>>=;
}
if(j<k) j+=k;
}
int id=;
for(int h=; h<=len; h<<=) {
++id;
for(int i=; i<len; i+=h){
LL w=;
for(int j=i; j<i+(h>>); ++j){
LL u=y[j],t=mul(y[j+h/],w);
y[j]=u+t;
if(y[j]>=P) y[j]-=P;
y[j+h/]=u-t+P;
if(y[j+h/]>=P) y[j+h/]-=P;
w=mul(w,wn[id]);
}
}
}
if(op==-){
for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
LL inv=qpow(len,P-,P);
for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
}
}
void Convolution(LL A[],LL B[]){
for (int i=;i<len;i++) A[i]=mul(A[i],B[i]);
}
int x,n,m;
LL num[<<],now[<<];
int main() {
scanf("%d%d",&n,&m);
int mx = ;
for(int i = ; i <= n; ++i) scanf("%d",&x),num[x]=,mx = max(x,mx);
mx*=m;
for(len=; len<=mx; len<<=);
now[] = ;getwn();
NTT(now,); NTT(num,);
while(m > ) {
if(m%) {
Convolution(now,num);
}
Convolution(num,num);
m/=;
}
NTT(now,-);
for(int i = ; i <= mx; ++i) {
if(now[i]) printf("%d ",i);
}
return ;
} /*
3 3
3 5 11
*/

Educational Codeforces Round 9 E. Thief in a Shop NTT的更多相关文章

  1. Educational Codeforces Round 9 E. Thief in a Shop dp fft

    E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...

  2. codeforces Educational Codeforces Round 9 E - Thief in a Shop

    E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...

  3. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  4. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  5. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  6. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  7. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  8. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  9. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

随机推荐

  1. 九度oj 题目1369:字符串的排列

    题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...

  2. BZOJ 2463: [中山市选2009]谁能赢呢?【博弈】

    这题不科学~~本以为鬼谷子的钱袋是能在BZOJ写的最短的程序了,这题还要短…..好吧,思考难度神马的还是有点的(至少对我这种蒟蒻来说).很明显这是道博弈论的题目,在纸上画出了n=1~4的博弈树,发现b ...

  3. BZOJ3130 [Sdoi2013]费用流 【网络流 + 二分】

    题目 Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识. 最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量.一个合法的网络流方案必须满足:(1)每 ...

  4. 常州模拟赛d5t1 journalist

    分析:出题人丧心病狂卡spfa......只能用dijkstar+堆优化. 主要的难点是字典序的处理上,一个想法是在做最短路的时候处理,边松弛边记录,比个大小记录最佳答案.具体的思路大概和最短路计数差 ...

  5. noip普及组考纲+样题合集——初级篇(OIer必看)

    很明显我是想发提高组合集的.普及组考纲……用发么. 当然如果你想看的话也可以,就一点点: 递归.排序…… 很明显上面那都不是重点.普及组只要掌握搜索.二分.单调队列.数学.随机化等等,一等奖没问题的, ...

  6. 济南学习 Day5 T3 晚

    回文串(palindromes) [题目描述] 判断是否能将字符串S分成三段非空回文串. [输入说明] 第一行一个整数T,表示数据组数. 对于每一个组,仅包含一个由小写字母组成的串. [输出说明] 对 ...

  7. Day 9 Linux samba & ngnix

    (摘) Samba服务 一.Samba简介  Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服 ...

  8. 生成PDF文档

    byte[] buffer = context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearCon ...

  9. linux fork()

    一. linux下C语言可以用fork()建立子进程.fork函数返回两个值,对于子进程,返回0; 父进程,返回子进程ID. 所以用if(fork()==0)      {子进程执行的代码段:}els ...

  10. Spring的IoC容器-Spring ApplicationContext容器

    Application Context是spring中较高级的容器.和BeanFactory类似,它可以加载配置文件中定义的bean,将所有的bean集中在一起,当有请求的时候分配bean. 另外,它 ...