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的值. [ ...
随机推荐
- JS正则之---HTML版
话不多说 上代码 <html><head> <meta http-equiv="Content-Type" content="text/h ...
- python cx_oracle 环境搭建
背景说明: 之前的环境本来是可以用的,是另外一个项目(python27)需要的时候搭建的.新项目采用的是python36,安装的cx_oracle的版本是7,而环境中的Oracle客户端是11,导致p ...
- Codeforces 1178C. Tiles
传送门 考虑一块块填,首先 $(1,1)$ 有 $4$ 种方案 然后根据 $(1,1)$ 的右边颜色,$(1,2)$ 有两种方案,$(1,3)$ 根据 $(1,2)$ 也有两种方案... 考虑 $(2 ...
- python+minicap(二)
一.push文件至手机中 minicap 的使用有很强的针对性,针对不同架构的CPU和SDK制作了不同的 "minicap" 和 "minicap.so" 文件 ...
- 数据绑定-绑定Servlet内置对象
数据绑定:获取用户提交的参数,绑定到入参的参数中,就叫数据绑定. 绑定Servlet内置对象: 测试:
- JavaScript斑马线表格制作
//实现斑马线表格 //方法1: document.write('<table border="1">'); for(var i=1; i<11; i++){ i ...
- sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrect string value: '\\xE6\\xB1\\x89\\xE8\\xAF\\xAD...' for column 'className' at row 1") [SQL: INSERT INTO classmessage (`classId
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrect string value: '\\xE ...
- 物联网的语言c,python,go等
日本生鱼片 电热水器的使用方法http://www.hiry.cn/b/mt/33959.html 物联网层次很多,首先要看你从事哪个层级的工作了.既然你问语言,那么肯定是开发类的工作,开发类的对象中 ...
- python异步IO编程(二)
python异步IO编程(二) 目录 开门见山 Async IO设计模式 事件循环 asyncio 中的其他顶层函数 开门见山 下面我们用两个简单的例子来让你对异步IO有所了解 import asyn ...
- 89. Gray Code (Java)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...