这个题有循环节,可以不用这么做,这个可以当一个模板

#include <iostream>
#include <cstdio> using namespace std;
typedef long long ll;
struct matrix{
int r,c;ll m[5][5];
};
matrix A,B,C,D;
int n;
void init(){
A.m[1][1]=3;A.m[1][2]=1;
B.m[1][1]=1;B.m[1][2]=1;
B.m[2][1]=-1;B.m[2][2]=0;
A.r=1;A.c=2;
B.r=2;B.c=2;
}
void multyply(matrix &A,matrix B){
int i,j,k;
if(A.c!=B.r) printf("err\n");
for(i=1;i<=A.r;++i){
for(j=1;j<=B.c;++j){
C.m[i][j]=0;
for(k=1;k<=A.c;++k){
C.m[i][j]+=A.m[i][k]*B.m[k][j];
}
}
}
A.c=B.c;
for(i=1;i<=A.r;++i){
for(j=1;j<=B.c;++j){
A.m[i][j]=C.m[i][j];
}
}
}
void fast_pow(ll b){
if(b==0) return;
D.m[1][1]=1;D.m[1][2]=0;
D.m[2][1]=0;D.m[2][2]=1;
D.r=2;D.c=2;
while(b){
if(b&1){
multyply(D,B);
}
multyply(B,B);
b>>=1;
}
multyply(A,D);
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
if(n==1){
printf("%d\n",1);
}
else if(n==2){
printf("%d\n",4);
}
else{
if(n-1==2){
printf("%d\n",6);
}
else{
fast_pow(n-3);
printf("%I64d\n",3+A.m[1][1]);
}
}
}
return 0;
}

最新文章

  1. iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)
  2. 搜索引擎关键词劫持之php篇(源码与分析)
  3. 【BZOJ 4456】【UOJ #184】【ZJOI 2016】旅行者
  4. Vue.js进阶
  5. JS深入了解闭包
  6. ArrayList调用remove方法需要注意的地方
  7. winston 日志管理4
  8. 函数查询(Function Query)
  9. tools安装
  10. Ios学习
  11. 素数判定 AC 杭电
  12. ecshop 微信支付插件
  13. cocos2d-x 3.0 使用最新物理引擎的一个源代码实例
  14. CSS3秘笈:第六章
  15. `define、parameter、localparam三者的区别
  16. UnityShader-菲涅尔反射(Fresnel Reflection)
  17. Python中布尔值是False的所有值
  18. Windows 运行命令大全,装逼必备哦!
  19. VS2015右键集成TortoiseGit
  20. project 2013 设置工期为1个工作日,但开始时间与结束时间不是同一天

热门文章

  1. [Cerc2005]Knights of the Round Table
  2. 什么是Etcd,如何运维Etcd ?
  3. 离线安装docker-ce
  4. 全栈性能测试修炼宝典-JMeter实战笔记(一)
  5. Netty之JAVA BIO模型
  6. 3、剑指offer-数组——数组中重复的数字
  7. spring restTemplate 进行http请求的工具类封装
  8. centos7+python3+selenium+chrome
  9. qbxt 学习笔记 10.2
  10. Redis 实战 —— 11. 实现简单的社交网站