博客
关于我
sdnu1085.爬楼梯再加强版(矩阵快速幂)
阅读量:273 次
发布时间:2019-03-01

本文共 1337 字,大约阅读时间需要 4 分钟。

Description

WZ是个蛋痛的人,总是喜欢琢磨蛋痛的事,比如他最近想知道上楼梯总共有多少种方式。已知他一步可以迈一阶、两阶或者三阶,现在给你楼梯的阶数,让你计算总共有多少种方式。

Input

输入有多组数据,每组数据占一行,表示楼梯的阶数。(1<=N<=100,000,000,000)

Output

对于每组数据,输出一行,表示上楼方式的总数 % 1000000007。

Sample Input

12

Sample Output

12

矩阵快速幂 

#include
using namespace std;typedef long long ll;const int N=3;const int MOD=1000000007;struct mat{ ll a[N][N];};mat mat_mul(mat x,mat y){ mat res; memset(res.a,0,sizeof(res.a)); for(int i=0; i<3; i++) for(int j=0; j<3; j++) for(int k=0; k<3; k++) res.a[i][j]=(res.a[i][j]+(x.a[i][k]*y.a[k][j])%MOD)%MOD; return res;}long long mat_pow(ll n){ mat c,res; memset(c.a,0,sizeof(c.a)); c.a[0][0]=c.a[0][1]=c.a[0][2]=1; c.a[1][0]=1; c.a[2][1]=1; memset(res.a,0,sizeof(res.a)); for(int i=0; i<3; i++) res.a[i][i]=1; while(n) { if(n&1) res=mat_mul(res,c); c=mat_mul(c,c); n=n>>1; } return ((4*res.a[0][0])%MOD+(2*res.a[0][1])%MOD+res.a[0][2]%MOD)%MOD;///4 2 1分别是前三项}int main(){ long long n; while(scanf("%lld",&n)!=EOF) { if(n==1) cout<<1<<'\n'; else if(n==2) cout<<2<<'\n'; else if(n==3) cout<<4<<'\n'; else { long long ans=mat_pow(n-3); cout<
<<'\n'; } } return 0;}

 

转载地址:http://usio.baihongyu.com/

你可能感兴趣的文章
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>