基准时间限制: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的结果。
运用矩阵乘法去做,有矩阵,可以矩阵快速幂求出转移矩阵即可得到结果。

#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 ;
}
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
在2*N的一个长方形方格中,用一个1*2的骨牌排满方格。

 
问有多少种不同的排列方法。
 
例如:2 * 3的方格,共有3种不同的排法。(由于方案的数量巨大,只输出 Mod 10^9 + 7 的结果)
Input
输入N(N <= 1000)
Output
输出数量 Mod 10^9 + 7
Input示例
3
Output示例
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 ;
}
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
给出A,B和N,求f(n)的值。
 
Input
输入3个数:A,B,N。数字之间用空格分割。(-10000 <= A, B <= 10000, 1 <= N <= 10^9)
Output
输出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的更多相关文章

  1. 51Nod——T 1242 斐波那契数列的第N项

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 基准时间限制:1 秒 空间限制:131072 KB 分值: 0  ...

  2. (矩阵快速幂)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, ...

  3. 1242 斐波那契数列的第N项

    1242 斐波那契数列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F( ...

  4. 斐波那契数列的第N项

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 题目: 斐波那契数列的定义如下:   F(0) = 0 ...

  5. C#求斐波那契数列第30项的值(递归和非递归)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)

    对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...

  7. js中的斐波那契数列法

    //斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...

  8. 剑指Offer面试题:8.斐波那契数列

    一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...

  9. 算法: 斐波那契数列C/C++实现

    斐波那契数列: 1,1,2,3,5,8,13,21,34,....     //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...

随机推荐

  1. 网页调用外部APP

    <activity android:name=".MainActivity" android:label="@string/app_name"> & ...

  2. Linux filesystem structures.

    1. / – Root Every single file and directory starts from the root directory. Only root user has write ...

  3. (转帖) 如何將值delay n個clock? (SOC) (Verilog)

    来源:http://www.cnblogs.com/oomusou/archive/2009/06/15/verilog_dly_n_clk.html /* (C) OOMusou 2009 http ...

  4. struts2与spring整合问题,访问struts2链接时,spring会负责创建Action

    每次访问一次链接,spring会创建一个对象,并将链接所带的参数注入到Action的变量中(如何做到的呐) 因为: struts2的action每次访问都重新创建一个对象,那spring的ioc是怎么 ...

  5. 小试牛刀-嘿嘿,创建job了

    今天 周六,我写了这个存储过程.用意:检查 数据库中是否有 该类的 job,如果有那么取job_name  赋值给 job_old,    把job_old加上时间戳 改造成 job_new.那么jo ...

  6. java学习——IO流

    字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表.获取对应的文字.在对这个文字进行操作.简单说:字节流+编码表 ---------------------------- ...

  7. 爬虫爬oj用户头像

    import requests import Queue import urllib import urllib2 import re import requests alreadyImg = set ...

  8. C++学习之DLL注入

    #include<stdio.h> #include<Windows.h> #include<TlHelp32.h> //typedef unsigned long ...

  9. POJ 2438 (哈密顿回路)

    分析: 2*n个小朋友,每个最多有n-1个"敌人",显然是存在哈密顿回路的. 预处理边,然后找哈密顿回路. code #include <iostream> #incl ...

  10. NoSQL数据库技术特性解析之文档数据库

    现今云计算的从业人员对NoSQL一词并不感到陌生,虽然很多技术人员都长期从事关系数据库的工作,但现在他们对NoSQL技术充满期待.对于企业来说,从关系型数据库到NoSQL数据库转变绝对是个需要深思熟虑 ...