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 ...
随机推荐
- VueJS样式绑定:v-bind
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- pjblog支持QQ、新浪微博一键登录
转载地址: http://www.ruisoftcn.com/blog/article.asp?id=955
- CAM350 10.5移动和叠层的用法
1.快捷键 EC复制 IMO测量最近距离 UZ放大,缩小尺寸 UC拷 ...
- 笔记03 MVVM 开发的几种模式(WPF)
转自http://www.cnblogs.com/buptzym/p/3220910.html 在WPF系(包括SL,WP或者Win8)应用开发中,MVVM是个老生常谈的问题.初学者可能不会有感觉,但 ...
- vim 自动补全
1. vim编辑器自带关键字补全 触发: ctrl + n or ctrl + p 补全命令: <C-n> 普通关键字 [能够根据buffer以及标签文件列表等 ...
- CentOS7配置opencv for python && eclipse c/c++[更新]
更改前的安装过程有些问题,主要是ffmpeg-devel的安装部分,这里重新说一下 两种安装方法: 第一种,直接: # yum install numpy opencv* 这种方法安装了之后,能够在p ...
- Sublime Text 3相关配置和设置
Sublime Text 3打开txt中文乱码的解决方法 Sublime Text是一个很强大的编辑器,但是对中文的支持并不好,在Sublime Text 2 时,能够通过命令行的方式安装编码包来解决 ...
- Django-extra的用法
## select提供简单数据 # SELECT age, (age > 18) as is_adult FROM myapp_person; Person.objects.all().extr ...
- linux下启动和关闭weblogic(转载)
在weblogic定义的域中可以找到如下命令: /[youHome]/domains/[yourDomain]/startWebLogic.sh /[youHome]/domains/[yourDom ...
- from memory cache
from memory cache