矩阵快速幂模版

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
const int MOD = (int) 1e9+7;
struct Matrix {
static const int MAXN = 3;
ll num[MAXN][MAXN], col, row;
void clear() {
memset(num, 0, sizeof(num));
col = row = 0;
}
void unit() {
col = row = 3;
for(int i = 0; i < MAXN; i++) num[i][i] = 1;
}
void build(){
col = row = 3;
num[1][0] = num[2][1] = num[0][2] = num[2][2] = 1;
}
};
Matrix operator * (const Matrix & a, const Matrix & b){
Matrix res;
res.clear();
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
(res.num[i][j] += a.num[i][k] * b.num[k][j]) %= MOD;
}
}
}
return res;
}
Matrix operator ^ (Matrix a, ll k) {
Matrix res;
res.clear(); res.unit();
while(k) {
if(k & 1ll) res = res * a;
a = a * a;
k >>= 1;
}
return res;
}
ll T, n;
int main() {
cin>>T;
while(T--) {
cin>>n;
if(n <= 3) {cout<<1<<endl;continue;}
n -= 3;
Matrix a;
a.clear(); a.build();
a = a ^ n;
ll ans = 0;
for(int i = 0; i < 3; i++) ans += a.num[i][2];
ans %= MOD;
cout<<ans<<endl;
}
return 0;
}

洛谷 [P1939] 矩阵加速数列的更多相关文章

  1. 洛谷 P1939 矩阵加速(数列)

    题意简述 \(a[1]=a[2]=a[3]=1\) \(a[x]=a[x−3]+a[x−1](x>3)\) 求a数列的第n项对1000000007取余的值. 题解思路 矩阵加速 设\[ F=\b ...

  2. 洛谷 P1939 【模板】矩阵加速(数列) 解题报告

    P1939 [模板]矩阵加速(数列) 题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值 ...

  3. [洛谷P1939]【模板】矩阵加速(数列)

    题目大意:给你一个数列a,规定$a[1]=a[2]=a[3]=1$,$a[i]=a[i-1]+a[i-3](i>3)$求$a[n]\ mod\ 10^9+7$的值. 解题思路:这题看似是很简单的 ...

  4. 【洛谷P1939】 矩阵加速模板

    https://www.luogu.org/problemnew/show/P1939 矩阵快速幂 斐波那契数列 首先看一下斐波那契数列的矩阵快速幂求法: 有一个矩阵1*2的矩阵|f[n-2],f[n ...

  5. 洛谷.2042.[NOI2005]维护数列(Splay)

    题目链接 2017.12.24 第一次写: 时间: 2316ms (1268ms) 空间: 19.42MB (19.5MB)(O2) 注:洛谷测的时间浮动比较大 /* 插入一段数:将这些数先单独建一棵 ...

  6. 洛谷 P1939 【模板】矩阵加速(数列)

    题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值. 输入输出格式 输入格式: 第一行一 ...

  7. 洛谷P1939【模板】矩阵加速(数列)+矩阵快速幂

    思路: 这个 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 可以想成: [a(n) ] [1 0 1] [a(n-1)   ] [a(n-1) ] =    ...

  8. 洛谷P1473 零的数列 Zero Sum

    P1473 零的数列 Zero Sum 134通过 170提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 路过的一定帮我看错了我死了- 题目描述 请考虑 ...

  9. 洛谷 P1005 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

随机推荐

  1. spring 中bean学习笔记

    spring 中bean 一.bean的定义和应用 1. bean 形象上类似于getXX()和setXX()的一种. 2. 由于java是面向对象的,类的方法和属性在使用中需要实例化. 3. 规律: ...

  2. CentOS 7.0关闭防火墙

    .关闭firewall: systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止fir ...

  3. 引入了junit为什么还是用不了@Test注解

    pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...

  4. jQuery筛选器及练习

    jQuery初识   jQuery是什么? jQuery是一个兼容多浏览器的JavaScript库. jQuery能极大地简化JavaScript编程,它的宗旨就是:"Write less, ...

  5. Bootstrap 网格系统(Grid System)

    Bootstrap 网格系统(Grid System) Bootstrap提供了一套响应式,移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 什么是 ...

  6. iOS 设计模式

    很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...

  7. __new__.py

    def func(self): print('hello %s' %self.name)def __init__(self,name,age): self.name = name self.age = ...

  8. Linux服务器的弱口令检测及端口扫描

    一.弱口令检测--John the Ripper John the Ripper工具可以帮助我们扫描出系统中密码安全性较低的用户,并将扫描后的结果显示出来. 1.安装John the Ripper: ...

  9. python--MySQL权限管理 数据备份还原

    一 权限管理 mysql最高管理者是root用户, 这个一般掌握在公司DBA手里, 当你想去对数据库进行一些操作的时候,需要DBA授权给你. 1. 对新用户增删改 1. 创建用户 # 要先use my ...

  10. Linux下安装SaltStack

    一.配置yum源和epel源 epel源下载地址:http://pan.baidu.com/s/1o7NJ26u 1.配置yum源 (1)上传操作系统镜像文件来配置yum源,挂载点目录为/yum mk ...