Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7715   Accepted: 5474

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:

.

Source

 
想学高斯消元,先学矩阵。
二分思想,在A^B mod m中已经体现..
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5.  
  6. struct node
  7. {
  8. int a[][];
  9. }now,cur;
  10.  
  11. void make_first()
  12. {
  13. now.a[][]=;
  14. now.a[][]=;
  15. now.a[][]=;
  16. now.a[][]=;
  17.  
  18. cur.a[][]=;
  19. cur.a[][]=;
  20. cur.a[][]=;
  21. cur.a[][]=;
  22. }
  23.  
  24. struct node make_cheng(node a,node b) //发现结构体忘记了。
  25. {
  26. struct node f;
  27. int i,k,j;
  28. memset(f.a,,sizeof(f.a));
  29. for(i=;i<=;i++)
  30. for(k=;k<=;k++)
  31. if(a.a[i][k])
  32. {
  33. for(j=;j<=;j++)
  34. if(b.a[k][j])
  35. {
  36. f.a[i][j]+=a.a[i][k]*b.a[k][j];
  37. if(f.a[i][j]>=)
  38. f.a[i][j]%=;
  39. }
  40. }
  41. return f;
  42. }
  43.  
  44. void make_EF(int n)
  45. {
  46. make_first();
  47. while(n)
  48. {
  49. if(n&)
  50. {
  51. now=make_cheng(now,cur);//!!!
  52. }
  53. n=n/;
  54. cur=make_cheng(cur,cur);
  55. }
  56. printf("%d\n",now.a[][]);
  57. }
  58.  
  59. int main()
  60. {
  61. int n;
  62. while(scanf("%d",&n)>)
  63. {
  64. if(n==-)break;
  65. if(n==){printf("0\n");continue;}
  66. if(n==||n==){printf("1\n");continue;}
  67. make_EF(n-);
  68. }
  69. return ;
  70. }

poj 3070 Fibonacci 矩阵相乘的更多相关文章

  1. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  2. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  3. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. POJ 3070 Fibonacci 矩阵高速求法

    就是Fibonacci的矩阵算法.只是添加一点就是由于数字非常大,所以须要取10000模,计算矩阵的时候取模就能够了. 本题数据不强,只是数值本来就限制整数,故此能够0ms秒了. 以下程序十分清晰了, ...

  5. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  6. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  7. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  8. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  9. 题解报告:poj 3070 Fibonacci

    题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...

随机推荐

  1. Python(模块&包)

    参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html 在linux下给pycharm安装第三方库,需要在.bashrc中加: 因为对应 ...

  2. Linux下的压缩及归档

    Linux下常用的压缩格式有: gz,bz2,xz,zip,Z //只能压缩文件不能压缩目录,如果传递一个目录,他会把目录中的文件逐个压缩 ..压缩算法:算法不同,压缩比也不同 gz:gzip,压缩后 ...

  3. JMeter PerfMon Metrics Collector性能监控插件

    官方文档地址https://jmeter-plugins.org/wiki/PerfMon/ JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况. JMeter正常自带可以通过To ...

  4. docker 运行容器时为容器起别名

    docker run --name=mydemo -p  -d 2222:80 imagename --name: 指定容器名称 -p:指定容器端口号 -d:指定容器后台运行

  5. 升级vue-cli为 cli3 并创建项目

    一.升级npm install -g @vue/cli 二.创建项目 1.vue  create vue3-project 下面会提示让你配置下自己想要用到的功能,然后它会自动帮你安装,这个看自己需求 ...

  6. 查看哪个用户、IP、什么时间登陆过服务器

    2019-01-07 utmpdump /var/log/wtmp 或者 who /var/log/wtmp

  7. Openerp 添加修改报表

    Report Designer 模块在生成新报表的时候是有BUG的不建议直接使用,不过我们也可以通过该插件再写简单的代码来实现新添加报表,插件安装成功后我们可以按照下列方法来添加报表 OpenERP ...

  8. MySQL Migration Tool报“initialized java loader”错误的问题

    MySQL Migration Tool报“initialized java loader”错误的问题   运行MySQL Migration Tool时经常会提示“An error occured ...

  9. java多线程---------java.util.concurrent并发包----------ThreadPoolExecutor

    ThreadPoolExecutor线程池 一.三个构造方法 ThreadPoolExecutor(int corePoolSize,int MaxmumPoolSize,long KeepAlive ...

  10. 各种数据库maven的pom文件编写与ibernate链接配置

    各种数据库Hibernate链接配置 Derby db driver maven dependency <dependency>         <groupId>org.ap ...