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 ...
随机推荐
- v8随心记
因为node原因,研究v8已经有段时间了,但是一直也没有抽空写点什么,现在想想有好多当时清晰的东西又模糊了.哎,伤心的很啊.但是一时又想不起什么章法,所以只能随手胡乱写了. 下载.编译: https: ...
- hdu 1560(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...
- Target frameworks
https://docs.microsoft.com/en-us/dotnet/standard/frameworks When you target a framework in an app or ...
- 状态栏,ActionBar,工具栏高度调整
1.在属性中可以这样设置更改ActionBar的高度android:layout_marginTop="?android:attr/actionBarSize" Rect fram ...
- sql中IN的含义
in后面可以是查询集合,也可以是诸如('1','2','3','4') 例: UPDATE CC_PROBLEM_INFO SET Z_STATUS = 1 WHERE CONTENT_ID IN ( ...
- 二 Android Studio 打包EgretApp (开机画面、横竖屏、调试、和原生交互)
测试环境: Windows7 Egret Engine 5.0.14 Egret support 5.0.12 Android Studio 2.3 目录: 一 修改开机画面 二 横竖屏设置 三 修改 ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- angular 封装公共方法
angular封装公共方法到service中间件,节省开发时间 layer.service.ts openAlert(callback) {// 传递回调函数 const dialogRef = th ...
- Hibernate的调用数据库的存储过程
Hibernate并没有给出直接调用数据库的存储过程的API,所以咋们就要通过调用原生的的connection对象来实现对存储过程的条用 Hibernate调用存储过程的步骤: 1:获得原生conne ...
- Python: 使用pip升级所有包
pip 当前内建命令并不支持升级所有已安装的Python模块. 列出当前安装的包: pip list 列出可升级的包: pip list --outdate 升级一个包: pip install ...