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 ...
随机推荐
- Redux作用
作用:Redux是为了解决React中组件与组件之间数据传递的问题. React组件之间的传递有三种情况:1.父组件传递数据给子组件:由于redux是一个单向数据流的框架,所以它的数据就只能由父组件传 ...
- pycharm5.0 激活方式
Pycharm5注册方式 0x1 ,安装 0x2 , 调整时间到2038年. 0x3 ,申请30天试用 0x4, 退出pycharm 0x5, 时间调整回来. ##注册方法2### 注册方法: ...
- 【转载】C#中的==、Equal、ReferenceEqual
1. ReferenceEquals, == , Equals Equals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等. a) ReferenceEquals ...
- idea设置提示不区分大小写
- docker--caffe
Running an official image You can run one of the automatic builds. E.g. for the CPU version: docker ...
- 对A-Star寻路算法的粗略研究
首先来看看完成后的效果: 其中灰色代表路障,绿色是起点和移动路径,红色代表终点 // = openArray[i+1].F) { minNode = openArray[i+1]; } } sta ...
- 项目中遇到Uncaught TypeError: Converting circular structure to JSON报错问题
最近公司项目中出现一个报错Uncaught TypeError: Converting circular structure to JSON,,根据上述报错可以知道代码是运行到JSON.stringi ...
- 使用注解来构造IoC容器-转
新手,对于一些觉得有用的东西,直接转过来用了,自己理解的比较肤浅 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<co ...
- Qt中的打印操作
Qt中对打印的支持是有一个独立的printsupport模块来完成的,所以,要想在程序中使用Qt的打印功能,必须先在pro文件中添加下面这句代码: QT += printsupport在这个模块中,提 ...
- 利用ES6中的Proxy和Reflect 实现简单的双向数据绑定
利用ES6中的Proxy (代理) 和 Reflect 实现一个简单的双向数据绑定demo. 好像vue3也把 obj.defineProperty() 换成了Proxy+Reflect. 话不多说 ...