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 实施细节 Rotate Image

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie Rotate Image Total Accepted: 15609 Total Submi ...

  2. HDU 2063:过山车(偶匹配,匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. 一切从编辑器说起:web前端代码编辑器

    俗话说:工欲善其事,必先利其器. 工欲善其事必先利其器.谓工匠想要使他的工作做好,一定要先让工具锋利.比喻要做好一件事,准备工作非常重要. 前端写代码也是一样,需要一个好的适合自己的代码编辑器. 我想 ...

  4. 一个简单的RPC框架

    一个 系统模型 二.数据库代码实现 1. mkdir database cd database vim dbInit.c /* * * Database Init tool * */ #include ...

  5. Android开发学习总结(五)——Android应用目录结构分析(转)

    一.手动创建android项目 手动创建一个Android项目,命名为HelloWorld,命令如下: android create project -n HelloWorld -t 1 -p E:/ ...

  6. html标和下标应用

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. mysql经常使用命令总结

    MySQL经常使用指令(备查) 最经常使用的显示命令: 1.显示数据库列表.  show databases;  2.显示库中的数据表:  use mysql; show tables;  3.显示数 ...

  8. Codeforces Round #267 (Div. 2) A

    题目: A. George and Accommodation time limit per test 1 second memory limit per test 256 megabytes inp ...

  9. 走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能

    原文:走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能 最近公司来一个非常虎的dba  10几年的经验 这里就称之为蔡老师吧 在征得我们蔡老同意的前提下  我们来分享一下蔡老给我们 ...

  10. Objective-C基调(4)Category

    OC它提供了一种不同的方式--Category,可以动态地添加新的行为已经存在的类(方法),这确保了较小的类的原始设计,然后逐渐加入扩展. 正在使用Category扩张的上课时间,你并不需要创建一个子 ...