P1939【模板】矩阵加速(数列)
P1939【模板】矩阵加速(数列)
难受就难受在a[i-3],这样的话让k=3就好了。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<cstring>
#define mod 1000000007
#define For(i,a,b) for(register long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.26
using namespace std;
long long k,t; struct matrix
{
long long a[];
long long b[][];
matrix operator *(const matrix&c)const
{
matrix r;
For(i,,)
For(j,,)
{
r.b[i][j]=;
For(k,,)
r.b[i][j]=(r.b[i][j]+(b[i][k]*c.b[k][j])%mod)%mod;
}
return r;
}
}a; void in(long long &x)
{
long long y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(long long x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} matrix ksm(matrix a,long long b)
{
matrix r=a;b--;
if(b==)
return r;
while(b%==)
{
a=a*a;
b>>=;
}
while(b>)
{
if(b%==)
r=r*a;
a=a*a;
b>>=;
}
return r;
} int main()
{
in(t);
while(t--)
{
in(k);
if(k==||k==||k==)
{
o(),p('\n'); }
else
{
a.a[]=;
a.a[]=;
a.a[]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
a.b[][]=;
matrix r,ans;
r=ksm(a,k-);
for(register int i=;i<=;i++)
For(j,,)
{
ans.a[i]=;
For(k,,)
ans.a[i]=(ans.a[i]+(a.a[i]*r.b[k][j])%mod)%mod;
}
For(i,,)
o((ans.a[i]%mod+mod)%mod),p('\n');
} }
return ;
}
P1939【模板】矩阵加速(数列)的更多相关文章
- 【洛谷P1939】 矩阵加速模板
https://www.luogu.org/problemnew/show/P1939 矩阵快速幂 斐波那契数列 首先看一下斐波那契数列的矩阵快速幂求法: 有一个矩阵1*2的矩阵|f[n-2],f[n ...
- 洛谷 [P1939] 矩阵加速数列
矩阵快速幂模版 #include <iostream> #include <cstring> #include <cstdlib> #include <alg ...
- Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)
补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...
- 洛谷 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)取余的值 ...
- [洛谷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$的值. 解题思路:这题看似是很简单的 ...
- LuoGu P1939 【模板】矩阵加速(数列)
板子传送门 矩阵快速幂学完当然要去搞一搞矩阵加速啦 (矩阵加速相对于矩阵快速幂来说就是多了一个构造矩阵的过程) 关于怎样来构造矩阵,这位大佬讲的很好呢 构造出矩阵之后,我们再去用矩阵快速幂乘出来,取[ ...
- 【luogu P1939 【模板】矩阵加速(数列)】 题解
题目链接:https://www.luogu.org/problemnew/show/P1939 对于矩阵推序列的式子: 由题意知: f[x+1] =1f[x] + 0f[x-1] + 1f[x-2] ...
- 斐波那契数列F(n)【n超大时的(矩阵加速运算) 模板】
hihocoder #1143 : 骨牌覆盖问题·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个 ...
- 洛谷 P1939 矩阵加速(数列)
题意简述 \(a[1]=a[2]=a[3]=1\) \(a[x]=a[x−3]+a[x−1](x>3)\) 求a数列的第n项对1000000007取余的值. 题解思路 矩阵加速 设\[ F=\b ...
随机推荐
- Linux下搭建gtk+2.0开发环境
安装gtk2.0 sudo apt-get install libgtk2.0-dev 查看 2.x 版本 pkg-config --modversion gtk+-2.0 #有可能需要sudo ap ...
- F - Set of Strings
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concat ...
- proxysql 系列 ~ 总揽概括
一 简介: proxysql相关知识汇总 二 proxysql 相关报错 1 proxysql 报错 too many connections 分析 proxysql关于连接池的参数 ...
- 转:Delphi中使用比较少的一些语法
http://www.cnblogs.com/Murphieston/p/5577836.html 本文是为了加强记忆而写,这里写的大多数内容都是在编程的日常工作中使用频率不高的东西,但是又十分重要. ...
- python - getattr 与 getattribute 机制
#__getattribute__ class Foo(): def __init__(self,name): self.name = name def __getattr__(self, item) ...
- Mask RCNN 学习笔记
下面会介绍基于ResNet50的Mask RCNN网络,其中会涉及到RPN.FPN.ROIAlign以及分类.回归使用的损失函数等 介绍时所采用的MaskRCNN源码(python版本)来源于GitH ...
- 复制vmware主机修改网卡
https://blog.csdn.net/gui951753/article/details/79491092
- ubuntu 14.04 安装 OpenCV -2.4.13
1. 安装 (1) 更新软件源 sudo apt-get update sudo apt-get upgrade (2)删除以前安装的 FFMPEG 和 x264 库: sudo apt-get re ...
- 机器学习编程语言之争,Python夺魁
机器学习编程语言之争,Python夺魁 随着科技的发展,拥有高容量.高速度和多样性的大数据已经成为当今时代的主题词.数据科学领域中所采用的机器学习编程语言大相径庭.究竟哪种语言最适合机器学习成为争论不 ...
- C++ 字符串的编码
转载链接:https://www.cnblogs.com/akb48/p/5439154.html windows平台 char 表示单字符,占用一个字节 wchar_t 表示宽字符,占用两个字节 L ...