题目链接:传送门

题解:

  设定dp[i][j]在深度为i下,使用j个节点的方案数

  显然的转移方程组就是 dp[h][n] = dp[h-1][i] * dp[h-1][n-i-1] + 2*dp[h-1][i]*dp[h-2][n-i-1];

  卷积形式

  利用FFT加速求解dp[h]

下面是AC代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #pragma comment(linker, "/STACK:102400000,102400000")
  4. #define ls i<<1
  5. #define rs ls | 1
  6. #define mid ((ll+rr)>>1)
  7. #define pii pair<int,int>
  8. #define MP make_pair
  9. typedef long long LL;
  10. typedef unsigned long long ULL;
  11. const long long INF = 1e18+1LL;
  12. const double pi = acos(-1.0);
  13. const int N = 7e5+, M = 1e3+,inf = 2e9;
  14.  
  15. const long long P=786433LL,mod = 786433LL;
  16. const LL G=10LL;
  17.  
  18. LL mul(LL x,LL y){
  19. return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
  20. }
  21. LL qpow(LL x,LL k){
  22. LL ret=;
  23. while(k){
  24. if(k&) ret=mul(ret,x);
  25. k>>=;
  26. x=mul(x,x);
  27. }
  28. return ret;
  29. }
  30. LL wn[];
  31. void getwn(){
  32. for(int i=; i<=; ++i){
  33. int t=<<i;
  34. wn[i]=qpow(G,(P-)/t);
  35. }
  36. }
  37.  
  38. int len;
  39. void NTT(LL y[],int op){
  40. for(int i=,j=len>>,k; i<len-; ++i){
  41. if(i<j) swap(y[i],y[j]);
  42. k=len>>;
  43. while(j>=k){
  44. j-=k;
  45. k>>=;
  46. }
  47. if(j<k) j+=k;
  48. }
  49. int id=;
  50. for(int h=; h<=len; h<<=) {
  51. ++id;
  52. for(int i=; i<len; i+=h){
  53. LL w=;
  54. for(int j=i; j<i+(h>>); ++j){
  55. LL u=y[j],t=mul(y[j+h/],w);
  56. y[j]=u+t;
  57. if(y[j]>=P) y[j]-=P;
  58. y[j+h/]=u-t+P;
  59. if(y[j+h/]>=P) y[j+h/]-=P;
  60. w=mul(w,wn[id]);
  61. }
  62. }
  63. }
  64. if(op==-){
  65. for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
  66. LL inv=qpow(len,P-);
  67. for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
  68. }
  69. }
  70. LL dp[][N],tmp[N];
  71. int n,h;
  72. int main() {
  73. freopen("avl.in", "r", stdin);
  74. freopen("avl.out", "w", stdout);
  75. getwn();
  76. scanf("%d%d",&n,&h);
  77. dp[][] = ,dp[][] = ,dp[][] = ;
  78. len = ;
  79. for(int i = ; i <= h; ++i) {
  80. len = (<<(i+));
  81. NTT(dp[i-],);NTT(dp[i-],);
  82. for(int j = ; j < len; ++j)
  83. tmp[j] = (dp[i-][j] * dp[i-][j])%mod;
  84. NTT(tmp,-);
  85. for(int j = ; j < len; ++j)
  86. dp[i][j] = 2LL*tmp[j-] % mod;
  87. for(int j = ; j < len; ++j)
  88. tmp[j] = (dp[i-][j] * dp[i-][j])%mod;
  89. NTT(tmp,-);
  90. for(int j = ; j < len; ++j)
  91. dp[i][j] += tmp[j-], dp[i][j] %= mod;
  92. NTT(dp[i-],-);
  93. NTT(dp[i-],-);
  94. }
  95. printf("%lld\n",dp[h][n]);
  96. return ;
  97. }

Gym - 100341C FFT优化DP的更多相关文章

  1. Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp

    比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...

  2. 【题解】Music Festival(树状数组优化dp)

    [题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...

  3. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  4. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  5. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  6. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  7. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  8. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  9. 优化DP的奇淫技巧

    DP是搞OI不可不学的算法.一些丧心病狂的出题人不满足于裸的DP,一定要加上优化才能A掉. 故下面记录一些优化DP的奇淫技巧. OJ 1326 裸的状态方程很好推. f[i]=max(f[j]+sum ...

随机推荐

  1. 九度oj 题目1345:XXX定律之画X

    题目描述: 给你一个n,然后让你输出F(n)规则是这样的,F(n)的输出结果是:F(n-1)     F(n-1)       F(n-1) F(n-1)      F(n-1) F(1)的输出结果是 ...

  2. ios弹性头部

    很久没写博客了,金天有点时间来写下,一直觉得弹性头部很炫,看起来高大上,写起来蛮简单的 层次分析 一共有3层,最底部是图像层,中间是scrollView或者它的子类,最上层是scrollView上面添 ...

  3. BZOJ 3270 博物馆 ——概率DP 高斯消元

    用$F(i,j)$表示A在i,B在j的概率. 然后很容易列出转移方程. 然后可以高斯消元了! 被一个问题困扰了很久,为什么起始点的概率要加上1. (因为其他博客上都是直接写成-1,雾) 考虑初始状态是 ...

  4. jenkins换端口号

    两个地方 1,检查 /etc/init.d/jenkins 脚本,修改 do_start 函数的 check_tcp_port 命令,端口号从 8080 换成 8082: 2,修改 /etc/defa ...

  5. BSGS算法 (小步大步 Baby Step Gaint Step)

    当你要求满足: $$ A^x \equiv B \ (\bmod \ P) $$ 的最小非负整数 x (gcd(A,P)==1)就可以用到 BSGS 了 设 $ m=\sqrt{P} $ 向上取整 处 ...

  6. 洛谷 [P2734] 游戏

    博弈论+区间dp 有博弈论吗?大约只有一个博弈论的壳子 设 dp[i][j] 表示区间 i ~ j 先手最多能取多少, 它可以由 i ~ j - 1 与 i + 1 ~ j 来转移, 等于上述两个区间 ...

  7. bootstrap-datatables

    刚写到datatimepicker的时候想到这个问题. 这可以说是我接触到的第一个功能如此齐全的一款依赖型插件.我把依赖于别人库的插件这么称呼. 首先上官网:http://datatables.clu ...

  8. python操作excel--生成图表

    [问题] 想要折腾Python中的Excel中的图标,Chart,Graph. [解决过程] 1.参考: use python to generate graph in excel 说是可以用pywi ...

  9. MySQL什么时候会使用内部临时表?

    1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...

  10. 基于SSH+shiro+solr的家庭记账系统

    项目地址: https://github.com/jianghuxiaoao/homeaccount