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

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

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

Stanford Local 2006

问题链接:POJ3070 Fibonacci
问题简述:(略)
问题分析
    矩阵快速幂的模板题。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* POJ3070 Fibonacci */#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;const int MOD = 1e4;
const int N = 2;struct Matrix
{int m[N][N];Matrix() {}Matrix operator*(Matrix const &a)const{Matrix b;memset(b.m, 0, sizeof(b.m));for (int i = 0 ;i < N; i++)for (int j = 0; j < N; j++)for (int k = 0; k < N; k++)b.m[i][j] = (b.m[i][j] + this->m[i][k] * a.m[k][j]) % MOD;return b;}
};Matrix pow_mod(Matrix a, int n)
{Matrix b;memset(b.m, 0, sizeof(b.m));for (int i = 0; i < N; i++)b.m[i][i] = 1;while (n > 0){if (n & 1) b = b * a;a = a * a;n >>= 1;}return b;
}int main()
{Matrix a;for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)a.m[i][j] = 1;a.m[1][1] = 0;int n;while (~scanf("%d", &n) && n != -1){Matrix b = pow_mod(a, n);printf("%d\n", b.m[0][1]);}return 0;
}

POJ3070 Fibonacci【矩阵快速幂】相关推荐

  1. poj3070 Fibonacci 矩阵快速幂

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

  2. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

  3. HDU 3306 Another kind of Fibonacci 矩阵快速幂

    题目链接 因为S(N) , S(N) = A(0)^2 +A(1)^2+--+A(n)^2.所以构造的矩阵一定要维护A(n)^2 s[n-1]=s[n-2]+A[n-1]^2 A[n]=x*A[n-1 ...

  4. POJ3070 Fibonacci(矩阵快速幂)

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

  5. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  6. SDNU 1062.Fibonacci(矩阵快速幂)

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

  7. 矩阵快速幂 POJ 3070 Fibonacci

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

  8. (矩阵快速幂)解所有类似Fibonacci 的题目

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

  9. POJ3070矩阵快速幂简单题

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

最新文章

  1. JSP第二次作业_6小题
  2. pads铺铜不能开启drp_PowerPCB (PADS )常见问题全集-泪滴-规则设定
  3. 多项式乘法c语言,急!!!!c语言:求n次多项式的加法和乘法
  4. 【渝粤教育】广东开放大学 财务管理 形成性考核 (58)
  5. oracle dbra,资源供给:IO子系统之二
  6. Centos 下PHP编译安装fileinfo扩展
  7. 首届全国信创大赛圆满收官,信创新势力载誉而归!
  8. 推送MobPush-API说明
  9. python参考手册下载_Python中文手册【Word版 】
  10. 你知道这些SOLIDWORKS零件图知识吗?
  11. RRZCMS安全防护建议
  12. push notifications step1 打不开
  13. Micron:DDR2 SDRAM与SODIMM
  14. 山东法律学校97级计算机班,我校计算机学院97级计算机专业校友重聚母校
  15. 为什么现在java这么难找,现在java工作难找吗
  16. access是干什么的软件
  17. 五种主流音频无损压缩格式简介
  18. 香蕉派 BPI-R2 作为4G多路聚合融合通信应用开发平台
  19. Jenkins Git Changelog Plugin
  20. 开始协议处理句柄[http-nio-8080]_微软win10发布KB4520062更新,解决登录黑屏和开始菜单空白等问题...

热门文章

  1. 使用阿富汗和巴基斯坦地区的SRTM数据生成山体阴影和彩色地形图
  2. windows下安装mongodb4.x版本
  3. 2年3个月推倒重来的决心 独立游戏《须弥•域》的涅槃之路
  4. Starling 动画功能
  5. 关于用C#编写ActiveX控件4(转)
  6. php 查字符串,PHP查询字符串技巧分享
  7. 死磕 18 个 Java 8 的日期处理,工作必用!
  8. JDK历史版本主要新特性
  9. Python 之 变量进阶(理解)
  10. Spark mapPartition方法与map方法的区别