# 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

#include <iostream>
#include <string.h>

int a[101];
int len;

int find(int start) {
if (a[len - 1] % 2 == 1)
return 0;

for (int d = start; d < len - 1; ++d) {
a[d + 1] += (a[d] % 2) * 10;
a[d] /= 2;
}
a[len - 1] /= 2;

if (a[start] == 0)
start++;
return 1 + find(start);
}

int main()
{
int test;
std::cin >> test;
std::string key;

for (int c = 1; c <= test; ++c) {
if (c != 1)
std::cout << std::endl;

std::cin >> key;
memset(a, 0, sizeof(a));
for (int d = 0; key[d] != '\0'; ++d)
a[d] = key[d] - '0';
len = key.length();

std::cout << "Case " << c << ": " << 1 + find(0) << std::endl;
}
return 0;
}

• 本文已收录于以下专栏：

## sicily 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Ha...
• yuhao199555
• 2015年05月25日 23:13
• 373

## Sicily 1028. Hanoi Tower Sequence

• q271736642
• 2013年10月05日 21:00
• 1031

## [sicily online]1028. Hanoi Tower Sequence

Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tower is a famous...
• qiuchenl
• 2012年11月26日 20:43
• 2586

## 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence
• Here_I_Come
• 2013年10月31日 18:52
• 725

## SOJ 1028. Hanoi Tower Sequence

• richlaji
• 2014年11月04日 11:42
• 300

## python 汉诺塔问题(Tower of Hanoi Puzzle)

• And_w
• 2016年03月18日 21:46
• 636

## Sicily 1028 Hanoi Tower Sequence

/* 【题目大意】 汉诺塔，将其移动的每一步的盘号列出作为序列，求第p步需要移动的盘的盘号 序列如下：1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1.... ...
• fqeunagn_II
• 2013年01月05日 21:18
• 1339

## zoj 1239 Hanoi Tower Troubles Again!

。。。
• xinag578
• 2015年03月08日 19:44
• 498

## Hanoi Tower问题分析

• zinss26914
• 2013年08月07日 23:21
• 1951

## 汉诺塔(Tower of Hanoi)问题的求解——利用栈与递归

• cainv89
• 2016年05月22日 23:32
• 6637

举报原因： 您举报文章：sicily 1028. Hanoi Tower Sequence 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)