[Sicily online] 1028. Hanoi Tower Sequence

Source: Internet
Author: User
Constraints

Time Limit: 1 secs, memory limit: 32 MB

Description

HANOI tower is a famous game hosted ted 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. obviusly, 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 isT, The number of test cases.
Each test case contains one integerP(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

414100100000000000000

Sample output

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

Question Analysis:

1. A large integer. It must be of the string type.

2. Let's look at the example:

1:1

2: 121

PM 21

4:121312141231621

We can find that the nth Group of the rule is (n-1 data) + N + (n-1 data)

Another rule is the series 121312141213121, where the occurrence of 1, 2, and 3 is an equal interval, if n % 2 = 1 is required, the result is 1 N % 4 = 2 and 2 N % 8 = 3.

It can be seen that the number of times N is divided by 2 + 1 is the result.

# Include <iostream> # include <stdio. h >#include <cmath> # include <iomanip> # include <vector> # include <string> # include <algorithm> # include <sstream> using namespace STD; // The return value is string. The divisor Int Is the remainder. Pair <string, int> strmod (string X1, int x2) {int num = 0; string result; For (string :: size_type I = 0; I <x1.size (); I ++) {num = num * 10 + (x1 [I]-'0'); int TMP = num/2; num = num % X2; Result = Result + (char) (TMP + '0');} while (1) {If (result. size () = 1) bre AK; If (result [0] = '0') result. erase (result. begin (); else break;} return make_pair (result, num);} int main () {int N; CIN> N; For (INT I = 0; I <n; I ++) {string data; CIN> data; cout <"case" <I + 1 <":"; int COUNT = 0; for (string: size_type J = 0; j ++) {pair <string, int> TMP = strmod (data, 2); Data = TMP. first; If (TMP. second = 0) {count ++;} else break;} cout <count + 1 <Endl; if (I! = N-1) cout <Endl ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.