CodeForces_507C_Guess Your Way Out!(规律)
来源:互联网 发布:程序员网 编辑:程序博客网 时间:2024/04/28 08:04
题型:数论
题意:
高度为h的满二叉树,从根节点出发,按照"LRLRLRLR...."的指令往下走,走到第n个叶子结点结束。如果走到了非n的叶子结点,那么就跳过下一个指令,回溯然后按照指令继续走没有走过的结点。(看题目解释就能看懂)
问到达第n个叶子结点之前,有多少个结点被走过。
分析:
先设置一个初始方向dir,因为是二叉树,可以设置要么为0要么为1。
h不断减少,每一层,判断所走方向的子树包不包含叶子节点n。
如果是,那么走进去,dir取反,走过一个节点。
如果不是,那么会将那个方向的子树的所有结点都走一遍。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define LL long longusing namespace std;LL n;int h;int main() { LL ans = 0; LL dir = 0; cin>>h>>n; n--; while(h--) { if(((n>>h)&1)^dir) { ans += (1LL<<(h+1)); } else { ans ++; dir ^= 1; } } cout<<ans<<endl; return 0;}
0 0
- CodeForces_507C_Guess Your Way Out!(规律)
- codeforces 558D Guess Your Way Out! II 规律
- C. Guess Your Way Out!
- CF-Guess Your Way Out!
- cf_287 C. Guess Your Way Out!
- CF C.Guess Your Way Out!
- Soma.js – Your Way Out of Chaotic JavaScript
- Codeforces 507C - Guess Your Way Out! (思维)
- codeforces--507C--Guess Your Way Out!(论读题的重要性。。)
- 【codeforces】 Round #287 (Div. 2) C. Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
- C. Guess Your Way Out!(Codeforces Round#287(div2))
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
- [CF]287div2 C. Guess Your Way Out!
- CF #287 div2 C Guess Your Way Out!
- Codeforces Round #287 (Div. 2)C. Guess Your Way Out!
- codeforces 558 D Guess Your Way Out! II
- ElectroServer5打包及部署
- Android开源项目分类汇总 - GitHub
- if (!x++)与 if (!++x)的区别
- Xcode 单元测试
- Oracle Query Performance Tuning: 12–Step Program
- CodeForces_507C_Guess Your Way Out!(规律)
- http://blog.csdn.net/u011639256/article/details/18575557
- 有向无环图的动态规划实现
- AndroidStudio开发NDK
- note for "strtok"
- SO_RCVBUF和SO_SNDBUF默认值
- eclipse项目上有红色感叹号
- directshow的中文资料之设备列举和捕捉接口
- hdu 1233 畅通工程