SOJ 1028

1028. Hanoi Tower Sequence

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never moving a larger one onto a smaller. 

The best way to tackle this problem is well known: We first transfer the n-1 smallest to a different peg (by recursion), then move the largest, and finally transfer the n-1 smallest back onto the largest. For example, Fig 1 shows the steps of moving 3 disks from peg 1 to peg 3.

Now we can get a sequence which consists of the red numbers of Fig 1: 1, 2, 1, 3, 1, 2, 1. The ith element of the sequence means the label of the disk that is moved in the ith step. When n = 4, we get a longer sequence: 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1. Obviously, the larger n is, the longer this sequence will be.
Given an integer p, your task is to find out the pth element of this sequence.

Input

The first line of the input file is T, the number of test cases.
Each test case contains one integer p (1<=p<10^100).

Output

Output the pth element of the sequence in a single line. See the sample for the output format.
Print a blank line between the test cases.

Sample Input

4
1
4
100
100000000000000

Sample Output

Case 1: 1
 
Case 2: 3
 
Case 3: 3
 
Case 4: 15

Problem Source

ZSUACM Team Member


样例: 

1:1

2:121

3:1213121

4:121312141213121

  可以看出移动的第n步,如果n%2^1==1的话则第n步就是1,若n%2^2==2的话第n步就是2,同理n%2^k==k的话第n步就是k。若n%2^k==k是n可以被2整除k-1次的充要条件,也就是说我们只需要计算n可以被2整除的次数,然后次数+1就是最终结果。

  大数自然是要string读取,然后保存在数组里,模仿除法竖式的过程来进行除法。


// Problem#: 1028
// Submission#: 5052815
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include
#include
#include
#include
using namespace std;
int T,count;//T为case个数,count为被2整除的次数 
string N;//N用来保存大数 
vector initial;//将大数保存在容器中,每个数字即使一个容器的一个元素 
int answer(string n)
{
    count=0;
    initial.clear();
    for(int i=0;i tmp;//用来保存除以2后的结果 
        int temp=initial[0];
        for(int i=0;i>T;
    int firstTime=1;
    for(int i=0;i>N;
        if(!firstTime) cout<


你可能感兴趣的:(SOJ)