sicily 1028. Hanoi Tower Sequence

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

不难发现,当p是2^k(k=0,1...)时,输出k+1。仔细观察序列,不断地找到最接近p的比p小的一个2的整数次幂(2^k1),然后得到p与2^k之差。这时分两种情况:

情况1:如果差是也是2的整数次幂(2^k2),则输出k2+1。

情况2:如果差不是2的整数次幂,则以差作为新的p,继续寻找最接近p的比p小的一个2的整数次幂,再做差判断,然后判断是情况1还是情况2。

说的不知道明不明白,任何数都能表达成2^k之和的形式,举个例子,如果p是十进制的14,用二进制表示就是2^3+2^2+2^1。我们只关心最后的2^1,答案是2。用二进制表达就是1110。答案就是最右边0的数量+1。代码如下:

#include <bits/stdc++.h>
using namespace std;
void divide_2(char *a,int len){
	for(int i=len-1;i>=0;i--){
		if((a[i]-'0')&1){
			a[i+1]+=5;
		}
		a[i]=(a[i]-'0')/2+'0';
	}
}
int main(){
	int t;
	scanf("%d",&t);
	int tmp=t;
	bool flag=true;
	while(t--){
		char a[200];
		scanf("%s",a);
		int len=strlen(a);
		int countt=1;
		while(!(a[len-1]-'0'&1)){
			divide_2(a,len);
			countt++;
		}
		if(!flag)
		printf("\n");
		printf("Case %d: %d\n",tmp-t,countt);
		flag=false;
	}
} 
时间: 2024-10-13 14:42:29

sicily 1028. Hanoi Tower Sequence的相关文章

1028. Hanoi Tower Sequence

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

3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? 无外链        相关测试数据下载  链接? 无数

Hanoi Tower问题的求解

文章前部分为转载,转自http://www.cnblogs.com/yanlingyin/ 当然.这是一个经典的递归问题~   想必来看这篇博文的同学对汉诺塔应该不会陌生了吧, 写这篇博还是有初衷的: 之前学数据结构的时候自己看书.也上网上查了很多资料,资料都比较散.而且描述的不是很清楚,对于当时刚刚 接触算法的我,要完全理解还是有一定难度.今天刚好有时间就整理了下思路.重写分析了一下之前的疑惑的地方. 没有透彻的地方便都豁然开朗了.所以迫不及待把我的想法记录下来,和大家分享. 如果你也是和之前

汉诺塔 Hanoi Tower

一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上穿好了由大到小的64片金片,这就是所谓的汉诺塔(Hanoi Tower).不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面. 僧侣们预言,当所有的金片从梵天穿好的金片上移到另一根针上时,世界末日就会来临,而梵塔.寺庙和众生也会随之灭亡...... 故事不多说了,汉诺塔是递归思想的典型应用,上代码: 1 #i

ZOJ-1239 Hanoi Tower Troubles Again!

链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with

Codeforces Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis

HDU 1329 Hanoi Tower Troubles Again!

Hanoi Tower Troubles Again! Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 132964-bit integer IO format: %I64d      Java class name: Main People stopped moving discs from peg to peg after they know the numbe

zoj 2954 Hanoi Tower

Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs (peg 1, peg 2 and peg 3) and N disks of different radii. Initially all disks are located on the first pe

ZOJ 2954 Hanoi Tower(模拟啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1953 You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs (peg 1, peg 2 and peg 3) and N disks of different radii. Initially all disks are located on