Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9650   Accepted: 6856

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

和普通的高速幂的写法同样,不同的是须要计算矩阵相乘,仅仅要写对矩阵的乘法,就没难度了
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
struct node{
LL s11 , s12 , s21 , s22 ;
};
node f(node a,node b)
{
node p ;
p.s11 = (a.s11*b.s11 + a.s12*b.s21)%10000 ;
p.s12 = (a.s11*b.s12 + a.s12*b.s22)%10000 ;
p.s21 = (a.s21*b.s11 + a.s22*b.s21)%10000 ;
p.s22 = (a.s21*b.s12 + a.s22*b.s22)%10000 ;
return p ;
}
node pow(node p,int n)
{
node q ;
q.s11 = q.s22 = 1 ;
q.s12 = q.s21 = 0 ;
if(n == 0)
return q ;
q = pow(p,n/2);
q = f(q,q);
if( n%2 )
q = f(q,p);
return q ;
}
int main()
{
int n ;
node p ;
while(scanf("%d", &n) && n != -1)
{
p.s11 = p.s12 = p.s21 = 1 ;
p.s22 = 0 ;
p = pow(p,n);
printf("%d\n", p.s12);
}
return 0;
}

poj3070--Fibonacci(矩阵的高速幂)的更多相关文章

  1. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  2. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  3. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  4. POJ3070 Fibonacci[矩阵乘法]【学习笔记】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  5. HDU2256-Problem of Precision(矩阵构造+高速幂)

    pid=2256">题目链接 题意:求sqrt(sqrt(2) + sqrt(3)) ^ 2n MOD 1024 思路: 代码: #include <iostream> # ...

  6. HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  7. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  8. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  9. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

随机推荐

  1. [LeetCode] Regular Expression Matching [6]

    称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  2. Android Tween Animation

    View Animation, 它显示在view向上Tween Animation Tween动画.本质上没有变化View对象本身.只要改变它绘制 实施方式有两种.一个xml定义,直接在代码中的定义 ...

  3. Cordova探险系列(一个)

    最早接触PhoneGap平台是在1年多之前,可以使用HTML.CSS和JavaScript跨平台来编写Android或者IOS设备程序.而且应用的核心代码不须要多少改动就行移植.确实让我感觉的到它应该 ...

  4. MySQL性能、监控与灾难恢复

    原文:MySQL性能.监控与灾难恢复 监控方案:     up.time    http://www.uptimesoftware.com/    收费     Cacti        http:/ ...

  5. 简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值(转)

    本文将简单介绍如何使用PowerMock和Mockito来mock1. 构造函数2. 静态函数3. 枚举实现的单例4. 选择参数值做为函数的返回值5. 在调用mock出来的方法中,改变方法参数的值 一 ...

  6. C++四种类型的转换

    在C/C++使用的语言 (type) value(您还可以使用type(value))对于显式类型转换,经常提到投.转换程序猿的精度等完全掌握手,一个传统投往往是过度使用.成为C++要根源. 为了降低 ...

  7. Java的进程内缓存框架:EhCache (转)

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种缓存 ...

  8. 使用C/C++编译预处理时须要注意的问题

    1.宏定义不是C/C++语句,不须要使用语句结束符":",否则它也被看做宏体的一部分. 2.不要在引用宏定义的參数列表中使用增量和减量运算符,否则将导致变量的多次求值.比如: #d ...

  9. 使用Java快速实现进度条(转)

    基于有人问到怎样做进度条,下面给个简单的做法: 主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax. ...

  10. unity3D实际的原始视频游戏开发系列讲座10它《战斗:外来入侵》在第一季度游戏开发

    解说文件夹 <保卫战:异形入侵>游戏开发 第一讲   游戏演示和资源的介绍 第二讲  "异形"怪物的实现 第三讲  "异形"怪物生命值的体现 第四讲 ...