CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)
题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少。
分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做k次FFT求卷积求卷积肯定爆炸,考虑用分治的形式计算,因为中间计算的时候会重复计算一些幂次,所以用记忆化搜索的形式,保留计算结果。
因为只要计算出哪些项不为0,所以卷积之后求结果时,系数非0项用1作系数即可,否则分分钟炸精度。
当然也可以用快速幂求解#.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e6 + 10;
const double PI = acos(-1.0);
struct Complex{
double x, y;
inline Complex operator+(const Complex b) const {
return (Complex){x +b.x,y + b.y};
}
inline Complex operator-(const Complex b) const {
return (Complex){x -b.x,y - b.y};
}
inline Complex operator*(const Complex b) const {
return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
}
} va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
int lenth = 1, rev[MAXN * 2 + MAXN / 2];
int N, M; // f 和 g 的数量
// f g和 的系数
// 卷积结果
// 大数乘积
int f[MAXN],g[MAXN];
vector<LL> conv;
vector<LL> multi;
//f g
void init()
{
int tim = 0;
lenth = 1;
conv.clear(), multi.clear();
memset(va, 0, sizeof va);
memset(vb, 0, sizeof vb);
while (lenth <= N + M - 2)
lenth <<= 1, tim++;
for (int i = 0; i < lenth; i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
}
void FFT(Complex *A, const int fla)
{
for (int i = 0; i < lenth; i++){
if (i < rev[i]){
swap(A[i], A[rev[i]]);
}
}
for (int i = 1; i < lenth; i <<= 1){
const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
for (int j = 0; j < lenth; j += (i << 1)){
Complex K = (Complex){1, 0};
for (int k = 0; k < i; k++, K = K * w){
const Complex x = A[j + k], y = K * A[j + k + i];
A[j + k] = x + y;
A[j + k + i] = x - y;
}
}
}
}
void getConv(){ //求多项式
init();
for (int i = 0; i < N; i++)
va[i].x = f[i];
for (int i = 0; i < M; i++)
vb[i].x = g[i];
FFT(va, 1), FFT(vb, 1);
for (int i = 0; i < lenth; i++)
va[i] = va[i] * vb[i];
FFT(va, -1);
for (int i = 0; i <= N + M - 2; i++)
conv.push_back((LL)(va[i].x / lenth + 0.5)>0?1:0);
}
int base[MAXN];
vector<LL> mi[1005];
int num;
bool make[1005];
vector<LL> dfs(int i,int j,int cc){
if(i==j){
vector<LL> res;
for(int i=0;i<num;++i){
res.push_back(base[i]);
}
make[cc] = true;
mi[cc] = res;
return res;
}
vector<LL> L,R;
int m = (i+j)>>1;
if(make[m-i+1]) L = mi[m-i+1];
else L = dfs(i,m,m-i+1);
if(make[j-m]) R = mi[j-m];
else R = dfs(m+1,j,j-m);
N = L.size();
M = R.size();
for(int i=0;i<N;++i) f[i] = L[i];
for(int i=0;i<M;++i) g[i] = R[i];
getConv();
make[cc] = true;
mi[cc] = conv;
return conv;
}
int cnt[MAXN];
int main()
{
int n,k;
scanf("%d %d",&n, &k);
int mx = -1;
int mn = 100005;
for(int i=1,tmp;i<=n;++i){
scanf("%d",&tmp);
cnt[tmp]++;
mn = min(mn,tmp);
mx = max(mx,tmp);
}
num = mx+1;
for(int i=0;i<num;++i){
base[i] = cnt[i];
}
conv = dfs(1,k,k);
int sz = conv.size();
for(int i=k*mn;i<sz;++i){
if(!conv[i]) continue;
printf("%d%c",i,i==sz-1?'\n':' ');
}
return 0;
}
CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)的更多相关文章
- codeforces 632E. Thief in a Shop fft
题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- codeforces 793 D. Presents in Bankopolis(记忆化搜索)
题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...
- 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 ...
随机推荐
- VC++ GetSafeHwnd用法
GetSafeHwnd HWND GetSafeHwnd() const; 当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. ...
- hdu 2821(dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时 ...
- 获取软件的apk包名、查看手机设备名称等
获取软件的apk包名 1.下载aapt,这里使用的是SDK自带的aapt,找到SDK路径下面的aapt.exe,本机路径:E:\downloads\android-sdk_r23.0.2-window ...
- android开发环境之ADT安装,卸载,更新
http://hj198703.iteye.com/blog/1316991 1.官网:http://developer.android.com/sdk/index.html2.ADT组件在线安装 ...
- JZOJ.5326【NOIP2017模拟8.21】LCA 的统计
Description
- vue里监听安卓的物理返回键
Hybrid App中,原生内嵌H5单页,由于安卓是有物理返回键的,按安卓物理返回键的时候会返回到上一个路由. 实际中需求是:当有弹层的时候,按物理返回键是关闭弹层,是页面的时候才执行返回上一个路由, ...
- Oracle的启动与关闭
启动数据库的前提条件: 环境变量定义好($ORACLE_HOME,$ORACLE_SID,$PATH) 能密码文件认证或OS认证(确保能登入sys) 有正确的参数文件(启动数据库需要查找参数文件,默认 ...
- JavaScript学习(5)-Image对象和动态HTML
JavaScript学习5 1.image 对象 对象引用 document.images[n] document.images["imageName"] document.ima ...
- Solr 整合
1. Solr 与 Lucene 的区别 Lucene是一个开发源代码的全文检索引擎工具包,它不是一个完整的全文检索引擎,Lucene 提供了完整的查询引擎 和索引引擎,目的是为软件开发人员提供一个简 ...
- ubuntu14下创建软件的快捷启动方式
下载软件,使用softname/bin/softname.sh即可启动,但是很麻烦,每次都要打开terminal 为了方便,我们需要创建desktop文件指向这个启动软件的shell文件(以创建Pyc ...