Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2.

Input

a single line containing n (where 0 ≤ n ≤ 100,000,000,000)

Output

print Fn mod 1000000007 in a single line.

Sample Input

99999999999

Sample Output

669753982

Hint

An alternative formula for the Fibonacci sequence is

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

Source

Unknown
思路:一开始想着要暴力的办法,或者用python,但还是tle了,然后发现这玩意儿可以用矩阵快速幂,就来一波骚操作了。
#include<bits/stdc++.h>
using namespace std;#define ll long long
#define eps 1e-9
#define pi acos(-1)const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 1000 + 8;ll n;struct matrix
{ll m[2][2];
}b, tp, res, init;matrix mul(matrix a, matrix b)
{matrix c;for(int i = 0; i < 2; i++){for(int j = 0; j < 2; j++){c.m[i][j] = 0;for(int k = 0; k < 2; k++){c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;c.m[i][j] %= mod;}}}return c;
}matrix matrix_mi(matrix p, ll k)
{matrix t = res;while(k){if(k & 1)t = mul(t, p);k >>= 1;p = mul(p, p);}return t;
}int main()
{std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++){if(i == 1 && j == 1)init.m[i][j] = 0;elseinit.m[i][j] = 1;}cin >> n;b = init;for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++)if(i == j)res.m[i][j] = 1;elseres.m[i][j] = 0;tp = matrix_mi(b, n);cout << tp.m[0][1] <<'\n';return 0;
}

转载于:https://www.cnblogs.com/RootVount/p/11469372.html

SDNU 1062.Fibonacci(矩阵快速幂)相关推荐

  1. poj3070 Fibonacci 矩阵快速幂

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18084   Accepted: 12572 Descr ...

  2. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

  3. HDU 3306 Another kind of Fibonacci 矩阵快速幂

    题目链接 因为S(N) , S(N) = A(0)^2 +A(1)^2+--+A(n)^2.所以构造的矩阵一定要维护A(n)^2 s[n-1]=s[n-2]+A[n-1]^2 A[n]=x*A[n-1 ...

  4. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  5. POJ3070 Fibonacci(矩阵快速幂)

    用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...

  6. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...

  7. (矩阵快速幂)解所有类似Fibonacci 的题目

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  8. POJ3070 Fibonacci【矩阵快速幂】

    Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...

  9. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

最新文章

  1. 新手零基础学习Python第一步,搭建开发环境!
  2. 牛客第四次多校Maximum Mode
  3. 【springboot】模板路径、静态资源路径、WebRoot的本地路径
  4. 通过纯css实现图片居中的多种实现方式
  5. arduino 休眠 节能_Arduino低功耗掉电模式看门狗唤醒
  6. 百度视觉技术部人脸检测方向招聘实习生~北京
  7. Vue指令_常用vue指令_自定义全局指令_自定义局部指令---vue工作笔记0016
  8. 怎样看win10是不是永久激活的?
  9. Logistic Regression 的简单推导
  10. bzoj 2440: [中山市选2011]完全平方数(二分+莫比乌斯函数)
  11. bat 取得服务列表_临汾进出口经营者备案,查看详情_共勤外贸服务
  12. 【PCIe 协议】听说你做 PCIe 很多年,还不知道 PCIe Hierarchy ID 是什么 ???
  13. r2游戏服务器网站,神秘揭晓《R2》公测服务器名称首度公布
  14. 1万条数据大概占多大空间_国漫丨2019年上半年漫画数据报告
  15. 想学IT的必看!不断提升自己创造溢价的能力,附带学习经验
  16. env: bash\r: No such file or directory
  17. 计算机学院考勤管理办法,学生考勤管理规定
  18. 知晓当前是在哪一个 Activity Kotlin.Android
  19. 微信小程序背景图片真机不显示问题
  20. php 神经网络,神经网络算法基础入门

热门文章

  1. 解决zabbix的cannot allocate shared memory of size错误
  2. 洛谷P2016战略游戏
  3. C.【转】C语言字符串与数字相互转换
  4. nginx+ssl+pm2 部署 nodejs 服务
  5. 利用Access-Control-Allow-Origin响应头解决跨域请求
  6. 幸福的2016-----年终总结
  7. JVM学习笔记:Java运行时数据区域
  8. Arcgis for Javascript实现两个地图的联动
  9. FAILED BINDER TRANSACTION
  10. 浮动5-常用列表显示(案例)