Fibonacci
 

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 思路:矩阵快速幂,没什么可说的。
 #include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct Matrix{
int m[][];
Matrix(){
memset(m, , sizeof(m));
}
}Matrix;
Matrix mtMul(Matrix A, Matrix B){
Matrix tmp;
for(int i = ;i < ;i ++)
for(int j = ;j < ;j ++)
for(int k = ;k < ;k ++){
int t = (A.m[i][k] * B.m[k][j])%;
tmp.m[i][j] = (tmp.m[i][j] + t)%;
}
return tmp;
}
Matrix mtPow(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtPow(A, k >> );
Matrix res = mtMul(tmp, tmp);
if(k & ) res = mtMul(res, A);
return res;
}
int main(){
int n;
while(~scanf("%d", &n) && (n+)){
if(n == ) printf("0\n");
else{
Matrix M;
M.m[][] = M.m[][] = M.m[][] = ;
M.m[][] = ;
Matrix tmp = mtPow(M, n);
printf("%d\n", tmp.m[][]);
}
}
return ;
}
 

POJ ---3070 (矩阵乘法求Fibonacci 数列)的更多相关文章

  1. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  2. poj 3070 矩阵快速幂模板

    题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...

  3. 用PL0语言求Fibonacci数列前m个中偶数位的数

    程序说明:求Fibonacci数列前m个中偶数位的数: 这是编译原理作业,本打算写 求Fibonacci数列前m个数:写了半天,不会写,就放弃了: 程序代码如下: var n1,n2,m,i; pro ...

  4. C++项目參考解答:求Fibonacci数列

    [项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...

  5. 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: /  0                      n=0 f(n)=      ...

  6. 《面试题精选》15.O(logn)求Fibonacci数列

    题目:定义Fibonacci数列例如以下: /    0                      n=0 f(n)=      1                      n=1          ...

  7. 求Fibonacci数列通项公式

    0. Intro \[f_n=\begin{cases} 0 & (n=0) \\ 1 & (n=1) \\ f_{n-1}+f_{n-2} & (n>1) \end{c ...

  8. ☆ [HDU2157] How many ways?? 「矩阵乘法求路径方案数」

    传送门:>Here< 题意:给出一张有向图,问从点A到点B恰好经过k个点(包括终点)的路径方案数 解题思路 一道矩阵乘法的好题!妙哉~ 话说把矩阵乘法放在图上好神奇,那么跟矩阵唯一有关的就 ...

  9. 用Python实现求Fibonacci数列的第n项

    1. 背景——Fabonacci数列的介绍(摘自百度百科): 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacc ...

随机推荐

  1. ICBC中的路由设置

    才去的中国工商银行,用身份证叫了A0076的号,前边还有26个人,闲来无聊果断拿出手机收取wifi.有两个ssid为ICBC的路由,信道分别是1号和6号,还好不需要密码,不过一会就连接上了. 那我先上 ...

  2. 配置 Struts2 Hello World

    http://javaweb.group.iteye.com/group/wiki/1505-struts2-under-helloworld---how-to-make-the-first-of-t ...

  3. http通信协议详解

    转载自:http://blog.csdn.net/gueter/article/details/1524447 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒 ...

  4. (转)HTTP协议(3)

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  5. win8.1系统下,点击一个窗口,【当前活动窗口】该窗口无法置顶

    两个或多个窗口同时显示在桌面的时候,点击下一层的窗口,无法置顶显示,无论怎么点击,还是隐藏在原置顶窗口的后面,只能手动把原置顶窗口最小化后,才能看到.例如,A窗口现在置顶,B窗口在A的后面,露出来一部 ...

  6. jdbc 连接Oracle informix Mysql

    package com.basicSql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...

  7. EasyUI portal自定义小图标,不是用js方式加载

    <script src="~/Scripts/jquery.portal.js"></script> <script> $(function ( ...

  8. Xcode 7.3.1的模拟器路径

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Core ...

  9. [DP] Rod-cutting problem

    给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同.下面是一个例子 我们要通过切成小段卖出尽可能高的总价钱.问题是:How to decompose the problem? De ...

  10. codeforces 388C Fox and Card Game

    刚刚看到这个题感觉是博弈题: 不过有感觉不像,应该是个贪心: 于是就想贪心策略: 举了一个例子: 3 3 1 2 3 4 3 4 1 2 5 4 1 2 5 8 如果他们两个每次都拿对自己最有利的那个 ...