斐波那契数列 51nod
输入1个数n(1 <= n <= 10^18)。
输出F(n) % 1000000009的结果。
运用矩阵乘法去做,有矩阵,可以矩阵快速幂求出转移矩阵即可得到结果。

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
int n = ;
struct mat
{
LL a[][];
};
mat mul(mat m1,mat m2)
{
mat ans;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
LL temp = ;
for(int k=;k<n;k++)
{
temp+=m1.a[i][k]*m2.a[k][j];
}
ans.a[i][j] = temp % ;
}
return ans;
}
mat pow(mat m,LL b)
{
if(b<=)
return m;
mat temp = pow(m,b/);
if(b&)
return mul(mul(temp,temp),m);
else
return mul(temp,temp);
}
int main()
{
LL num;
mat beg;
beg.a[][]=beg.a[][]=beg.a[][]=;beg.a[][]=;
cin>>num;
cout<<pow(beg,num-).a[][]<<endl;
return ;
}

输入N(N <= 1000)
输出数量 Mod 10^9 + 7
3
3 显然,N=1时一种方法,N=2时有两种方法。
当N>2,可分为两种情况,1是竖着放,那么方法数目为前n-1个的结果,f(n-1)
2是两个横着放,这样占用了两个格子,方法数目是前n-2个结果 f(n-2)
f(n)=f(n-1)+f(n-2),f(1)=1,f(2)=2;
由上面程序略作修改
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
int n = ;
#define M 1000000007
struct mat
{
LL a[][];
};
mat mul(mat m1,mat m2)
{
mat ans;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
LL temp = ;
for(int k=;k<n;k++)
{
temp+=m1.a[i][k]*m2.a[k][j];
}
ans.a[i][j] = temp%M;
}
return ans;
}
mat pow(mat m,LL b)
{
if(b<=)
return m;
mat temp = pow(m,b/);
if(b&)
return mul(mul(temp,temp),m);
else
return mul(temp,temp);
}
int main()
{
LL num;
mat beg;
beg.a[][]=beg.a[][]=beg.a[][]=;beg.a[][]=;
cin>>num;
mat tmp;
tmp.a[][]=,tmp.a[][]=tmp.a[][]=,tmp.a[][]=;
mat r = pow(beg,num-);
mat as=mul(tmp,r);
cout<<as.a[][]<<endl;
return ;
}
输入3个数:A,B,N。数字之间用空格分割。(-10000 <= A, B <= 10000, 1 <= N <= 10^9)
输出f(n)的值。 同样思路用矩阵做,注意避免负数的出现 (ans+7)%7.只需把递归式中系数修改。
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
int n = ;
#define M 1000000007
struct mat
{
LL a[][];
};
mat mul(mat m1,mat m2)
{
mat ans;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
LL temp = ;
for(int k=;k<n;k++)
{
temp+=m1.a[i][k]*m2.a[k][j] ;
}
ans.a[i][j] = (temp+)%;
}
return ans;
}
mat pow(mat m,LL b)
{
if(b<=)
return m;
mat temp = pow(m,b/);
if(b&)
return mul(mul(temp,temp),m);
else
return mul(temp,temp);
}
int main()
{
LL num,t1,t2;
cin>>t1>>t2>>num;
mat beg;
beg.a[][]=t1,beg.a[][]=t2,beg.a[][]=;beg.a[][]=;
mat r = pow(beg,num-);
cout<<(r.a[][]+r.a[][]+)%<<endl;
return ;
}
斐波那契数列 51nod的更多相关文章
- 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, ...
- 1242 斐波那契数列的第N项
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F( ...
- 斐波那契数列的第N项
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 题目: 斐波那契数列的定义如下: F(0) = 0 ...
- C#求斐波那契数列第30项的值(递归和非递归)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)
对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...
- js中的斐波那契数列法
//斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...
- 剑指Offer面试题:8.斐波那契数列
一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...
- 算法: 斐波那契数列C/C++实现
斐波那契数列: 1,1,2,3,5,8,13,21,34,.... //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...
随机推荐
- js 终止页面加载
var xmlhttp = new XMLHttpRequest(); xmlhttp.abort(); window.stop();
- 面试题——分析从输入url到页面返回的过程(或者查询返回过程)
1. You enter a URL into the browser(输入一个url地址) 2.The browser looks up the IP address for the domain ...
- iOS GorupBy
转自: IOS 数组分组 Grouped NSArray 1 2 3 4 5 6 7 8 NSMutableSet *set=[NSMutableSet set]; [_list enumera ...
- MySQL 连接
MySQL 连接 使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@ ...
- CSS 布局Float 【4】
一些浮动模型的基本知识:浮动模型也是一种可视化格式模型,浮动的框可以左右移动(根据float属性值而定),直到它的外边缘碰到包含框 或者另一个浮动元素的框的边缘.浮动元素不在文档的普通流中,文档的普通 ...
- 面向对象设计模式之TemplateMethod模板方法(行为型)
动机:在软件构建过程中,对于某一项任务,他常常有稳定的整体操作结构,但各个子步骤却有很多改变的需求,或者由于固有的原因(比如框架与应用之间的关系)而无法和任务的整体结构同时实现:如何在确定稳定操作结构 ...
- Bootstrap中的 Typeahead 组件
Bootstrap 中的 Typeahead 组件其实就是嵌入到其中的typeahead.js插件,可以完成输入框的自动匹配功能,在通过一些人工的调整基本可以胜任所有的匹配功能和场景,下面介绍下简单的 ...
- Yii2的相关学习记录,alert等美化、confirm异步、session中的flash及小部件的使用(六)
呃,系统自带的alert.confirm等弹出框实在是难看,作为一个颜控,这能忍? 这里我用的是kartik-v/yii2-dialog,这个是基于bootstrap3-dialog这个来做了一些常用 ...
- PHP 运算符 详解
PHP 算数运算符 运算符 名称 例子 结果 + 加法 $x + $y $x 与 $y 求和 - 减法 $x - $y $x 与 $y 的差数 * 乘法 $x * $y $x 与 $y 的乘积 / 除 ...
- python函数参数的pack与unpack
python函数参数的pack与unpack 上周在使用django做开发的时候用到了mixin(关于mixin我还要写一个博客专门讨论一下,现在请参见这里),其中又涉及到了一个关于函数参数打包(pa ...