Sicily 1028. Hanoi Tower Sequence

Source: Internet
Author: User

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 is given a tower of n disks, initially stacked in decreasing size on one of the 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-of-tackle this problem was 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's 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 would be.
Given An integer p, your task was 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
414100100000000000000
Sample Output
Case 1:1 case 2:3 case 3:3 case 4:15
Problem Source

ZSUACM Team Member

This one is made by yourself, spents 0.02s:

#include <stdio.h> #include <string.h>//by observing the law, the number on position K is the number of times that K can be divisible +1int ac[110] = {0};//for high precision divided by low precision bool         is_ok;//determines whether the next time can be divided by 2int divi (int first_pos, int length) {for (int i = first_pos; i < length; i++) {//High precision divided by low precision            if (Ac[i] > 0) {if (Ac[i]% 2 = = 1) {ac[i + 1] + = 10;        } Ac[i]/= 2; }} if (Ac[length] > 0) {//If the length position is greater than 0 (Ac[i + 1] + + 10;) length-1 This one appears singular, can not be complete except IS_OK = False        ;    return 0;    } if (Ac[first_pos] = = 0)//update digit length return first_pos + 1; else return first_pos;}    int main () {int T, I, length, counter, Kongzhi = 0, First_pos, J;        scanf ("%d", &t);        for (i = 1; I <= t; i++) {char a[105] = {'} '};        IS_OK = true;        memset (AC, 0, sizeof (AC));        scanf ("%s", a);                Length = strlen (a);        if (Kongzhi)//output format printf ("\ n");                Kongzhi = 1; if ((a[Length-1]-' 0 ')% 2 = = 1) {//If the singular is directly judged by printf ("Case%d:1\n", I, 1);        Continue                } counter = 0; if (length <= 20) {//If the maximum value of unsigned long long is less than 18446744073709551615 directly judging char judge[] = "18446744073709551615            ";                if (strcmp (A, judge) <= 0 | | length <=) {unsigned long long sum = 0;                for (j = 0; J < length; J + +) {sum = sum * + a[j]-' 0 ';                    } while (sum% 2 = = 0) {counter++;                Sum/= 2;                } printf ("Case%d:%d\n", I, counter + 1);            Continue        }} for (j = 0; J < length; J + +) {Ac[j] = a[j]-' 0 ';                } first_pos = 0;            while (1) {//For high precision divided by low precision First_pos = Divi (first_pos, length);            if (IS_OK) counter++;             Else   Break    } printf ("Case%d:%d\n", I, counter + 1);                               } return 0;}
later, using an array of 10^9 's cardinality to improve efficiency, this time is 0s:

#include <stdio.h> #include <string.h> #include <math.h> #define MAX_LENGTH 105//Here is a 9 storage method with a base of 10, Improved efficiency int Base_index = 9;//cardinality is 10 9 times long long unit_base = (long Long) POW (10, 9);//base size long long UNIT[MAX_LENGTH/9 + 9];//base Maximum length array of 10^9 long long per_base[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};//build Unit array    Use the Char char_a[max_length];bool is_ok;//to determine if you want to divide the next time by 2int ready (int LENGTH) {memset (unit, 0, sizeof (unit)); int unit_length = length% 9 = = 0?        LENGTH/9: LENGTH/9 + 1;//calculation required unit length int i, j, K;            for (i = unit_length-1, j = length-1; I >= 0; i--) {//Note Be sure to build from the end for (k = 0; J >= 0; J--, k++) {            if (k = = 9)//each 9 digit update one jumps to the previous unit subscript break;        Unit[i] + = (char_a[j]-' 0 ') * per_base[k]; }} return unit_length;}        int divi (int first_pos, int last_pos) {if (Unit[last_pos]% 2 = = 1) {//If the end of the singular is directly unable to continue dividing by 2 Is_ok = false;    return 0; } foR (int i = first_pos; I <= last_pos; i++) {//High precision Division if (Unit[i]% 2 = = 1) {unit[i + 1] + = unit_base;    } Unit[i]/= 2;    } if (Unit[first_pos] = = 0)//update Unit array First return First_pos + 1; else return first_pos;}    int main () {int T, I, length, counter, Kongzhi = 0, First_pos, J;        scanf ("%d", &t);        for (i = 1; I <= t; i++) {if (Kongzhi)//output format printf ("\ n");                Kongzhi = 1;        memset (char_a, ' n ', sizeof (char_a));        scanf ("%s", char_a);        Length = strlen (char_a);                IS_OK = true;            if ((char_a[length-1]-' 0 ')% 2 = = 1) {//If the singular is directly judged by printf ("Case%d:1\n", I, 1);        Continue                } counter = 0; if (length <= 20) {//If the maximum value of unsigned long long is less than 18446744073709551615 directly judging char judge[] = "18446744073709551615            ";            if (strcmp (char_a, judge) <= 0 | | length <= 19) {    unsigned long long sum = 0;                for (j = 0; J < length; J + +) {sum = sum * + char_a[j]-' 0 ';                    } while (sum% 2 = = 0) {counter++;                Sum/= 2;                } printf ("Case%d:%d\n", I, counter + 1);            Continue        }} int unit_length = ready (length);                First_pos = 0; while (1) {First_pos = Divi (First_pos, unit_length-1);//constant high precision divided by 2 if (IS_OK) {Coun            ter++;            } else {break;            }} printf ("Case%d:%d\n", I, counter + 1);                            } return 0;}

Sicily 1028. Hanoi Tower Sequence

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.