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 ;
}
上一篇:zoj3820 Building Fire Stations 树的中心


下一篇:SQL2005:使用varchar(max)代替text