Gym - 100341C FFT优化DP
题目链接:传送门
题解:
设定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代码
- #include<bits/stdc++.h>
- using namespace std;
- #pragma comment(linker, "/STACK:102400000,102400000")
- #define ls i<<1
- #define rs ls | 1
- #define mid ((ll+rr)>>1)
- #define pii pair<int,int>
- #define MP make_pair
- typedef long long LL;
- typedef unsigned long long ULL;
- const long long INF = 1e18+1LL;
- const double pi = acos(-1.0);
- const int N = 7e5+, M = 1e3+,inf = 2e9;
- const long long P=786433LL,mod = 786433LL;
- const LL G=10LL;
- LL mul(LL x,LL y){
- return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
- }
- LL qpow(LL x,LL k){
- LL ret=;
- while(k){
- if(k&) ret=mul(ret,x);
- k>>=;
- x=mul(x,x);
- }
- return ret;
- }
- LL wn[];
- void getwn(){
- for(int i=; i<=; ++i){
- int t=<<i;
- wn[i]=qpow(G,(P-)/t);
- }
- }
- int len;
- void NTT(LL y[],int op){
- for(int i=,j=len>>,k; i<len-; ++i){
- if(i<j) swap(y[i],y[j]);
- k=len>>;
- while(j>=k){
- j-=k;
- k>>=;
- }
- if(j<k) j+=k;
- }
- int id=;
- for(int h=; h<=len; h<<=) {
- ++id;
- for(int i=; i<len; i+=h){
- LL w=;
- for(int j=i; j<i+(h>>); ++j){
- LL u=y[j],t=mul(y[j+h/],w);
- y[j]=u+t;
- if(y[j]>=P) y[j]-=P;
- y[j+h/]=u-t+P;
- if(y[j+h/]>=P) y[j+h/]-=P;
- w=mul(w,wn[id]);
- }
- }
- }
- if(op==-){
- for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
- LL inv=qpow(len,P-);
- for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
- }
- }
- LL dp[][N],tmp[N];
- int n,h;
- int main() {
- freopen("avl.in", "r", stdin);
- freopen("avl.out", "w", stdout);
- getwn();
- scanf("%d%d",&n,&h);
- dp[][] = ,dp[][] = ,dp[][] = ;
- len = ;
- for(int i = ; i <= h; ++i) {
- len = (<<(i+));
- NTT(dp[i-],);NTT(dp[i-],);
- for(int j = ; j < len; ++j)
- tmp[j] = (dp[i-][j] * dp[i-][j])%mod;
- NTT(tmp,-);
- for(int j = ; j < len; ++j)
- dp[i][j] = 2LL*tmp[j-] % mod;
- for(int j = ; j < len; ++j)
- tmp[j] = (dp[i-][j] * dp[i-][j])%mod;
- NTT(tmp,-);
- for(int j = ; j < len; ++j)
- dp[i][j] += tmp[j-], dp[i][j] %= mod;
- NTT(dp[i-],-);
- NTT(dp[i-],-);
- }
- printf("%lld\n",dp[h][n]);
- return ;
- }
Gym - 100341C FFT优化DP的更多相关文章
- Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp
比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...
- 【题解】Music Festival(树状数组优化dp)
[题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...
- bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...
- bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- [BZOJ3156]防御准备(斜率优化DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- 优化DP的奇淫技巧
DP是搞OI不可不学的算法.一些丧心病狂的出题人不满足于裸的DP,一定要加上优化才能A掉. 故下面记录一些优化DP的奇淫技巧. OJ 1326 裸的状态方程很好推. f[i]=max(f[j]+sum ...
随机推荐
- 九度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)的输出结果是 ...
- ios弹性头部
很久没写博客了,金天有点时间来写下,一直觉得弹性头部很炫,看起来高大上,写起来蛮简单的 层次分析 一共有3层,最底部是图像层,中间是scrollView或者它的子类,最上层是scrollView上面添 ...
- BZOJ 3270 博物馆 ——概率DP 高斯消元
用$F(i,j)$表示A在i,B在j的概率. 然后很容易列出转移方程. 然后可以高斯消元了! 被一个问题困扰了很久,为什么起始点的概率要加上1. (因为其他博客上都是直接写成-1,雾) 考虑初始状态是 ...
- jenkins换端口号
两个地方 1,检查 /etc/init.d/jenkins 脚本,修改 do_start 函数的 check_tcp_port 命令,端口号从 8080 换成 8082: 2,修改 /etc/defa ...
- BSGS算法 (小步大步 Baby Step Gaint Step)
当你要求满足: $$ A^x \equiv B \ (\bmod \ P) $$ 的最小非负整数 x (gcd(A,P)==1)就可以用到 BSGS 了 设 $ m=\sqrt{P} $ 向上取整 处 ...
- 洛谷 [P2734] 游戏
博弈论+区间dp 有博弈论吗?大约只有一个博弈论的壳子 设 dp[i][j] 表示区间 i ~ j 先手最多能取多少, 它可以由 i ~ j - 1 与 i + 1 ~ j 来转移, 等于上述两个区间 ...
- bootstrap-datatables
刚写到datatimepicker的时候想到这个问题. 这可以说是我接触到的第一个功能如此齐全的一款依赖型插件.我把依赖于别人库的插件这么称呼. 首先上官网:http://datatables.clu ...
- python操作excel--生成图表
[问题] 想要折腾Python中的Excel中的图标,Chart,Graph. [解决过程] 1.参考: use python to generate graph in excel 说是可以用pywi ...
- MySQL什么时候会使用内部临时表?
1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...
- 基于SSH+shiro+solr的家庭记账系统
项目地址: https://github.com/jianghuxiaoao/homeaccount