题意:

问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)$。

  1. #include <bits/stdc++.h>
  2.  
  3. #define PI acos(-1)
  4. #define P1 998244353LL
  5. #define P2 469762049LL
  6. #define LL long long
  7. #define gn 3
  8.  
  9. const int N = ;
  10.  
  11. using namespace std;
  12.  
  13. int R[N<<];
  14.  
  15. LL qpow(LL x,int n,LL P)
  16. {
  17. LL ans = ;
  18. for(;n;n>>=,x = x*x % P) if(n&) ans = ans*x % P;
  19. return ans;
  20. }
  21.  
  22. void DFT(LL a[],int n,int tp_k,LL P)
  23. {
  24. for(int i=;i<n;i++) if(i<R[i]) swap(a[i],a[R[i]]);
  25. for(int d=;d<n;d<<=)
  26. {
  27. LL wn = qpow(gn, (P-)/(d<<),P);
  28. if(tp_k == -) wn = qpow(wn, P-,P);
  29. for(int i=;i<n;i += (d<<))
  30. {
  31. LL wt = ;
  32. for(int k=;k<d;k++, wt = wt*wn % P)
  33. {
  34. LL A0 = a[i+k], A1 = wt * a[i+k+d] % P;
  35. a[i+k] = A0+A1;
  36. a[i+k+d] = A0+P-A1;
  37. if(a[i+k] >= P) a[i+k] -= P;
  38. if(a[i+k+d] >= P) a[i+k+d] -= P;
  39. }
  40. }
  41. }
  42. LL inv = qpow(n, P-,P);
  43. if(tp_k==-)
  44. for(int i=;i<n;i++) a[i] = a[i] * inv % P;
  45. }
  46.  
  47. LL A[N<<],B[N<<];
  48.  
  49. int main()
  50. {
  51. //freopen("test.txt","w",stdout);
  52. int n,K;
  53. cin>>n>>K;
  54. int L = ,tot;
  55. while((<<L)<*K) L++;
  56. tot = (<<L);
  57. for(int i=;i<tot;i++) R[i]=(R[i>>]>>)|((i&)<<(L-));
  58. for(int i=,x;i<=n;i++) scanf("%d",&x), A[x] = , B[x] = ;
  59. DFT(A,tot,,P1);
  60. for(int i=;i<tot;i++) A[i] = qpow(A[i], K, P1);
  61. DFT(A,tot,-,P1);
  62. DFT(B,tot,,P2);
  63. for(int i=;i<tot;i++) B[i] = qpow(B[i], K, P2);
  64. DFT(B,tot,-,P2);
  65. for(int i=;i<tot;i++) if(A[i] || B[i]) printf("%d ",i);
  66. printf("\n");
  67. return ;
  68. }

Thief in a Shop的更多相关文章

  1. 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 ...

  2. 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 ...

  3. C - Thief in a Shop - dp完全背包-FFT生成函数

    C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...

  4. codeforces Educational Codeforces Round 9 E - Thief in a Shop

    E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...

  5. 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 ...

  6. 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 ...

  7. CF632E Thief in a Shop 和 CF958F3 Lightsabers (hard)

    Thief in a Shop n个物品每个价值ai,要求选k个,可以重复.问能取到哪几个价值? 1 ≤ n, k ≤ 1000,1 ≤ ai ≤ 1000 题解 将选一个物品能取到的价值的01生成函 ...

  8. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. VueJS样式绑定:v-bind

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  2. pjblog支持QQ、新浪微博一键登录

    转载地址: http://www.ruisoftcn.com/blog/article.asp?id=955

  3. CAM350 10.5移动和叠层的用法

    1.快捷键 EC复制                                       IMO测量最近距离                             UZ放大,缩小尺寸 UC拷 ...

  4. 笔记03 MVVM 开发的几种模式(WPF)

    转自http://www.cnblogs.com/buptzym/p/3220910.html 在WPF系(包括SL,WP或者Win8)应用开发中,MVVM是个老生常谈的问题.初学者可能不会有感觉,但 ...

  5. vim 自动补全

    1. vim编辑器自带关键字补全 触发: ctrl + n  or ctrl + p 补全命令: <C-n>              普通关键字 [能够根据buffer以及标签文件列表等 ...

  6. CentOS7配置opencv for python && eclipse c/c++[更新]

    更改前的安装过程有些问题,主要是ffmpeg-devel的安装部分,这里重新说一下 两种安装方法: 第一种,直接: # yum install numpy opencv* 这种方法安装了之后,能够在p ...

  7. Sublime Text 3相关配置和设置

    Sublime Text 3打开txt中文乱码的解决方法 Sublime Text是一个很强大的编辑器,但是对中文的支持并不好,在Sublime Text 2 时,能够通过命令行的方式安装编码包来解决 ...

  8. Django-extra的用法

    ## select提供简单数据 # SELECT age, (age > 18) as is_adult FROM myapp_person; Person.objects.all().extr ...

  9. linux下启动和关闭weblogic(转载)

    在weblogic定义的域中可以找到如下命令: /[youHome]/domains/[yourDomain]/startWebLogic.sh /[youHome]/domains/[yourDom ...

  10. from memory cache

    from memory cache