Counting Towers

题目描述

Your task is to build a tower whose width is 2 and height is n. You have an unlimited supply of blocks whose width and height are integers.

For example, here are some possible solutions for n=6:

Given n, how many different towers can you build? Mirrored and rotated towers are counted separately if they look different.

输入

The first input line contains an integer t: the number of tests.

After this, there are t lines, and each line contains an integer n: the height of the tower.

Constraints

1 ≤ t ≤ 100

1 ≤ n ≤

输出

For each test, print the number of towers modulo

样例输入
复制代码
3
2
6
1337
样例输出
复制代码
8
2864
640403945

题目大意: 建立一个宽为2,高为n的塔,有无限的宽度高度都为整数的方块,问最终可以构建出多少种塔,最终的方法数要模

**方法:**很明显的dp题,关键就是状态转移方程怎么想,由于塔的宽度仅为2,所以我们分析塔的一行,可以发现有两种情况,一个是塔的一行是同一个方块,一个则是来自不同方块,我们分别将这两种类型记作a和b,最终的方法数就是an+bn,接下来分析an和bn的计算:

an:an=2*an-1+bn-1

bn:

bn=an-1+4*bn-1

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1000000007;
const int N=1000000;
ll a[N+10],b[N+10];
int main(){
    int t;cin>>t;
    a[1]=1,b[1]=1;
    for (int i=2;i<=N;i++){
        a[i]=(2*a[i-1]+b[i-1])%mod;
        b[i]=(a[i-1]+4*b[i-1])%mod;
    }
    while(t--){
        int n;cin>>n;
        cout<<(a[n]+b[n])%mod<<"\n";
    }
    return 0;
}
相关推荐
tkevinjd1 小时前
力扣322-零钱兑换
算法·leetcode·动态规划
大鱼>1 小时前
AI+资产监控:农业设施智能监控系统
人工智能·深度学习·算法·机器学习
AI科技星2 小时前
乖乖数学·全域超复数统一场论:五大核心门槛与全套标准定量数据
人工智能·python·算法·金融·全域数学
Frostnova丶2 小时前
(13)LeetCode 53. 最大子数组和
算法·leetcode·职场和发展
什巳2 小时前
JAVA练习277- 找到字符串中所有字母异位
java·开发语言·算法·leetcode
机器学习之心2 小时前
基于遗传粒子群混合算法的无人机三维路径规划:Matlab 实现与多算法对比分析
算法·matlab·无人机·无人机三维路径规划·遗传粒子群混合算法
chh5632 小时前
C++--vector
开发语言·c++·学习·算法
本王是暴君2 小时前
AutoMem:把 Agent Memory 变成可训练的记忆管理技能
人工智能·算法·数据挖掘
zmzb01032 小时前
C++课后习题训练记录Day154
数据结构·c++·算法
木木子228 小时前
# 密码强度检测深度解析:正则表达式实时分析、多维度评分算法与可视化反馈
mysql·算法·华为·正则表达式·harmonyos