codeforces 632E. Thief in a Shop fft
5 seconds
512 megabytes
standard input
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.
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.
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.
3 2
1 2 3
2 3 4 5 6
5 5
1 1 1 1 1
5
3 3
3 5 11
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的更多相关文章
- CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)
题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少. 分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做 ...
- CodeForces - 632E Thief in a Shop 完全背包
632E:http://codeforces.com/problemset/problem/632/E 参考:https://blog.csdn.net/qq_21057881/article/det ...
- 2019.01.26 codeforces 632E. Thief in a Shop(生成函数)
传送门 题意简述:给nnn个物件,物件iii有一个权值aia_iai,可以选任意多个.现在要求选出kkk个物件出来(允许重复)问最后得到的权值和的种类数. n,k,ai≤1000n,k,a_i\le ...
- CodeForces 632E Thief in a Shop
题意:给你n种物品,每种无限个,问恰好取k个物品能组成哪些重量.n<=1000,k<=1000,每种物品的重量<=1000. 我们搞出选取一种物品时的生成函数,那么只要对这个生成函数 ...
- 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 ...
- C - Thief in a Shop - dp完全背包-FFT生成函数
C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...
- 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 ...
- 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 ...
- codeforces Educational Codeforces Round 9 E - Thief in a Shop
E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...
随机推荐
- box-shadow属性
一.定义和用法 box-shadow属性 向框添加一个或多个阴影. 二.语法 box-shadow: h-shadow v-shadow blur spread color inset; h-shad ...
- oracle ORA-00913: 值过多
--oracle中查看表是否被锁 查看表是否被锁 SELECT /*+ rule*/ a.sid, b.owner, object_name, object_type FROM v$loc ...
- Algorithms 4th - 1.1 Basic Programming Model - EXERCISES
欢迎交流 1.1.1 a. 7 b. 200.0000002 c. true 1.1.2 a. 1.618 b. 10.0 c. true d. 33 1.1.3 public class MainA ...
- [置顶] woff格式字体怎么打开和编辑?
如题! woff百度百科:http://baike.baidu.com/link?url=toS7yqpN9VlEcO2GOEp5JEA9-TeaZgIdVqTOv7iHshsNvk-V8HtxEY0 ...
- chrome误删书签恢复。
由于手残本来想添加网页到书签文件夹的,结果点了删除. 但是整个人就炸了,里面有我好多链接. 于是立马Google了一下,发现不少朋友和我一样,都是误删了书签或者书签文件夹. 但是chrome并没有书签 ...
- Java生成缩略图之Thumbnailator
Thumbnailator 是一个为Java界面更流畅的缩略图生成库.从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持 ...
- Windows2008RT搭建VPN服务器
总结一下2008系统搭建VPN的步骤和过程,自己有个人网站和服务要通过互联网发布出来.服务器放在自己家里,宽带是民用的.也就产生了服务发布的一些问题.用无法映射出真实的公网IP,或是一些其他内部的问题 ...
- 黑马程序员-- C语言交换两个整数变量值几种函数比较
总结了C语言中几种交换两个整数数值的函数,欢迎交流 #include <stdio.h> 使用多种交换变量值的函数比较 方法一:使用第三方临时变量 这种函数a,b只是值传递,实质上不能修交 ...
- Netfilter-packet-flow.svg
调试网络的方法:(Debugging the kernel using Ftrace) $ watch -n1 -d sudo cat /proc/net/snmp$ watch -n1 -d su ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(一)
目的: 结合现在比较流行的技术,通过一个demo 展示一个全栈式设计的各种技能. 一个全栈式的工程师,应该能设计通过verilog/VHDL做logical设计.能写内核驱动,能架站. 要熟悉veri ...