51 Nod 1242 矩阵快速幂求斐波那契数列
#include<bits/stdc++.h>
#define mod 1000000009
using namespace std;
typedef long long ll;
typedef long long LL;
struct Mat
{
LL mat[3][3];
Mat()
{
memset(mat,0,sizeof(mat));
}
LL* operator [](int x) //注意这种写法
{
return mat[x];
}
} A;
Mat Mut(Mat a,Mat b)
{
Mat c;
for(int k=0; k<3; k++)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
c[i][j]+=a[i][k]*b[k][j]%mod;
c[i][j]=c[i][j]%mod;
}
return c;
}
Mat Qpow(Mat a,LL n)
{
Mat c;
for(int i=0; i<3; ++i)
c[i][i]=1;
for(; n; n>>=1)
{
if(n&1) c=Mut(c,a);
a=Mut(a,a);
}
return c;
}
ll hh[3][3]={{1,1,0},{0,1,1},{0,1,0}};
int main()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
A.mat[i][j]=hh[i][j];
ll n;
cin>>n;
if(n==0){cout<<0<<endl;return 0;}
if(n==1){cout<<1<<endl;return 0;}
if(n==2){cout<<1<<endl;return 0;}
if(n==3){cout<<2<<endl;return 0;}
A=Qpow(A,n-3);
ll ans=((A.mat[0][0]*2%mod+A.mat[0][1])%mod+A.mat[0][2])%mod;
cout<<ans<<endl;
return 0;
}
51 Nod 1242 矩阵快速幂求斐波那契数列的更多相关文章
- poj3070矩阵快速幂求斐波那契数列
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 9368 Desc ...
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- 矩阵快速幂--51nod-1242斐波那契数列的第N项
斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...
- UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...
- 矩阵快速幂 求斐波那契第N项
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...
- python 快速幂求斐波那契数列
先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- POJ 3070 - 快速矩阵幂求斐波纳契数列
这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...
随机推荐
- SpringDataRedis配置
1.父pom.xml配置依赖包 <dependencyManagement> <dependencies> <dependency> <groupId> ...
- OpenTSDB在HBase中的底层数据结构设计
0.时序数据库 时间序列(Time Series):是一组按照时间发生先后顺序进行排列的数据点序列,通常一组时间序列的时间间隔为一恒定值(如1秒,5分钟,1小时等). 时间序列数据可被简称为时序数据. ...
- MySQL日期时间函数大全
1.获取当前时间+日期 函数now() mysql> select now(); +---------------------+ | now() | +--------------------- ...
- 协程+IO切换+小爬虫
from gevent import monkeymonkey.patch_all() import geventimport requests def f1(url): print(f'GET:{u ...
- js、jQuery各种高度
height.js ```$(document).height(); //整个网页的高度 $(window).height(); //浏览器可视窗口的高度 $(window).scrollTop(); ...
- Java8 parallelStream浅析
JAVA8中引入了lamda表达式和Stream接口.其丰富的API及强大的表达能力极大的简化代码,提升了效率,同时还通过parallelStream提供并发操作的支持,本文探讨parallelStr ...
- 使用jdbc操作ClickHouse
使用jdbc操作ClickHouse 2018年07月01日 01:33:00 狮子头儿 阅读数 10501 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...
- (转)Redis持久化的几种方式
radis持久化的几种方式 1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服 ...
- loj 6043「雅礼集训 2017 Day7」蛐蛐国的修墙方案
loj 爆搜? 爆搜! 先分析一下,因为我们给出的是一个排列,然后让\(i\)给\(p_i\)连边,那么我们一定会得到若干个环,最后要使得所有点度数为1,也就是这些环有完备匹配,那么最后一定全是偶环. ...
- 怎么定义vue-router的动态路由?怎么获取传过来的动态参数?
在router目录下的index.js文件中,对path属性加上/:id. 使用router对象的params.id 例如 : this.$route.params.id