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 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.
Sample Input
3 2
1 2 3
Sample Output
2 3 4 5 6
Hint
题意
有n个数,然后这n个数里面选k个加起来
问你一共能加出来多少种
题解:
多项式加法,加k次,问你最后的数是哪些,显然FFT模板,然后怼一波
其实DP也是可以兹瓷的。
dp[i]表示最少用多少个非a[1]能够构成a[1]*k+i的。
DP代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
int n,k,a[maxn],dp[maxn*maxn];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
n=unique(a+1,a+1+n)-(a+1);
for(int i=2;i<=n;i++)
a[i]=a[i]-a[1];
for(int i=1;i<=k*a[n];i++)
dp[i]=k+1;
for(int i=2;i<=n;i++)
for(int j=a[i];j<=k*a[i];j++)
dp[j]=min(dp[j],dp[j-a[i]]+1);
for(int i=0;i<=k*a[n];i++)
if(dp[i]<=k)
printf("%d ",a[1]*k+i);
return 0;
}
FFT代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1<<21;
const double PI = acos(-1.0);
struct Virt
{
double r,i;
Virt(double r = 0.0,double i = 0.0)
{
this->r = r;
this->i = i;
}
Virt operator + (const Virt &x)
{
return Virt(r+x.r,i+x.i);
}
Virt operator - (const Virt &x)
{
return Virt(r-x.r,i-x.i);
}
Virt operator * (const Virt &x)
{
return Virt(r*x.r-i*x.i,i*x.r+r*x.i);
}
};
//雷德算法--倒位序
void Rader(Virt F[],int len)
{
int j = len >> 1;
for(int i=1; i<len-1; i++)
{
if(i < j) swap(F[i], F[j]);
int k = len >> 1;
while(j >= k)
{
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
}
//FFT实现
void FFT(Virt F[],int len,int on)
{
Rader(F,len);
for(int h=2; h<=len; h<<=1) //分治后计算长度为h的DFT
{
Virt wn(cos(-on*2*PI/h),sin(-on*2*PI/h)); //单位复根e^(2*PI/m)用欧拉公式展开
for(int j=0; j<len; j+=h)
{
Virt w(1,0); //旋转因子
for(int k=j; k<j+h/2; k++)
{
Virt u = F[k];
Virt t = w*F[k+h/2];
F[k] = u+t; //蝴蝶合并操作
F[k+h/2] = u-t;
w = w*wn; //更新旋转因子
}
}
}
if(on == -1)
for(int i=0; i<len; i++)
F[i].r /= len;
}
//求卷积
void Conv(Virt F[],Virt G[],int len)
{
FFT(F,len,1);
FFT(G,len,1);
for(int i=0; i<len; i++)
F[i] = F[i]*G[i];
FFT(F,len,-1);
}
int mx = 0;
bool dp[maxn];
bool a[maxn];
Virt K1[maxn],K2[maxn];
void multiply(bool *A,bool *B,int l)
{
int len = 1;
while(len<=l+1)len*=2;
for(int i=0;i<len;i++)
{
K1[i].r=A[i];
K1[i].i=0;
K2[i].r=B[i];
K2[i].i=0;
}
Conv(K1,K2,len);
for(int i=0;i<=len;i++)
A[i]=K1[i].r>0.5;
}
void solve(int k)
{
if(k==0)
{
dp[0]=true;
}
else if(k%2==1)
{
solve(k-1);
multiply(dp,a,mx);
}
else
{
solve(k/2);
multiply(dp,dp,mx);
}
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
int x;scanf("%d",&x);
a[x]=true;mx=max(mx,x);
}
mx*=k;
solve(k);
for(int i=1;i<=mx;i++)
if(dp[i])printf("%d ",i);
cout<<endl;
}
Educational Codeforces Round 9 E. Thief in a Shop dp fft的更多相关文章
- codeforces Educational Codeforces Round 9 E - Thief in a Shop
E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...
- 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 ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D dp(最大连续子序列)
https://codeforces.com/contest/1155/problem/D 题意 一个n个数的数组\(a[i]\),可以选择连续的一段乘x,求最大连续子序列的值 题解 错误思路:贪心, ...
- Educational Codeforces Round 57 (Rated for Div. 2) D dp
https://codeforces.com/contest/1096/problem/D 题意 给一个串s,删掉一个字符的代价为a[i],问使得s的子串不含"hard"的最小代价 ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 概率dp+状压
题目链接: 题目 E. Another Sith Tournament time limit per test2.5 seconds memory limit per test256 megabyte ...
随机推荐
- mysql增删
create table msg (id int, name varchar(10)); 插入语句 insert into msg values(1,'root'); insert into msg( ...
- 在linux程序里面,知道一个函数地址,改函数是属于某个动态库的,怎么样得到这个动态库的全【转】
转自:http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml 另外dl_iterate_phdr可以查到当前进程所装在的所有 ...
- sicily 1153. 马的周游问题
一.题目描述 在一个8 * 8的棋盘中的某个位置有一只马,如果它走29步正好经过除起点外的其他位置各一次,这样一种走法则称马的周游路线,试设计一个算法,从给定的起点出发,找出它的一条周游路线. 为了便 ...
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- hihocoder 1135 : Magic Box
#1135 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. Whe ...
- 自己编译生成k8s的rpm包
我指的是以下几个安装包: -rw-r--r--. 1 root root 8976134 Jul 13 10:19 kubeadm-1.7.0-0.x86_64.rpm-rw-r--r--. 1 ro ...
- CF 某套题 O :Grid (简单BFS)
题意: 从左上角跳到右下角最少需要多少步,跳的规则为:可以向四个方向的任意一个方向跳当前格子中的步数,若跳不到右下角输出IMPOSSIBLE. 题解: BFS搜索,注意判断边界,标记. 代码: #in ...
- Project interpreter not specified(eclipse+pydev) (转)
[小记] 最近因为想配置Android的开发环境,把原来的MyEclipse5.5删了,下载了最新的Eclipse3.7版本,因为之前在进行Python开 发,就下载了最新的Pydev2.4版本,安装 ...
- shell脚本学习(五)
流程控制 先说几个注意的地方 1)注意你是在unix下编程,注意文件的编码如果你发现报错请用notepad++打开,编辑->文档格式转换->点unix,然后再上传运行即可 2)sh的流程控 ...
- SVN版本控制软件
一.版本控制软件 1.为什么需要版本控制软件 问题:① 团队开发 ② 异地协作 ③ 版本回退 2.解决之道 SCM(Software Configuration Management):软件配置管理 ...