POJ 2499 Binary Tree
题意:二叉树的根节点为(1,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数。
分析:往回一步一步走肯定超时,不过如果a>b,大的有点多,就没必要一步一步走,直接a/b就好了,然后把a变成a%b取余,那么a/b就是从现在的a到原来的a向左走的步数。(a<b同理)
同理,如果a=1的话,那么也没必要往回走了,因为从根节点到现在的结点就是向右走了b-1。(b=1同理)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int main(){
int T;
scanf("%d", &T);
for(int i = ; i <= T; ++i){
printf("Scenario #%d:\n", i);
int a, b;
scanf("%d%d", &a, &b);
int l, r;
l = r = ;
while(){
if(a == ){
r += b - ;
break;
}
if(b == ){
l += a - ;
break;
}
if(a > b){
l += a / b;
a = a % b;
}
else if(a < b){
r += b / a;
b = b % a;
}
}
printf("%d %d\n", l, r);
printf("\n");
}
return ;
}
POJ 2499 Binary Tree的更多相关文章
- Poj 2499 Binary Tree(贪心)
		题目链接:http://poj.org/problem?id=2499 思路分析:结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求 ... 
- POJ 2499 Binary Tree(二叉树,找规律)
		题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ... 
- Binary Tree                                                       分类:            POJ             2015-06-12 20:34    17人阅读    评论(0)    收藏
		Binary Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6355 Accepted: 2922 Descr ... 
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
		相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ... 
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ... 
- Leetcode 笔记 110 - Balanced Binary Tree
		题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ... 
- Leetcode, construct binary tree from inorder and post order traversal
		Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ... 
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
		Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ... 
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
		One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ... 
随机推荐
- POJ  3126 Prime Path  (BFS)
			[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ... 
- 类的__slots__属性
			为什么有"slots"属性? 默认情况下,python对象队象的每个实例(instance)都会有一个字典来存储该实例的属性,这样做的好处在于运行时期每个对象可以任意设置新的属性. ... 
- javascript实现经纬度与地址的互转
			最近项目中会用到将地址转换为经纬的.从出来,还一直未遇到过这类问题,下来自己提前学习了,将自己所学的记录在案. 在网上找了很多资料,最后确定了,百度的API,有实现相关的接口(API地址).使用API ... 
- SQL 必知必会-- 第1课:数据库基础和什么是SQL
			第1课 了解SQL 1 1.1 数据库基础 11.2 什么是SQL 61.3 动手实践 71.4 小结 8 第一课主要是一些概念,具体笔记如下: 1,数据库(database):保存有组织的数据的容器 ... 
- [WinForm] TableLayoutPanel和FlowLayoutPanel的使用
			这篇文章主要跟大家分享下,在配餐系统的开发中,对tableLayoutPanel 和 flowLayoutPanel 控件的使用方法和技巧 ——后附上 测试demo, 相信需要的朋友下载看后能很快的知 ... 
- 【Mood 16 】史上最全github使用方法:github入门到精通
			[初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git 是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管 ... 
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
			本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ... 
- uva 216 Getting in Line 最短路,全排列暴力做法
			题目给出离散的点,要求求出一笔把所有点都连上的最短路径. 最多才8个点,果断用暴力求. 用next_permutation举出全排列,计算出路程,记录最短路径. 这题也可以用dfs回溯暴力,但是用最小 ... 
- Uva 10305 - Ordering Tasks  拓扑排序基础水题 队列和dfs实现
			今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ... 
- IQueryable与IEnumberable的区别
			IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ... 
