列表

详情


NC205341. YetAnotherHanoiProblem

描述

One day, Little Gyro saw Onlystar playing the Hanoi Tower and wanted to test him.
The ordinary Hanoi Tower is a game with three pillars A, B and C. Given a tower consisting of n discs, these discs are nested on the pillar A within a decreasing way of size to display. Our goal is to move the entire tower to another pillar C. Only one disc can be moved at a time, and the larger disc cannot be placed under the smaller disc during every movement. Here shows the picture of it:

Then Little Gyro modified the rule of the Hanoi Tower game, supposed that the relative position of three pillars A, B and C is from left to right. Onlystar was asked to move the entire tower consisting of n discs from the leftmost pillar A to the rightmost pillar C, However, he couldn't move any disc directly from pillar A to pillar C. It means that Onlystar can only move discs through the pillar B in the middle.
Now, given the total number of discs, Onlystar wants to know the minimum steps there may be.

输入描述

There are multiple test cases. The first line of the input contains an integer T (1 ≤ T ≤ ), indicating the number of test cases. For each test case:
Each line contains one integer n (1 ≤ n ≤ ), indicating the total number of discs.

输出描述

For each test case, you should output the number of the minimum steps that Onlystar could achieve. 
Because the number may be very large, just output the number mod .

示例1

输入:

3
1
2
3

输出:

2
8
26

原站题解

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

C++14(g++5.4) 解法, 执行用时: 163ms, 内存消耗: 9232K, 提交时间: 2020-09-18 13:23:47

#include <bits/stdc++.h>
using namespace std;
long long sz[1000010];
int main()
{
	int a;
	cin>>a;
	sz[0]=0;
	for(int i=1;i<=1000005;i++)
	{
		sz[i]=((sz[i-1]+1)*3-1)%1000000007;
	}
	while(a--)
	{
		long long k;
		cin>>k;
		cout<<sz[k]<<endl;
	}
}

C++11(clang++ 3.9) 解法, 执行用时: 55ms, 内存消耗: 9324K, 提交时间: 2020-06-03 16:54:00

#include<cstdio>
long long int a[1000005];
int main()
{
	a[0]=1;
	for(int i=1;i<1000005;i++)
	{
		a[i]=a[i-1]*3;
		a[i]=a[i]%(1000000000+7);
	}
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		printf("%d\n",a[n]-1);
	}
}

pypy3(pypy3.6.1) 解法, 执行用时: 1236ms, 内存消耗: 31076K, 提交时间: 2020-06-03 16:06:20

mod = 1_000_000_007
for _ in range(int(input())):
    print((pow(3, int(input()), mod) - 1 + mod) % mod)

Python3(3.5.2) 解法, 执行用时: 1908ms, 内存消耗: 4344K, 提交时间: 2020-07-07 19:46:42

n=eval(input())
for i in range(n):
    t=eval(input())
    print(pow(3, t,1000000007)-1)

上一题