ACdream1187(简单找规律)
来源:互联网 发布:vp矢量化软件 编辑:程序博客网 时间:2024/06/07 07:59
C - Problem C
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus
Problem Description
Consider an infinite complete binary tree where the root node is 1/1 and left and right childs of node p/q are p/(p+q) and (p+q)/q, respectively. This tree looks like:
1/1 ______|______ | | 1/2 2/1 ___|___ ___|___ | | | |1/3 3/2 2/3 3/1...
It is known that every positive rational number appears exactly once in this tree. A level-order traversal of the tree results in the following array:
1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...
Please solve the following two questions:
- Find the n-th element of the array, where n starts from 1. For example, for the input 2, the correct output is 1/2.
- Given p/q, find its position in the array. As an example, the input 1/2 results in the output 2.
Input
The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).
T test cases follow. Each test case consists of one line.
The line contains a problem id (1 or 2) and one or two additional integers:
- If the problem id is 1, then only one integer n is given, and you are expected to find the n-th element of the array.
- If the problem id is 2, then two integers p and q are given, and you are expected to find the position of p/q in the array.
p and q are relatively prime.
1 ≤ n, p, q ≤ 264-1
p/q is an element in a tree with level number ≤ 64.
Output
For each test case:
- If the problem id is 1, then output one line containing "Case #x: p q", where x is the case number (starting from 1), and p, q are numerator and denominator of the asked array element, respectively.
- If the problem id is 2, then output one line containing "Case #x: n", where x is the case number (starting from 1), and n is the position of the given number.
Sample Input
41 22 1 21 52 3 2
Sample Output
Case #1: 1 2Case #2: 2Case #3: 3 2Case #4: 5
题意:RT
思路:规律很明显,递归往上求解即可,这题要用unsigned long long,结果我比赛的时候用long long,被这个坑到了,深刻的记下了
0 0
- ACdream1187(简单找规律)
- codeforce(找规律的简单题)
- zyf的简单数学题(找规律)
- hdoj1021简单题、找规律
- hdoj1005简单题、找规律
- hdoj1061简单题、找规律
- hdoj1097简单题、找规律
- hdoj1098简单题、找规律
- 简单找规律问题列表
- ZOJ2185 简单分块 找规律
- HDOJ1021(找规律)
- POJ1740(找规律)
- hdu (找规律)
- hdu4915(找规律)
- HDU5100(找规律)
- Hexagons!(找规律)
- Alarm(找规律)
- hdu6130(找规律)
- 安装配置samba服务
- javascript--瀑布流
- 2015阿里巴巴秋招在线笔试题
- sdutoj1020递推——神、上帝以及老天爷
- Langton's Ant
- ACdream1187(简单找规律)
- 数据结构----单向链表之 新建-插入-删除-排序(选择法)-合并-删除-销毁
- Codeforces Round #249 (Div. 2) D. Special Grid
- hdu 3886 Final Kichiku “Lanlanshu” (数位dp)
- ubuntu部署android自动编译打包
- 用U盘重装Win7系统方法
- 1070. Mooncake (25)
- 合作开发-实现后台管理框架
- 字节对齐详解