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

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
题意:求斐波拉契数列,只不过n值很大要用到矩阵快速幂
分析:这是矩阵快速幂的入门题,借此题写下模板。
首先我们很容易的得到递推式:f(n) = f(n-1)+f(n-2)
也很容易的得到他们的矩阵式:
| f(n-1)  f(n-2)  |   x  | 1  1 |   =  | f(n)  f(n-1) |
|    0         0     |       | 1  0 |       |   0       0    |
a                      b                  c
写下简单的推导过程:首先把右边式子写在矩阵a第一行,把右边式子可能得到的结果写在矩阵c的第一行,a和c剩下的每行都为0,接下来根据矩阵a和矩阵c写出矩阵b。
得到矩阵式后,就是简单的套用矩阵快速幂的模板了,下面是我的代码
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e4 + 10;
const int mod = 10000;
typedef long long ll;
struct matrix {ll a[10][10];
};
matrix base, ans;
matrix multip( matrix x, matrix y ) { //求c矩阵的过程matrix tmp;for( ll i = 0; i < 2; i ++ ) {for( ll j = 0; j < 2; j ++ ) {tmp.a[i][j] = 0;for( ll k = 0; k < 2; k ++ ) {tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k] * y.a[k][j] ) % mod;}}}return tmp;
}
ll qow( ll a, ll b ) { //求数的快速幂,与此题无关ll sum = 1;while( b ) {if( b&1 ) {sum = sum*a%mod;}a = a*a%mod;b /= 2;}return sum;
}
ll f( ll x ) {  //矩阵快速幂的运算while( x ) {if( x&1 ) {ans = multip( ans, base );}base = multip( base, base );x /= 2;}return ans.a[0][0];
}
int main() {ll n;while( cin >> n ) {if( n == -1 ) {break;}memset( ans.a, 0, sizeof(ans.a) ); //初始化a矩阵和b矩阵,根据你所得到的矩阵式初始化memset( base.a, 0, sizeof(base.a) );ans.a[0][0] = 1, ans.a[0][1] = 0;base.a[0][0] = base.a[0][1] = base.a[1][0] = 1;if( n == 0 ) {cout << 0 << endl;} else if( n == 1 ) {cout << 1 << endl;} else {cout << f(n-1) << endl;}}return 0;
}

  

转载于:https://www.cnblogs.com/l609929321/p/9316768.html

POJ 3070 Fibonacci 矩阵快速幂模板相关推荐

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

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

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

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

  3. POJ3070 矩阵快速幂模板

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

  4. poj3070 Fibonacci 矩阵快速幂

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

  5. 快速幂+矩阵快速幂模板

    快速..运算 快速幂 运用位运算 代码 分析 矩阵快速幂 题目 分析 代码 拓一..: 快速幂 运用位运算 强大的位运算把我搞得蒙蒙的 理解了之后我表示很喜欢!!! 代码 int power(int ...

  6. 51nod 1113 矩阵快速幂 模板题

    1113 矩阵快速幂 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计 ...

  7. How many ways?? - hdu2157(矩阵快速幂-模板)

    分析:求Map^k,刚开始没有用快速幂,TLE了   代码如下: =================================================================== ...

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

  9. (转)矩阵快速幂模板

    大佬博客:https://blog.csdn.net/baidu_23081367/article/details/52347256 代码: const int mat_size = 5;//矩阵大小 ...

  10. 51nod 1113 矩阵快速幂 模板

    给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大,只需要输出每个元素Mod (10^9 + 7)的结果. Input 第1行:2个数N和M,中间用空格分隔. ...

最新文章

  1. yum update php5.5,(二)Centos7下Yum更新安装PHP5.5,5.6,7.0
  2. malloc函数具体解释
  3. 安装Scrapy遇到Comand c:\users\lenovo\appdata\local\programs\python\python35\python.exe
  4. tensorflow2.x版本无法调用gpu的一种解决方法
  5. DLL 的导入与导出
  6. 产品经理必须要了解的经济学原理--“口红效应”
  7. Spring JDBC的学习
  8. 对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
  9. iOS:quartz2D绘图小项目(涂鸦画板)
  10. Hopfield神经网络的联想记忆——数字识别的一些笔记
  11. 使用腾讯云服务器搭建自己网站应该怎么做
  12. OneDrive无法登陆正常使用 There was a problem connecting to onedrive
  13. 使用线程池批量爬取梨视频最热视频
  14. Platform Builder
  15. String类最详解(String、StringBuffer、StringBuilder、正则表达式)表格+代码让你一文读懂!!!
  16. AW codec驱动跨平台移植
  17. wms地图绘制工具_WMS/WMTS
  18. 关于pc页面在手机屏幕显示不全
  19. Threejs—BIM管道流向动态效果
  20. 软件测试(基础)· 软件测试的生命周期 · 如何描述一个 Bug · Bug 的级别 · Bug 的生命周期 · 争执 · Bug 评审

热门文章

  1. python复制csv数据_如何使用Python将CSV数据复制到现有xlsx文件
  2. 大数据 机器学习 人工智能_在这个季节中,您如何免费学习数据科学,人工智能和机器学习。...
  3. 声明与所在行数不兼容_深度理解:Windows DLL 二进制兼容性探究
  4. java-实战java高并发程序设计-ch2java并行程序基础
  5. 【编译打包】nginx-1.4.7-1.el6.lite.src.rpm
  6. CSS.DIV网页样式与布局学习总结
  7. My Job Exceptation
  8. 用接纳的心看待新员工
  9. 如何使用JavaScript 结合XSLT转换XML文档
  10. 远程管理技术对服务器管理的影响