题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172

题意:如题。

解法:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
struct Matrix{
LL a[3][3];
void set1(){
memset(a, 0, sizeof(a));
}
void set2(){
memset(a, 0, sizeof(a));
for(int i=0; i<3; i++) a[i][i]=1;
}
void set3()
{
a[0][0]=4,a[0][1]=17,a[0][2]=-12;
a[1][0]=1,a[1][1]=0,a[1][2]=0;
a[2][0]=0,a[2][1]=1,a[2][2]=0;
}
};
Matrix operator * (const Matrix &a, const Matrix &b){
Matrix res;
res.set1();
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]%mod+a.a[i][k]*b.a[k][j]%mod + mod)%mod;
}
}
}
return res;
}
Matrix qsm(Matrix a, LL n){
Matrix res;
res.set2();
while(n){
if(n&1) res = res*a;
a = a*a;
n/=2LL;
}
return res;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
LL n;
scanf("%lld", &n);
if(n==2){
puts("31");
}
else if(n==3){
puts("197");
}
else if(n==4){
puts("1255");
}
else{
Matrix A,B;
A.set3();
B = qsm(A, n-4);
LL ans = (B.a[0][0]*1255%mod+B.a[0][1]*197%mod+B.a[0][2]*31%mod+mod)%mod;
printf("%lld\n", ans%mod);
}
}
return 0;
}

最新文章

  1. 深入理解Spark(一):Spark核心概念RDD
  2. Linux分区和挂载硬盘
  3. MVC之前的那点事儿系列(1):进入CLR
  4. 主从Reactor多线程模型
  5. 有关于break,continue,return的区别和代码分析
  6. Think Python - Chapter 10 - Lists
  7. UVALive 6869(后缀数组)
  8. 基于Vue.js的大型报告页项目实现过程及问题总结(一)
  9. shell脚本实现git和svn统计log代码行
  10. Go 接口(interface)
  11. php 文件读取方式
  12. 任意格式视频转MP4格式
  13. MySQL笔记(4)---表
  14. 传统项目转前端工程化——路由跳转时出现浏览器锁死和白屏【该死的同步ajax】
  15. 上传文件Base64格式(React)
  16. Final发布——视频博客
  17. SpringCloud初体验:七、gateway 网关服务如何做token验证
  18. TeamCity编译执行selenium上传窗口脚本缺陷
  19. maven项目修改项目名
  20. 【Java】Serializable 接口

热门文章

  1. BZOJ [Ctsc2002] Award 颁奖典礼 解题报告
  2. Vue项目搭建过程
  3. 拼接sql语句参数绑定
  4. AES encryption of files (and strings) in java with randomization of IV (initialization vector)
  5. HDU4009:Transfer water(有向图的最小生成树)
  6. springmvc不通过controller进行页面跳转
  7. BigBlueButton简介
  8. RabbitMQ的使用总结
  9. Mysql优化小记1
  10. 基数排序——尚未补完的坑QAQ