列表

详情


NC229489. Four Column Hanoi Tower

描述

Based on the classical problem of tower of Hanoi, there are four rods indexed by A,B,C,D (the only difference between this problem and the classical one) and N disks of various diameters, which can slide onto any rod. The puzzle begins with disks stacked on one rod in order of decreasing size, the smallest on the top, thus approximating a conical shape. The objective of the puzzle is to move the entire stack to the last rod (indexed by D), obeying the following rules:
You need to calculate the minimum number of moves required to solve the problem.

输入描述

The first line is a positive integer , indicating that there are T test data.
Next, there are T lines. Each line has a positive integer
, indicating the number of plates in each of a test data.

输出描述

Each test data outputs a line as a positive integer, that is, the minimum number of steps required to move all N plates from the first column A to the last column D.

示例1

输入:

5
1
2
3
4
5

输出:

1
3
5
9
13

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python2 解法, 执行用时: 70ms, 内存消耗: 4332K, 提交时间: 2022-03-27 20:54:30

a = [0,1]
z=0
y=2
tt=2
for i in range(2,10001):
    a.append(a[i-1]+tt)
    z+=1
    if(z==y):
        tt=tt*2
        y+=1
        z=0
t = input()
for i in range(0,t):
    b = input()
    print a[b]

pypy3 解法, 执行用时: 179ms, 内存消耗: 27684K, 提交时间: 2023-03-30 21:11:37

T = int(input())
f = []
f.append(0)
f.append(1)
cnt=2
p=0
q=2
for i in range(2,10001):
	f.append(f[i-1]+cnt)
	p+=1
	if p==q:
		q+=1
		p=0
		cnt*=2
for i in range(T):
	x = int(input())
	print(f[x])

Python3 解法, 执行用时: 115ms, 内存消耗: 7064K, 提交时间: 2021-11-13 15:12:47

import math

T = int(input())
for i in range(T):
    n = int(input())
    t = int(round(math.sqrt(2 * n)))
    print(int(1 - (2 ** (t - 2)) * (t * t - 3 * t - 2 * n + 4)))

上一题