传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=200

试题描述:

地球人都知道Fibonicca数列:

1 1 2 3 5 8 -----

输入两个正整数L,R,输出Fibonicca数列第L项加到第R项的结果,因为答案可能很大,请输出答案的后7位(不保留前导零)。

输入:

第一行为两个正整数L,R.

输出:

输出答案的后7位(不保留前导零)

输入示例:

1 5

输出示例:

12

其他说明:

60%数据: 1<=L<=R<=1000000
100%数据: 1<=L<=R<=2^31-1
其中20%数据保证L=R

题解:前缀和思想:f(L,R)=f(1,R)-f(1,L),然后变成裸矩阵乘。

                                            [0 , 1 , 0]

[f(n-2),f(n-1),S(n-2)] * [1 , 1 , 1] = [f(n-1),f(n),S(n-1)]

                                            [0 , 0 , 1]

注意n=1,2特判。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=;
struct Matrix{
long long A[][];
Matrix(){memset(A,,sizeof(A));}
};
void print(Matrix M){
for(int i=;i<;i++){
for(int j=;j<;j++) printf("%d ",M.A[i][j]);
puts("");
}puts("");return;
}
Matrix mul(Matrix a,Matrix b){
Matrix ans;
for(int i=;i<;i++)
for(int j=;j<;j++){
for(int k=;k<;k++) ans.A[i][j]+=a.A[i][k]*b.A[k][j];
ans.A[i][j]%=mod;
}
return ans;
}
Matrix pow(Matrix a,int n){
Matrix ans=a,tmp=a;n--;
while(n){
if(n&) ans=mul(ans,tmp);
tmp=mul(tmp,tmp);
n>>=;
} return ans;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-;ch=getchar();}
while(isdigit(ch)) x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;} if(x<) putchar('-'),x=-x;
int len=,buf[]; while(x) buf[len++]=x%,x/=;
for(int i=len-;i>=;i--) putchar(buf[i]+'');return;
}
int initn[][]={
{,,},
{,,},
{,,}
};
int solve(int n){
if(n==) return ;
if(n==) return ;//真坑!
Matrix M;
for(int i=;i<;i++)
for(int j=;j<;j++)
M.A[i][j]=initn[i][j]; M=pow(M,n-);
//puts("here!");print(M);
return (M.A[][]+M.A[][])%mod;
} void init(){
return;
}
void work(){
int a=read(),b=read();
printf("%d\n",(solve(b)-solve(a-)+mod)%mod);
return;
}
void print(){
return;
}
int main(){
init();work();print();return ;
}

COJ 0200 Fibonacci的更多相关文章

  1. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  2. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  3. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  4. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

  5. fibonacci数列(五种)

    自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...

  6. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  7. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...

  8. 算法系列:Fibonacci

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  9. UVa #11582 Colossal Fibonacci Numbers!

    巨大的斐波那契数 The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and  ...

随机推荐

  1. 【LeetCode】Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  2. 一个站点的诞生06-- ORM

    站点上的数据,存在数据库里. 一般用Mysql,也实用sqlite,Postgre.操作数据库要会SQL语言,这个有点麻烦,经常须要查手冊. 此外.每家数据库在实现SQL语言的时候,经常会加料,添加一 ...

  3. JDK5-自动拆装箱

    拆装箱:在基本类型与其对应的引用类型之间转换 装箱:Integer iObj = 5; 拆箱:int i = 5 + iObj; 装箱时,一个字节以内的数据在一个常量池中(小整数的使用频率高),即-1 ...

  4. android中的数据库操作(转)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  5. Android(java)学习笔记236:多媒体之加载大图片到内存(Bitmap API)

    1.Bitmap (API使用) android里面的bitmap中,一个像素点需要4个byte去表示,这是因为android表示颜色是" argb ":其中 a 表示是透明度,然 ...

  6. HUD2087

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 using name ...

  7. Activity对话框

    对话框Activity style 在style.xml中加入 <!--对话框风格--> <style name="dialog" parent="@a ...

  8. C#多线程lock解决数据同步

    1.代码实例: public class ThreadTest4 { public static void Init() { //多个线程修改同一个值,使用lock锁解决并发 ; i < ; i ...

  9. Google2016开发者大会

    Android主讲: 一.吴晶:android笔记博主(博客:http://www.race604.com/) 主题:Android低功耗蓝牙(BLE)实践 低功耗蓝牙在可穿戴和智能家居里边用的比较多 ...

  10. 10个加速Table Views开发的Tips(转)

    本文由CocoaChina译者yake_099(博客)翻译,作者:David McGraw原文:10 Actionable Performance Tips To Speed Up Your Tabl ...