Google China New Grad Test 2014 Round A Problem B
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.
InputThe first line of the input gives the number of test cases, T. 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.
OutputFor 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.
Limits1 ≤ T ≤ 100; p and q are relatively prime.
Small dataset1 ≤ n, p, q ≤ 216-1; p/q is an element in a tree with level number ≤ 16.
Large dataset1 ≤ n, p, q ≤ 264-1; p/q is an element in a tree with level number ≤ 64.
SampleInput 2 1 2 2 3 2 Output Case #1: 1 2 Case #2: 2 Case #3: 3 2 Case #4: 5
解题思路:这题太遗憾了。匆忙之中提交大数据,结果发现数据超出long范围,尽快用BigInteger改写,刚好超过了提交时间。也是一道水题,找出推导的方法,递归求解就可以了。
BigInteger findPQ(BigInteger p, BigInteger q) { if (p.equals(new BigInteger("1")) q.equals(new BigInteger("1"))) return new BigInteger("1"); if (p.compareTo(q) 0) return findPQ(p, q.subtract(p)).multiply(new BigInteger("2")); else return findPQ(p.subtract(q), q).multiply(new BigInteger("2")).add(new BigInteger("1")); BigInteger[] findN(BigInteger n) { BigInteger[] ret = new BigInteger[2]; if (n.intValue() == 1) { ret[0] = new BigInteger("1"); ret[1] = new BigInteger("1"); return ret; BigInteger p = n.divide(new BigInteger("2")); BigInteger[] pr = findN(p); if (n.equals(p.multiply(new BigInteger("2")))) { // left ret[0] = pr[0]; ret[1] = pr[0].add(pr[1]); return ret; } else { // right ret[0] = pr[0].add(pr[1]); ret[1] = pr[1]; return ret; void run() { int tests = sc.nextInt(); for (int test = 1; test = tests; test++) { int id = sc.nextInt(); System.out.print(String.format("Case #%d:", test)); if (id == 1) { BigInteger n = new BigInteger(sc.next()); for (BigInteger i : findN(n)) { System.out.print(" " + i); System.out.println(); } else { BigInteger p = new BigInteger(sc.next()); BigInteger q = new BigInteger(sc.next()); BigInteger res = findPQ(p, q); System.out.println(" " + res); }
相关文章
- Google凭借Buzz进军社交网络
- google地图怎么下载离线地图_谷歌瓦片行列号算经纬度
- android flag_activity_new_task结束,怎样避免使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TA
- 免费google代理服务器_google服务器ip地址
- Google Aviator——轻量级 Java 表达式引擎实战
- 什么是 Web 应用里加载 google font 带来的 FOIT 和 FOUT 问题?
- 警惕!Emotet新变体正从Google Chrome中窃取你的信用卡信息
- Google Friend Connect: 给你的网站加上社会化属性
- 使用 Google Analytics 统计 Feed 流量
- Google Android MVP示例解读
- 揭秘 TensorFlow:Google 开源到底开的是什么?
- 关于李飞飞、李佳重磅加盟 Google,这里有三个有意思的地方
- 挑战 Google TPU,AI 芯片新玩家面临哪些难题?
- 谷歌宣布Daydream VR一体机,支持Inside-out位置追踪 | Google I/O 2017
- Google I/O 2017:李飞飞 ——我为什么对TensorFlow研究云感到兴奋
- 美参议院新法案或将颠覆苹果和Google的应用商店主导地位
- 模拟一个类似百度google的模糊搜索下拉列表