矩阵快速幂 求斐波那契第N项
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std; const int M = 1e9+; struct Matrix {
long long a[][];
Matrix() {
memset(a, , sizeof(a));
}
Matrix operator * (const Matrix y) {
Matrix ans;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
for(int k = ; k <= ; k++)
ans.a[i][j] += a[i][k]*y.a[k][j];
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
ans.a[i][j] %= M;
return ans;
}
void operator = (const Matrix b) {
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
a[i][j] = b.a[i][j];
}
}; int solve(long long x) {
Matrix ans, trs;
ans.a[][] = ans.a[][] = ;
trs.a[][] = trs.a[][] = trs.a[][] = ;
while(x) {
if(x&)
ans = ans*trs;
trs = trs*trs;
x >>= ;
}
return ans.a[][];
} int main() {
int n;
scanf("%d", &n);
cout << solve(n-) << endl;
return ;
}
POJ 3070
矩阵快速幂 求斐波那契第N项的更多相关文章
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- 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 ...
- poj3070矩阵快速幂求斐波那契数列
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 9368 Desc ...
- 51 Nod 1242 矩阵快速幂求斐波那契数列
#include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...
- 矩阵快速幂--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, ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- python 快速幂求斐波那契数列
先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...
- POJ 3070 - 快速矩阵幂求斐波纳契数列
这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
随机推荐
- 冲刺周日 Fighting SunDay
一.SunDay照片 二.项目分工 三.今日份燃尽图 四.项目进展 码云团队协同环境构建完毕 利用Leangoo制作任务分工及生成燃尽图 完成AES加解密部分代码 用代码实现对文件的新建.移动.复制. ...
- [ git ] eclipse如何与git 配合工作。
原文链接http://blog.csdn.NET/yangzhihello/article/details/11003941 呵呵,看看这个吧.先去安装eclipse.然后在现在 egit,应该可以从 ...
- 无法加载模块 TP3.2
报错 3.2的路由功能是针对模块设置的,所以URL中的模块名不能被路由,路由定义也通常是放在模块配置文件中. 'MODULE_DENY_LIST' => array('Common','User ...
- 微信小程序 API 数据缓存
微信小程序 数据缓存 (类似于 cookie) wx.setStorage() 将数据存储在本地缓存中制定的 key 中.会覆盖掉原来该 key 对应的内容,数据存储生命周期跟小程序本身一致,即除用户 ...
- 编译rxtx
https://blog.csdn.net/github_29989383/article/details/51886234 https://cloud.tencent.com/developer/a ...
- leetcode-mid-sorting and searching - 240. Search a 2D Matrix II -NO
mycode time limited def searchMatrix(matrix, target): def deal(data): if not data: return False ro ...
- hashMap与 hashTable , ArrayList与linkedList 的区别(详细)
ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个 ...
- 【ABAP系列】SAP Web Dynpro 技术简介
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP Web Dynpro 技 ...
- finereport连接mysql8.0的解决办法
1.具体连接操作 首先将mysql-connector-java-8.0以上的jar包放到FindReport安装目录下的webapps下的WEB-INF下的lib下. 打开finereport,找到 ...
- (一)VS2015下配置OpenGL
刚开始用OpenGL,一开始不太明白配置库的原理,在VS2015下耗费了大量时间.这里将配置过程做个笔记,以供日后查看.配置过程中,需要下载cmake构建工具以及glew和glfw库. 下载地址为: ...