51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
斐波那契数列的定义如下:
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) (n >= 2)
(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)
给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可。
Input
输入1个数n(1 <= n <= 10^18)。
Output
输出F(n) % 1000000009的结果。
Input示例
11
Output示例
89
#include<iostream>
#include<algorithm>
#include<string.h>
#define inf 1000000009
#define ll long long
using namespace std;
struct node{
ll t[2][2];
node(){
memset(t,0,sizeof(t));
}
node operator*(node p){
node c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.t[i][j]=(c.t[i][j]%inf+t[i][k]*p.t[k][j]%inf)%inf;
return c;
}
};
node pow(ll n,node a){
node b;
b.t[0][0]=b.t[1][1]=1;//切记:对角线上一定要为1
while(n){
if(n&1)
b=a*b;
a=a*a;
n>>=1;
}
return b;
}
int main()
{
ll n;
scanf("%lld",&n);
node a;
a.t[0][0]=a.t[0][1]=a.t[1][0]=1;
if(n==0||n==1)
printf("%lld\n",n);
else{
ll d=pow(n-1,a).t[0][0];
printf("%lld\n",d%inf);
}
return 0;
}
51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)的更多相关文章
- HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 1242 斐波那契数列的第N项
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F( ...
- 51Nod——T 1242 斐波那契数列的第N项
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 ...
- (矩阵快速幂)51NOD 1242斐波那契数列的第N项
斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, ...
- HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂
MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i ) ( i>=3) mod 1000000007 是质数 , 依据费马小定理 a^phi( p ) = 1 ( ...
- 51nod 1242 斐波那契数列的第N项
之前一直没敢做矩阵一类的题目 其实还好吧 推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html 但是后面的斐波那契 推导不是很懂 前面讲的挺 ...
- 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; ; ...
- 51nod 1242 斐波那契数列的第N项——数学、矩阵快速幂
普通算法肯定T了,所以怎么算呢?和矩阵有啥关系呢? 打数学符号太费时,就手写了: 所以求Fib(n)就是求矩阵 | 1 1 |n-1 第一行第一列的元素. | 1 0 | 其实学过线代 ...
- Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...
随机推荐
- Linux安装JDK(tar)
我以JDK1.8为例 ⒈下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- [BugBounty] Sleeping stored Google XSS Awakens a $5000 Bounty
来源:https://blog.it-securityguard.com/bugbounty-sleeping-stored-google-xss-awakens-a-5000-bounty/ 理解 ...
- Pytorch tutorial 之Datar Loading and Processing (2)
上文介绍了数据读取.数据转换.批量处理等等.了解到在PyTorch中,数据加载主要有两种方式: 1. 自定义的数据集对象.数据集对象被抽象为Dataset类,实现自定义的数据集需要继承Dataset. ...
- Python运维开发基础03-语法基础 【转】
上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现“用户登陆 ...
- aiohttp的笔记之TCPConnector
TCPConnector维持链接池,限制并行连接的总量,当池满了,有请求退出再加入新请求.默认是100,limit=0的时候是无限制 1.use_dns_cache: 使用内部DNS映射缓存用以查询D ...
- 后台拼接json字符串,传到前台时注意特殊符号处理
1.后台拼接常用的形式: sb.AppendFormat("\"字段名\":\"{0}\"", i + 1); 这个要注意字符串截断 ...
- curl的http上传文件代码
int http_post_file(const char *url, const char *user, const char *pwd, const char *filename){ ass ...
- u3d常用代码小集合
01.基本碰撞检测代码 function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.name == ...
- Ubuntu16下apache2安装ssl阿里云证书
1.用下面的命令确保ssl模块已经加载进apache: a2enmod ssl 如果你看到了“Module ssl already enabled”这样的信息就说明你成功了,如果你看到了“Enabli ...
- selenium之css定位小结
前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...