题目链接

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. javascript 全选与反选

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  2. CentOS添加中科大、163 yum源

    首先备份CentOS-Base.repo [root@richard yum.repos.d]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos ...

  3. WPF中的布局控件(转)

    WPF中使用Panel进行页面布局,Panel是一个抽象类,它作为所有Panel面板控件的基类.Panel并不是继承自Control类,而是直接从FrameworkElement继承.看Panel的继 ...

  4. json(转)

    转自:http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如何使用J ...

  5. Spring-----多环境中加载资源配置文件

    转载自:http://blog.csdn.net/hekewangzi/article/details/51942128

  6. 易宝网上支付平台的PHP接口代码

    本代码参照自韩顺平149讲视频后5讲,需要学习的朋友可以参考本代码 这是测试图片: 以下是代码部分: <?php function HmacMd5($data, $key) { //需要配置环境 ...

  7. Centos6.5源码编译安装nginx

    1.安装pcre下载地址:http://jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gz #tar -axvf pcre ...

  8. PHP比较全的友好的时间显示,比如‘刚刚’,'几秒前'等

    分享一个php友好的比较完成的时间格式化函数,包括‘刚刚’,'几秒之前',‘几分钟前’,'几小时前',几天前,几周前,几个月前等.调用方式很简单,是从ThinkSNS 里面拿出来的. /** * 友好 ...

  9. JS的匿名函数和递归应用

    今天有项目需要用到JS的递归,开始按照php的实现方法,发现不行,后来只能使用arguments.callee. 简单的例子,如下 var act = 'if(num <= 1){return ...

  10. 标准C编程-笔记全集

    C语言的基本概念 编写一个简单的C程序,后缀名保存为c(本次文件名为a.c) gcc:对c程序进行编译和连接:gcc a.c ./a.out:运行程序,输出程序的结果:其中a是c程序的文件名 说明:其 ...