程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ACdream1187(簡單找規律)

ACdream1187(簡單找規律)

編輯:C++入門知識

ACdream1187(簡單找規律)


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

        4
        1 2
        2 1 2
        1 5
        2 3 2

        Sample Output

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

        題意:RT
        思路:規律很明顯,遞歸往上求解即可,這題要用unsigned long long,結果我比賽的時候用long long,被這個坑到了,深刻的記下了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved