矩阵快速幂模版

#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. openstack安装dashboard后访问horizon出错 End of script output before headers: django.wsgi

    在配置文件中增加如下的一句解决问题 /etc/apache2/conf-available/openstack-dashboard.conf WSGIApplicationGroup %{GLOBAL ...

  2. PCL点云处理可视化——法向显示错误“no override found for vtk actor”解决方法

    转:https://blog.csdn.net/bflong/article/details/79137692 参照:https://blog.csdn.net/imsaws/article/deta ...

  3. mac ssd开启trim模式

    开启方法 sudo trimforce enable

  4. lg、ln的表示方法

    c语言中 函数 log(x) 表示是以e为底的自然对数,即 ln(x) 函数 log10(x) 以10为底的对数,即 lg(x) 以其它数为底的对数用换底公式来表示 log(a)/log(b) 函数 ...

  5. ndarray数组变换

    import numpy as np 维度变换 a = np.arange(24) a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ...

  6. int型除以int型

    int型除以int型得到的还是int型 就算你是这样的:float a = 5/3,虽然你定义的a是float型,但a得到的结果依旧是1.0000而不是1.66666 5/3先得到1,然后再转换成1. ...

  7. rf统计条数

    js模式 直接引用关键字模式

  8. JS中鼠标左右键以及中键的事件

    在三维场景中有时候需要判断鼠标的事件,除了使用的click事件,只有鼠标左键有效,而右键无效.而对于onmousedown.onmouseup的时候鼠标的事件左键/右键有效.详细请看w3c上的资料. ...

  9. shell脚本,通过传入的参数来计算最大值和最小值以及平均值。

    [root@localhost zuoye]# cat quansges.sh #!/bin/bash > file [ ! $# -ge ] && || echo $* > ...

  10. Nginx代理tcp端口实现负载均衡

    Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify {   has ...