Logo
(追記) (追記ここまで)

12278번 - Rational Number Tree (Small) 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB109867581.522%

문제

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:

  1. 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.
  2. Given p/q, find its position in the array. As an example, the input 1/2 results in the output 2.

입력

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:

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

출력

For each test case:

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

제한

  • 1 ≤ T ≤ 100; p and q are relatively prime.
  • 1 ≤ n, p, q ≤ 216-1; p/q is an element in a tree with level number ≤ 16.

예제 입력 1

4
1 2
2 1 2
1 5
2 3 2

예제 출력 1

Case #1: 1 2
Case #2: 2
Case #3: 3 2
Case #4: 5

힌트

출처

Contest > Google > Google's Coding Competitions > Google of Greater China Test for New Grads of 2014 > Round A China New Grad Test 2014 B1번

Contest > Google > Kick Start > Google Kick Start 2013 > Round A C1번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /