题目链接

E. Thief in a Shop
time limit per test

5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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个数, 每个数为xi, 那么a[xi]++, 然后对a做k次fft就可以了。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
struct complex
{
double r,i;
complex(double _r = 0.0,double _i = 0.0)
{
r = _r; i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
void change(complex y[],int len)
{
int i,j,k;
for(i = , j = len/;i < len-; i++)
{
if(i < j)swap(y[i],y[j]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k) j += k;
}
}
void fft(complex y[],int len,int on)
{
change(y,len);
for(int h = ; h <= len; h <<= )
{
complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j = ;j < len;j+=h)
{
complex w(,);
for(int k = j;k < j+h/;k++)
{
complex u = y[k];
complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ;i < len;i++)
y[i].r /= len;
}
const int maxn = 2e6+;
complex x1[maxn], x2[maxn];
int a[maxn], b[maxn];
void cal(int *a, int *b, int &lena, int &lenb) {
int len = ;
while(len<lena+lenb)
len<<=;
for(int i = ; i<=lenb; i++) {
x1[i] = complex(b[i], );
}
for(int i = lenb+; i<len; i++)
x1[i] = complex(, );
for(int i = ; i<=lena; i++) {
x2[i] = complex(a[i], );
}
for(int i = lena+; i<len; i++)
x2[i] = complex(, );
fft(x1, len, );
fft(x2, len, );
for(int i = ; i<len; i++)
x1[i] = x1[i]*x2[i];
fft(x1, len, -);
for(int i = ; i<=lena+lenb; i++)
b[i] = (int)(x1[i].r+0.5);
for(int i = ; i<=lena+lenb; i++)
if(b[i]>)
b[i] = ;
lenb += lena;
}
int main()
{
int n, k, x;
cin>>n>>k;
for(int i = ; i<n; i++) {
scanf("%d", &x);
a[x]++;
}
b[] = ;
int lena = , lenb = ;
while(k) {
if(k&) {
cal(a, b, lena, lenb);
}
if(k>) {
cal(a, a, lena, lena);
}
k>>=;
}
for(int i = ; i<=lena+lenb; i++) {
if(b[i]) {
printf("%d ", i);
}
}
cout<<endl;
return ;
}

codeforces 632E. Thief in a Shop fft的更多相关文章

  1. CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)

    题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少. 分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做 ...

  2. CodeForces - 632E Thief in a Shop 完全背包

    632E:http://codeforces.com/problemset/problem/632/E 参考:https://blog.csdn.net/qq_21057881/article/det ...

  3. 2019.01.26 codeforces 632E. Thief in a Shop(生成函数)

    传送门 题意简述:给nnn个物件,物件iii有一个权值aia_iai​,可以选任意多个.现在要求选出kkk个物件出来(允许重复)问最后得到的权值和的种类数. n,k,ai≤1000n,k,a_i\le ...

  4. CodeForces 632E Thief in a Shop

    题意:给你n种物品,每种无限个,问恰好取k个物品能组成哪些重量.n<=1000,k<=1000,每种物品的重量<=1000. 我们搞出选取一种物品时的生成函数,那么只要对这个生成函数 ...

  5. 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 ...

  6. C - Thief in a Shop - dp完全背包-FFT生成函数

    C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...

  7. Educational Codeforces Round 9 E. Thief in a Shop NTT

    E. Thief in a Shop   A thief made his way to a shop. As usual he has his lucky knapsack with him. Th ...

  8. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

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

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

随机推荐

  1. JS 事件绑定的几种方式 小笔记

    第一种 var test=document.getElementById('add'); add.onclick=function(){ alert('1'); } 直接在对象上注册事件 缺点:如果我 ...

  2. html系列教程--input label

    <input> 标签:用于提交用户输入数据的文本框. input属性: 1.checked:用于checkbox,radio等元素,确定是否选中,true/false 2.disabled ...

  3. jquery.validate校验文件使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库<script src="../js/ ...

  4. spring 源码之 ioc 容器的初始化和注入简图

    IoC最核心就是两个过程:IoC容器初始化和IoC依赖注入,下面通过简单的图示来表述其中的关键过程:

  5. jquery写的tab切换效果 非常简单

    自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...

  6. 比callback更简洁的链式执行promise

    promise自己理解的也不够深刻,具体知识点不在这里细说了 直接上个例子,清晰明了,自己去悟吧 <script type="text/javascript"> //模 ...

  7. llinuxs介绍与常用命令

    一.Linux系统概述1.计算机资源软件资源硬件资源操作系统2.操作系统WindowsMacOSLinuxUnix3.Linux含义狭义Linux:由Linus一段内核代码广义Linux:Linux厂 ...

  8. struts2笔记01-环境搭建

    1.官网下载struts2 struts-2.3.28-all.zip,这个包可谓应有尽有,以后全靠它了! 2.jar包怎么选?       (1)struts-2.3.28-all\struts-2 ...

  9. 介绍几种IC卡

    介绍几种IC卡 Mifare卡:工作频率:13.56MHZ数据保存期:10年 操作时间:96ms读写距离:2.5-10cm   存储容量:8Kbit尺寸:85.6x54x0.76mm 存储器类型:EE ...

  10. SendMessage的返回值,就是由相应的响应消息函数的返回值(解释的简洁明了)

    SendMessage Return Values The return value specifies the result of the message processing and depend ...