描述

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

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:

.

 
输入
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
输出
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
样例输入
  1. 0
  2. 9
  3. 1000000000
  4. -1
样例输出
  1. 0
  2. 34
  3. 6875
  4.  
  5. 【题意】

斐波那契数列可以用矩阵来求

当求第非常大的一个斐波那契数的后几位时我们可以用矩阵快速幂求解了。

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<vector>
  4. #include<string.h>
  5. using namespace std;
  6. typedef vector<int>vec;
  7. typedef vector<vec>mat;
  8. const int N=;
  9. mat mul(mat a,mat b)//求两个矩阵的乘积
  10. {
  11. mat c(a.size(),vec(b[].size()));
  12. for(int i=;i<a.size();i++)
  13. {
  14. for(int k=;k<b.size();k++)
  15. {
  16. for(int j=;j<b[].size();j++)
  17. {
  18. c[i][j]=(c[i][j]+a[i][k]*b[k][j])%N;
  19. }
  20. }
  21. }
  22. return c;
  23. }
  24. mat get_ans(mat a,int n)//矩阵的快速幂
  25. {
  26. mat b(a.size(),vec(a.size()));
  27. for(int i=;i<a.size();i++)
  28. {
  29. b[i][i]=;
  30. }
  31. while(n>)
  32. {
  33. if(n&) b=mul(b,a);
  34. a=mul(a,a);
  35. n>>=;
  36. }
  37. return b;
  38. }
  39. int main()
  40. {
  41. long long int n;
  42. while(~scanf("%lld",&n),n>=)
  43. {
  44. if(n==-) break;
  45. mat a(,vec());
  46. a[][]=,a[][]=;
  47. a[][]=,a[][]=;
  48. a=get_ans(a,n);
  49. printf("%d\n",a[][]);
  50. }
  51. return ;
  52. }

fibonacci数列(二)_矩阵快速幂的更多相关文章

  1. nyoj_148_fibonacci数列(二)_矩阵快速幂

    fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 In the Fibonacci integer sequence, F0 = 0, F ...

  2. HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  4. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  5. bzoj5118 Fib数列2 二次剩余+矩阵快速幂

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5118 题解 这个题一看就是不可做的样子. 求斐波那契数列的第 \(n\) 项,\(n \leq ...

  6. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  7. hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)

    题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...

  8. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...

  9. BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂

    http://www.lydsy.com/JudgeOnline/problem.php?id=3231   和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...

随机推荐

  1. OC 实例变量(instance var)与属性(@property)的关系 isa指针

    实例变量(instance var)与属性(@property)的关系 Objective-C 2.0之后,声明一个@property name自动产生一个实例变量,名为_name,因此省去实例变量和 ...

  2. Java 集合系列 17 TreeSet

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  3. 20145236 《Java程序设计》实验一实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称:Java开发环境的熟悉(Linux + Eclipse) 实 ...

  4. grease monkey setTimeout

    在grease monkey中要使用如下方法进行setTimeout var f = function(){alert(1); setTimeout(f,100); } var inst=setTim ...

  5. 使用 HTML5 canvas 绘制精美的图形

    HTML5 是一个新兴标准,它正在以越来越快的速度替代久经考验的 HTML4.HTML5 是一个 W3C “工作草案” — 意味着它仍然处于开发阶段 — 它包含丰富的元素和属性,它们都支持现行的 HT ...

  6. AngularJS开发经验(转)

    AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小花招)来让 ...

  7. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  8. bat产生随机数并复制文件及生成文件列表

    有这样一个场景:我需要将同一个文件复制为上千个文件,并且文件名应为随机数.为了简单起见,不想写程序,直接写个BAT来,方便,简单,易用: 1. 搞定用BAT产生32位随机数,存为变量并使用,保存以下代 ...

  9. Octopus系列之接下来的任务

    更新默认国家[已实现] 更新每页显示条数的后台控制[已实现] 更新国家和区域的Ajax的关联[已实现] 更新详情页面的 属性选择 脚本提示[已实现 可以做到和兰亭一样的效果了] 增加优惠方案的设置和批 ...

  10. apache本地网址配置

    1 实现类似于域名访www.a.com问本地的空间,而不是放在apache下的htocs文件夹下,或者是wamp下的www文件下 2 首先修改C盘WINDOWS\system32\drivers\et ...