Google China New Grad Test 2014 Round A Problem B

Problem B. Rational Number Tree

Problem

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. 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.

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.

Limits

1 ≤ T ≤ 100; p and q are relatively prime.

Small dataset

1 ≤ n, p, q ≤ 216-1; p/q is an element in a tree with level number ≤ 16.

Large dataset

1 ≤ n, p, q ≤ 264-1; p/q is an element in a tree with level number ≤ 64.

Sample

Input  

4
1 2
2 1 2
1 5
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);
        }
    }
}
时间: 2024-10-02 18:59:24

Google China New Grad Test 2014 Round A Problem B的相关文章

Google China New Grad Test 2014 Round A Problem A

Problem A. Read Phone Number Problem Do you know how to read the phone numbers in English? Now let me tell you. For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 34

Google China New Grad Test 2014 Round A Problem C

Problem C. Sorting Problem Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges if they can p

Google 终于在 I/O 2014 大会上发布了自家车载系统 Android Auto

摘要: 上周,Google 终于在 I/O 2014 大会上发布了自家车载系统 Android Auto,自此通过屏幕投射进驻车厢的除了苹果 CarPlay 与微软的 Windows in the car,再添一员. 汽车品牌,成了这几家科技公司争 上周,Google 终于在 I/O 2014 大会上发布了自家车载系统 Android Auto,自此通过屏幕投射进驻车厢的除了苹果 CarPlay 与微软的 Windows in the car,再添一员.汽车品牌,成了这几家科技公司争夺的重要资源

Code Jam 2010 Round 1A Problem C

Problem C. Number Game Problem Arya and Bran are playing a game. Initially, two positive integers A and B are written on a blackboard. The players take turns, starting with Arya. On his or her turn, a player can replace A with A - kB for any positive

Code Jam 2010 Round 1B Problem A

Problem B. Picking Up Chicks Problem A flock of chickens are running east along a straight, narrow road. Each one is running with its own constant speed. Whenever a chick catches up to the one in front of it, it has to slow down and follow at the spe

Code Jam 2010 Round 1A Problem B

Problem B. Make it Smooth Problem You have a one-dimensional array of N pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their numbers. You can perform

Code Jam 2010 Round 1A Problem A

Problem A. Rotate Problem In the exciting game of Join-K, red and blue pieces are dropped into an N-by-N table. The table stands up vertically so that pieces drop down to the bottom-most empty slots in their column. For example, consider the followin

google 有望回归中国

Google可能会在未来三至六个月内重启中国市场战略,以百度为首的中文搜索已经大幅吞噬Google退出空余的市场份额,而g.cn.谷歌音乐等一系列本地化措施都将面临被边缘化的危险.这对Google这样一家商业公司并不是一个好消息,所有人都知道中国将会是全球较大的互联网市场,而被视作互联网代表之一的Google却游离在这一市场之外,尽管Google已经很小心避免退出对已有业务的影响,但这种影响随着时间刻度的拉长正在变得越来越明显. 2009年1月Google首席法律顾问大卫·多姆德(David D

Google靠什么来说服用户使用Google+?

在硬核用户眼中,同为实名社交网络的Google+比Facebook优秀多了:更时尚的设计,以圈子为核心的范围可控的内容分享机制,以及那些亮眼的附加功能:Photos漂亮的高分辨率大图.优秀的图片编辑.好用的自动备份.充满灵性的自动特效和Stories:对了,还有能够轻易驾驭文字.图片以及多人视频的IM hangouts. 但与此同时,就连最忠实的用户也得承认:Google+就是Google迟来多年的Facebook竞品.除了圈子等一些亮点功能外,它本质上并无创新:笨重的实名社交,仅此而已.那么,