标签:const std code namespace amp 二叉树 int iostream space
题意:二叉树的根节点为(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[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 10000 + 10; const int MAXT = 10000 + 10; using namespace std; int main(){ int T; scanf("%d", &T); for(int i = 1; i <= T; ++i){ printf("Scenario #%d:\n", i); int a, b; scanf("%d%d", &a, &b); int l, r; l = r = 0; while(1){ if(a == 1){ r += b - 1; break; } if(b == 1){ l += a - 1; 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 0; }
标签:const std code namespace amp 二叉树 int iostream space
原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6104849.html