Thief in a Shop
题意:
问n个物品选出K个可以拼成的体积有哪些。
解法:
多项式裸题,注意到本题中 $A(x)^K$ 的系数会非常大,采用NTT优于FFT。
NTT 采用两个 $2^t+1$ 质数,求原根 $g_n$ 后用 $g_n^1 $~$ g_n^{P-1}$ 的循环代替复数向量的旋转。
注意逆的 $w_n$ 是 $g_n ^ { - \frac{P-1}{len} }$,并且要用两个质数保证正确即可,$O(nlogn)$。
#include <bits/stdc++.h> #define PI acos(-1)
#define P1 998244353LL
#define P2 469762049LL
#define LL long long
#define gn 3 const int N = ; using namespace std; int R[N<<]; LL qpow(LL x,int n,LL P)
{
LL ans = ;
for(;n;n>>=,x = x*x % P) if(n&) ans = ans*x % P;
return ans;
} void DFT(LL a[],int n,int tp_k,LL P)
{
for(int i=;i<n;i++) if(i<R[i]) swap(a[i],a[R[i]]);
for(int d=;d<n;d<<=)
{
LL wn = qpow(gn, (P-)/(d<<),P);
if(tp_k == -) wn = qpow(wn, P-,P);
for(int i=;i<n;i += (d<<))
{
LL wt = ;
for(int k=;k<d;k++, wt = wt*wn % P)
{
LL A0 = a[i+k], A1 = wt * a[i+k+d] % P;
a[i+k] = A0+A1;
a[i+k+d] = A0+P-A1;
if(a[i+k] >= P) a[i+k] -= P;
if(a[i+k+d] >= P) a[i+k+d] -= P;
}
}
}
LL inv = qpow(n, P-,P);
if(tp_k==-)
for(int i=;i<n;i++) a[i] = a[i] * inv % P;
} LL A[N<<],B[N<<]; int main()
{
//freopen("test.txt","w",stdout);
int n,K;
cin>>n>>K;
int L = ,tot;
while((<<L)<*K) L++;
tot = (<<L);
for(int i=;i<tot;i++) R[i]=(R[i>>]>>)|((i&)<<(L-));
for(int i=,x;i<=n;i++) scanf("%d",&x), A[x] = , B[x] = ;
DFT(A,tot,,P1);
for(int i=;i<tot;i++) A[i] = qpow(A[i], K, P1);
DFT(A,tot,-,P1);
DFT(B,tot,,P2);
for(int i=;i<tot;i++) B[i] = qpow(B[i], K, P2);
DFT(B,tot,-,P2);
for(int i=;i<tot;i++) if(A[i] || B[i]) printf("%d ",i);
printf("\n");
return ;
}
Thief in a Shop的更多相关文章
- 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 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 ...
- C - Thief in a Shop - dp完全背包-FFT生成函数
C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...
- 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 dp fft
E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...
- 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 ...
- CF632E Thief in a Shop 和 CF958F3 Lightsabers (hard)
Thief in a Shop n个物品每个价值ai,要求选k个,可以重复.问能取到哪几个价值? 1 ≤ n, k ≤ 1000,1 ≤ ai ≤ 1000 题解 将选一个物品能取到的价值的01生成函 ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
- CF632E: Thief in a Shop(快速幂+NTT)(存疑)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- codeforces632E. Thief in a Shop (dp)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
随机推荐
- python(23)- 面向对象简单介绍
面向概述 面向过程:根据业务逻辑从上到下写垒代码 面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点:极大降低了程序的复杂 ...
- idea刷新项目、清除项目缓存
点击File -> Invalidate caches ,点击之后在弹出框中点击确认,之后软件就自动重启了
- 运维基础-IO 管道
什么是文件描述符FD或者文件句柄? 通过构建一个带有编号标记的通道(文件描述符)的进程结构来管理打开的文件.今晨连接到文件,从而达到这些文件所代表的的数据内容或者设备.通过使用通道0.1.2(称为标准 ...
- shell-判断循环
shell条件测试 test 每个完整的合理的编程语言都具有条件判断的功能. bash可以使用test命令,[]和()操作,还有if/then结构 字符串判断 -n string 判断字符串长度非零 ...
- Machine Learning:Neural Network---Representation
Machine Learning:Neural Network---Representation 1.Non-Linear Classification 假设还採取简单的线性分类手段.那么会面临着过拟 ...
- 几篇QEMU/KVM代码分析文章
QEMU/KVM结合起来分析的几篇文章,代码跟最新的版本有些差异,但大体逻辑一样,写得通俗易懂.我把链接放这里主要是为自己需要查看时调转过去方便,感谢作者的付出! QEMU Source Code S ...
- IP和归属地
ip: http://www.ip.cn/index.php?ip=10.132.98.143 归属地: http://www.ip138.com:8080/search.asp?action=mob ...
- python中if __name__ == '__main__': 的解析(转载)
当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...
- Navicat Premium创建MySQL存储过程
1.使用Navicat Premium打开创建函数向导,操作:连接名——数据库——函数——新建函数 2.选择过程——输入存储过程参数——完成(这一步可以不填写参数,编写存储过程代码的时候设置参数) 3 ...
- spring 集成 mybatis 后数据源初始化失败问题分析
问题背景: 项目使用spring,springmvc框架,后边需操作关系数据库,选择了mybatis + durid,集成mybatis后,项目一直启动失败.错误的原因是dataSource初始化的时 ...