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 ...
随机推荐
- Tachyon源代码结构分析(二)
公布人:南京大学PASA大数据实验室顾荣 前言 在上一篇<Tachyon源代码结构分析(一)>中,我们介绍了Tachyon的四大模块(Client模块.Master模块.Worker模块以 ...
- python(9)- python基础知识刷题
1. 执行 Python 脚本的两种方式 交互方式:命令行 Windows操作系统下,快捷键cmd,输入“python”启动交互式python解释器. 文件方式:python文件 2. 简述位.字 ...
- 1.excel如何让一列的数都乘以固定值
让B列等于A列乘以39.37 1.我们先选中B列中要编辑的单元: 2.再在编辑栏中输入公式:=A2*39.37 (PS:*号即表示是×号) 3.公式输入后,按下快捷键:CTRL+回车:记住一定要 ...
- nginx源代码分析--配置信息的继承&合并
这里仅仅讲述http{}模块下的配置: 在ngx_http_block()函数内(这个函数别调用时在ngx_inti_cycle内的ngx_conf_parse函数,这个函数遇到http命令时 回调n ...
- java中使用opencv
Java + opencv学习:在Eclipse下配置基于Java的OpenCV开发环境 2016-04-08 17:43 6491人阅读 评论(0) 收藏 举报 分类: OpenCV学习(10) ...
- CSS入门学习
一.What? CSS的全称是CascadingStyle Sheet,汉语意思是"级联样式表".通常又称为"风格样式表(StyleSheet)".它是用来进行 ...
- C++中字符数组和字符串string
字符数组 C++中字符数组用char str[]能够用来表示一个字符串. (1) 数组的大小和字符串的长度. 数组的大小一定要大于字符串的长度,由于系统会自己主动补上一个'\0'作为字符串的结束标 ...
- 求两个有序数组的中位数(4. Median of Two Sorted Arrays)
先吐槽一下,我好气啊,想了很久硬是没有做出来,题目要求的时间复杂度为O(log(m+n)),我猜到了要用二分法,但是没有想到点子上去.然后上网搜了一下答案,感觉好有罪恶感. 题目原型 正确的思路是:把 ...
- android studio 程序真机执行中文显示乱码
代码里中文显示正常,真机执行后中文显示乱码,解决的方法: build.gradle中加入一句 android { compileOptions.encoding = "GBK" }
- EasyDarwin开源流媒体服务器提供的RTMP直播推送库
EasyRTMP EasyRTMP是什么? EasyRTMP是一个EasyDarwin配套使用,也可以单独使用的RTMP推送库,通过EasyRTMP我们就可以避免接触到稍显复杂的RTMP推送流程,只需 ...