SDNU 1062.Fibonacci(矩阵快速幂)
Description
Input
Output
Sample Input
99999999999
Sample Output
669753982
Hint
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
#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(矩阵快速幂)相关推荐
- poj3070 Fibonacci 矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18084 Accepted: 12572 Descr ...
- POJ 3070 Fibonacci(矩阵快速幂入门、模板)
? 题目链接:http://poj.org/problem?id=3070 ? 这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...
- 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 ...
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- POJ3070 Fibonacci(矩阵快速幂)
用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...
- (矩阵快速幂)解所有类似Fibonacci 的题目
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ3070 Fibonacci【矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...
- 矩阵快速幂+构造方法
与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...
最新文章
- 新手零基础学习Python第一步,搭建开发环境!
- 牛客第四次多校Maximum Mode
- 【springboot】模板路径、静态资源路径、WebRoot的本地路径
- 通过纯css实现图片居中的多种实现方式
- arduino 休眠 节能_Arduino低功耗掉电模式看门狗唤醒
- 百度视觉技术部人脸检测方向招聘实习生~北京
- Vue指令_常用vue指令_自定义全局指令_自定义局部指令---vue工作笔记0016
- 怎样看win10是不是永久激活的?
- Logistic Regression 的简单推导
- bzoj 2440: [中山市选2011]完全平方数(二分+莫比乌斯函数)
- bat 取得服务列表_临汾进出口经营者备案,查看详情_共勤外贸服务
- 【PCIe 协议】听说你做 PCIe 很多年,还不知道 PCIe Hierarchy ID 是什么 ???
- r2游戏服务器网站,神秘揭晓《R2》公测服务器名称首度公布
- 1万条数据大概占多大空间_国漫丨2019年上半年漫画数据报告
- 想学IT的必看!不断提升自己创造溢价的能力,附带学习经验
- env: bash\r: No such file or directory
- 计算机学院考勤管理办法,学生考勤管理规定
- 知晓当前是在哪一个 Activity Kotlin.Android
- 微信小程序背景图片真机不显示问题
- php 神经网络,神经网络算法基础入门