Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9630   Accepted: 6839

Description

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.

Input

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.

Output

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).

Sample Input

  1. 0
  2. 9
  3. 999999999
  4. 1000000000
  5. -1

Sample Output

  1. 0
  2. 34
  3. 626
  4. 6875

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:

.

求斐波那契序列的公式。

由于该矩阵的特殊结构使得a(n+1)[0][0] = a(n)[0][0]+a(n)[0][1], a(n+1)[0][1] = a(n)[1][1], a(n+1)[1][0] = a(n)[0][1]+a(n)[1][0], a(n+1)[1][1] = a(n)[1][0];

code:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<cmath>
  7. #define M(a,b) memset(a,b,sizeof(a))
  8.  
  9. using namespace std;
  10.  
  11. int n;
  12. struct matrix
  13. {
  14. int a[][];
  15. void init()
  16. {
  17. a[][] = a[][] = a[][] = ;
  18. a[][] = ;
  19. }
  20. };
  21.  
  22. matrix mamul(matrix a,matrix b)
  23. {
  24. matrix c;
  25. for(int i = ;i<;i++)
  26. {
  27. for(int j = ;j<;j++)
  28. {
  29. c.a[i][j] = ;
  30. for(int k = ;k<;k++)
  31. c.a[i][j]+=(a.a[i][k]*b.a[k][j]);
  32. c.a[i][j]%=;
  33. }
  34. }
  35. return c;
  36. }
  37.  
  38. matrix mul(matrix s, int k)
  39. {
  40. matrix ans;
  41. ans.init();
  42. while(k>=)
  43. {
  44. if(k&)
  45. ans = mamul(ans,s);
  46. k = k>>;
  47. s = mamul(s,s);
  48. }
  49. return ans;
  50. }
  51.  
  52. int main()
  53. {
  54. while(scanf("%d",&n)==&n>=)
  55. {
  56. if(n==) puts("");
  57. else
  58. {
  59. matrix ans;
  60. ans.init();
  61. ans = mul(ans,n-);
  62. printf("%d\n",ans.a[][]%);
  63. }
  64. }
  65. return ;
  66. }

下面代码只是测试公式,无法解决取模的问题,因为中间为double型,无法取模:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<cmath>
  7. #define M(a,b) memset(a,b,sizeof(a))
  8.  
  9. using namespace std;
  10.  
  11. double Pow(double a,int n)
  12. {
  13. double ans = ;
  14. while(n>=)
  15. {
  16. if(n&)
  17. ans = a*ans;
  18. n = n>>;
  19. a = a*a;
  20. }
  21. return ans;
  22. }
  23.  
  24. int main()
  25. {
  26. int n;
  27. double a = (sqrt(5.0)+1.0)/;
  28. double b = (-sqrt(5.0)+1.0)/;
  29. double c = (sqrt(5.0))/;
  30. while(scanf("%d",&n)==)
  31. {
  32. int ans = (int)(c*(Pow(a,n)-Pow(b,n)))%;
  33. printf("%d\n",ans);
  34. }
  35. return ;
  36. }

poj3070 (斐波那契,矩阵快速幂)的更多相关文章

  1. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  2. 「GXOI / GZOI2019」逼死强迫症——斐波那契+矩阵快速幂

    题目 [题目描述] ITX351 要铺一条 $2 \times N$ 的路,为此他购买了 $N$ 块 $2 \times 1$ 的方砖.可是其中一块砖在运送的过程中从中间裂开了,变成了两块 $1 \t ...

  3. 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  4. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  5. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  6. POJ3070 斐波那契数列 矩阵快速幂

    题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...

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

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

  8. Fibonacci PKU logn 求斐波那契的快速方法!!!

    矩阵的快速幂 #include<cstdio> using namespace std; struct matrix { ][]; }ans,base; matrix multi( mat ...

  9. CF 316E3 Summer Homework(斐波那契矩阵+线段树)

    题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...

随机推荐

  1. 数据结构算法C语言实现---序言

    期末考试即将到来,打算花两周时间实现书上所有的算法.巩固学习成果(其实之前也没怎么听课......)毕竟考前突击,背背,ppt刷个90+是没多大意义的. 没错,就是下面这本 毕竟书也是借别人的,不抓紧 ...

  2. ubantu eclipe

    sudo tar zxvf '/tmp/eclipse-inst-linux64.tar.gz' -C/usr/lib 4.在终端输入: $ sudo gedit /usr/share/applica ...

  3. Android中定时执行任务的3种实现方法

    在Android开发中,定时执行任务的3种实现方法: 一.采用Handler与线程的sleep(long)方法(不建议使用,java的实现方式)二.采用Handler的postDelayed(Runn ...

  4. JS-制作可伸缩的水平菜单栏

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  5. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  6. sql 中的运算符级别 如and or not

    写了这么多简单的sql,很多东西忘记得差不多了,差点连最基本sql运算符优先级都忘了.平时最常用到and or的优先级都忘了 and的优先级高于or的优先级 举个例子 select * from us ...

  7. centos6.4 搭建svn服务器

    SVN作为新一代代码版本管理工具,有很多优点,管理方便,逻辑明确,安全性高,代码一致性高.SVN数据存储有两种方式,BDB(事务安全表类型)和FSFS(一种不需要数据库的存储系统),为了避免在服务器连 ...

  8. 爬虫4 html输出器 html_outputer.py

    #coding:utf8 __author__ = 'wang' class HtmlOutputer(object): def __init__(self): self.datas = []; de ...

  9. rem是如何实现自适应布局的?

    http://caibaojian.com/web-app-rem.html 使用rem 然后根据媒体查询实现自适应.跟使用JS来自适应适配也是同个道理,不过是js更精确一点.使用媒体查询: html ...

  10. 后台程序员的HTTP缓存

    1.后端程序员只需要关注请求头: if-None-Match //上一次response头中的ETag的值. 响应头: Etag //是URL的Entity Tag,用于标示URL对象是否改变,区分不 ...