题目链接

简单的

设从节点\(x\)开始不断往左儿子走h-1步,则编号和为\(x\sum_{i=0}^{h-1}2^i=x(2^h-1)\)。

若倒数第\(i\)步走向的是右儿子,则编号和会增加\(\sum_{j=0}^{i-1}2^j=2^i-1\)。

这样,从\(x\)往下走形成的长为\(h\)的链中,其中倒数\(i,i\in T\)步时走向右儿子,倒数\(i,i\not\in T\)步走向左儿子,这样得到的编号和为

\[x(2^h-1)+\sum_{i\in T}2^i-|T|=S
\]

令\(L=\lfloor\frac{S}{2^h-1}\rfloor\),显然\(|T|<h\le\log_2(S+1),x\le L\)。又因为

\[(L-1)(2^h-1)+\sum_{i\in T}2^i-|T|\le S-(2^h-1)+(2^h-h-1)\\
=S-h<S
\]

故\(x>L-1\)进而\(x=L\),或称对每个\(h\)有唯一的\(x=L\),那么方案数为方程\(\sum_{i\in T}2^i=S+|T|+L(2^h-1)\)的解的个数。(这显然最多只有一组解)

组合的

从节点\(x\)的左右儿子出发的两天简单链分别表示为\((h_0,T_0),(h_1,T_1)\),总的编号和

\[x+2x(2^{h_0}-1)+(2x+1)(2^{h_1}-1)+\sum_{i\in T_0}2^i+\sum_{i\in T_1}2^i-|T_0|-|T_1|\\
=x(2^{h_0+1}+2^{h_1+1}-3)+2^{h_1}-1+\sum_{i\in T_0}2^i+\sum_{i\in T_1}2^i-|T_0|-|T_1|=S
\]

大胆猜想\(h_0,h_1\)确定时有唯一的\(x=\lfloor\frac{S-2^{h_1}+1}{2^{h_0+1}+2^{h_1+1}-3}\rfloor\)。

接着问题转变为在\({2^1,2^2,\cdots,2^{h_0-1}},{2^1,2^2,\cdots,2^{h_1-1}}\)中一共选取\(n\)个数之和等于\(S-(\cdots)+n\)(QAQ)的方案数。

考虑枚举\(n\),设\(f[i,j,0/1]\)表示考虑完前\(i\)个指数后,已经选了\(j\)个数字,这一位(二进制末尾第\(i+1\)位)为是否进位的方案数。其中\(f[0,0,0]=1\)。

答案是\(\sum_{h_0,h_1}(\sum_n f[\max(h_0,h_1),n,0])\)。时间复杂度\(O(\log_2^5S)\)。

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=53; LL S,L,ans;
LL P[N]={1};
LL f[N][N*2][2]; int main() {
scanf("%lld",&S); L=log2(S+1);
for(int i=1; i<N; ++i) P[i]=P[i-1]<<1;
for(int h=1; h<=L; ++h) {
LL x=S%(P[h]-1);
for(int i=h; i; --i) if(x>=P[i]-1) x-=P[i]-1;
ans+=(!x);
}
for(int h0=1; h0<L; ++h0)
for(int h1=1; S+1-P[h1]>=P[h0+1]+P[h1+1]-3; ++h1) {
LL x=(S+1-P[h1])/(P[h0+1]+P[h1+1]-3);
LL r=(S+1-P[h1])%(P[h0+1]+P[h1+1]-3);
if(!r) {ans++; continue;}
if(h0==1&&h1==1) {ans+=(S==x*5+1); continue;}
for(int n=1; n<=h0+h1; ++n) {
LL C=r+n,L=log2(C);
if(C&1) continue;
memset(f[0],0,sizeof f[0]); f[0][0][0]=1;
for(int i=1; i<=L; ++i) {
int d=(C>>i)&1; memset(f[i],0,sizeof f[i]);
for(int j=0; j<=i+i-2&&j<=n; ++j)
for(int k=0; k<2; ++k) if(f[i-1][j][k])
for(int x=0; x<2; ++x) if(!x||i<h0)
for(int y=0; y<2; ++y) if(!y||i<h1)
if(((k+x+y)&1)==d) f[i][j+x+y][(k+x+y)>>1]+=f[i-1][j][k];
}
ans+=f[L][n][0];
}
}
printf("%lld\n",ans);
return 0;
}

不知道1K+ms是怎么跑出来的

[CF750G] New Year and Binary Tree Paths的更多相关文章

  1. CF750G New Year and Binary Tree Paths(DP)

    神仙题.为啥我第一眼看上去以为是个普及题 路径有两种,第一种是从 LCA 一边下去的,第二种是从 LCA 两边都下去了的. 先考虑第一种. 先枚举路径长度 \(h\). 当 LCA 编号是 \(x\) ...

  2. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  3. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  4. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  5. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  6. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

  7. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. 字典树Trie--实现敏感词过滤

    序言 Trie树 资料 https://blog.csdn.net/m0_37907797/article/details/103272967?utm_source=apphttps://blog.c ...

  2. Windows Server 快速生成免费SSL证书 (letsencrypt)

    最近官网需求部署个SSL证书,一番操作后把借鉴的网站与实际过程记录下来 Let's Encrypt,官网是https://letsencrypt.org/,它是一个由各大公司赞助的公益组织: 有趋势有 ...

  3. vue实现v-model父子组件间的双向通信

    首先讲清楚有个缺点:父页面若同时使用多个子组件,永远只会只能实现第一个双向驱动,我是新手,还在研究.如果有高手请指教,感谢! 子组件 <script> export default { m ...

  4. 《Effective C++》结语

    九月的这三周把<Effective C++>重读了一遍,尽量以自己的理解,用最简单的(其实太深入也不会写)的语言把书里面的重点都写了下来. 由于之前找实习占用了大量的时间,写的博客都比较水 ...

  5. [LOJ3124][CTS2019|CTSC2019]氪金手游:树形DP+概率DP+容斥原理

    分析 首先容易得出这样一个事实,在若干物品中最先被选出的是编号为\(i\)的物品的概率为\(\frac{W_i}{\sum_{j=1}^{cnt}W_j}\). 假设树是一棵外向树,即父亲比儿子先选( ...

  6. HUD 1166:敌兵布阵(线段树 or 树状数组)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  7. ES6学习一--ES5

    JSON对象方法 1.将JSON字符串转换成js对象(IE7及以下不支持)(IE7及以下兼容需其他手段) JSON.parse() 2.将js对象转成JSON字符串 JSON.stringify() ...

  8. Python中Counter统计数据输出具体办法

    from collections import Counter # 列表 l_one = [1709020621, 1709020621, 1770603107, 1770603105, 177060 ...

  9. mingw下的msys显示与输入乱码

    一直很喜欢gcc+vim这个貌似已经不用在强调了,好了,我只是想说明下我的问题是首先从gcc编译出错提示开始的 正如上面所说,安装完MinGW后使用gcc一编译,这程序没有错误还好,这一有错误发现输入 ...

  10. dvm 的进程和 Linux 的进程, 应用程序的进程是否为同一个概念?

    dvm 指 dalvik 的虚拟机. 每一个 Android 应用程序都拥有一个独立的 Dalvik 虚拟机实例,应用程序都在它自己的进程中运行.而每一个 dvm 都是在 Linux 中的一个进程,所 ...