Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7752   Accepted: 5501

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

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <stdlib.h>
 6
 7 using namespace std;
 8 int N;
 9 struct matrix
10 {
11        int a[2][2];
12 }origin,res;
13 matrix multiply(matrix x,matrix y)
14 {
15        matrix temp;
16        memset(temp.a,0,sizeof(temp.a));
17        for(int i=0;i<N;i++)
18        {
19                for(int j=0;j<N;j++)
20                {
21                        for(int k=0;k<N;k++)
22                        {
23                                temp.a[i][j]+=x.a[i][k]*y.a[k][j]%10000;
24                                temp.a[i][j]%=10000;
25                        }
26                }
27        }
28        return temp;
29 }
30 void calc(int n)
31 {
32      memset(res.a,0,sizeof(res.a));
33       origin.a[0][0]=1;origin.a[0][1]=1;
34       origin.a[1][0]=1;origin.a[1][1]=0;
35      for(int i=0;i<N;i++)
36      res.a[i][i]=1;
37      while(n)
38      {
39              if(n&1)
40                     res=multiply(res,origin);
41              n>>=1;
42              origin=multiply(origin,origin);
43      }
44 }
45 int main()
46 {
47     N=2;
48     int n;
49     while(cin>>n)
50     {
51         if(n==-1)
52         break;
53         if(n)
54         calc(n-1);
55         if(n)
56         cout<<res.a[0][0]<<endl;
57         else cout<<0<<endl;
58     }
59 }

View Code

转载于:https://www.cnblogs.com/ERKE/p/3272728.html

poj3070矩阵快速幂相关推荐

  1. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  2. POJ3070 矩阵快速幂模板

    题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...

  3. 快速幂、快速乘、矩阵快速幂

    文章目录 一 快速幂 模板 模板例题 二 快速乘 模板 三 矩阵快速幂 例题 模板题 应用题1 应用题2 一 快速幂 目的:当我们在求f(x) = a ^ x % mod时,f(x)的结果会呈指数型增 ...

  4. POJ3070 Fibonacci【矩阵快速幂】

    Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...

  5. poj3070 Fibonacci 矩阵快速幂

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18084   Accepted: 12572 Descr ...

  6. POJ3070 Fibonacci(矩阵快速幂)

    用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...

  7. 一文彻底搞懂快速幂(原理、实现、矩阵快速幂)

    前言 大家好,我是bigsai,之前有个小老弟问到一个剑指offer一道相关快速幂的题,这里梳理一下讲一下快速幂! 快速幂是什么? 顾名思义,快速幂就是快速算底数的n次幂.你可能疑问,求n次幂算n次叠 ...

  8. 矩阵快速幂求斐波那契数列(初学整理)

    参考文章: http://blog.csdn.net/u013795055/article/details/38599321 http://blog.csdn.net/g_congratulation ...

  9. 矩阵快速幂+构造方法

    与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...

最新文章

  1. SAP MM 物料主数据consumption视图数据的更新?
  2. phoneGap+jquery mobile项目经验
  3. python 从深度相机realsense生成pcl点云
  4. 这家获得谷歌投资的企业想把亚马逊挑下神坛
  5. LabelBinarizer的妙用
  6. PGM中置信传递、和—积算法
  7. vs2010 sp1 安装 Silverlight4_Tools 提示 错误 解决办法
  8. angularjs揭秘
  9. redis用HyperLogLog计算UV
  10. 小米冲击高端,这次能否成功?
  11. Asterisk怎样配置才能出视频
  12. SQL Server BI Step by step 1 AdventureWorks数据库的安装
  13. 《刻意练习》读书心得 驳斥1万小时定律、有目的的练习、自我实现的预言、走出舒适区
  14. 最新版CameraX入门(拍照、存储展示、切换前后摄像头、手电筒、闪光灯、手势伸缩、双击放大缩小)
  15. 个人永久性免费-Excel催化剂功能第53波-无比期待的合并工作薄功能
  16. 天津市儿童计算机编程课,天津2021少儿编程(少儿机器人编程是什么)
  17. 聚划算导航栏的反弹效果代码 纯原生js实现
  18. 高等数学A(一)第一章笔记分享
  19. 【MK转链机器人】开发淘宝、京东、苏宁、拼多多转链软件,及接口开放
  20. 学了又忘?通俗易懂 Android视图系统的设计与实现!

热门文章

  1. Vivado之增量编译的实现
  2. 【FPGA】SRIO IP核的三层协议的作用?
  3. 【 FPGA 】时钟抖动浅记
  4. 解决Adobe Reader安装错误150120
  5. QApplication类详细描述
  6. scala学习笔记-面向对象编程之Trait
  7. 黄聪:说说JSON和JSONP,也许你会豁然开朗(转)
  8. Response.Redirect:无法在发送 HTTP 标头之后进行重定向
  9. Android跳转intent简单教程
  10. c嵌入python类获取返回值