链接:
 
Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11236   Accepted: 7991

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:

.

代码:

#include<stdio.h>
#include<string.h>
#define MOD 10000
struct node
{
int m[][];
}a, b; node cheng(node x, node y)
{
int i, j, k;
node c; for(i=; i<; i++)
for(j=; j<; j++)
{
c.m[i][j] = ;
for(k=; k<; k++)
c.m[i][j] = (c.m[i][j] + x.m[i][k]*y.m[k][j])%MOD;
} return c;
} int Fast_MOD(int n)
{
a.m[][] = a.m[][] = a.m[][] = ;
a.m[][] = ; b.m[][] = b.m[][] = ; /// b 初始化为单位矩阵
b.m[][] = b.m[][] = ; while(n)
{
if(n&) /// n是奇数
b = cheng(b, a);
a = cheng(a, a);
n >>= ;
}
return b.m[][];
} int main()
{
int n;
while(scanf("%d", &n), n!=-)
{
printf("%d\n", Fast_MOD(n));
}
return ;
}

(矩阵快速幂) Fibonacci -- poj -- 3070的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

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

  2. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  3. poj 3070 Fibonacci 矩阵快速幂

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

  4. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  5. POJ 3070 Fibonacci 矩阵快速幂模板

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

  6. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  7. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  8. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  9. poj 3070 && nyoj 148 矩阵快速幂

    poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...

  10. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...

随机推荐

  1. Spring 中的国际化Message的简单例子(ApplicationContext) 不跟框架集成的版本

    首先,建立一个描述message的XML文件,名为messages.xml     <?xml version="1.0" encoding="UTF-8" ...

  2. Hibernate的一个问题object references an unsaved transient instance - save the transi5

    1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...

  3. python 日期格式

    %a 星期几的简写%A 星期几的全称%b 月分的简写%B 月份的全称%c 标准的日期的时间串%C 年份的后两位数字%d 十进制表示的每月的第几天%D 月/天/年%e 在两字符域中,十进制表示的每月的第 ...

  4. Unknown column in 'field list'

    好久都没有亲手写数据库的连接以及操作了,近期一直都是用ejb,直接就映射到数据库了,所以关于jdbc的知识都忘得差不多了.不过吧,为了学习struts2,还是重新将jdbc知识捡起来.找了一上午的错误 ...

  5. tensorflow生成随机数的操作 tf.random_normal & tf.random_uniform & tf.truncated_normal & tf.random_shuffle

    tf.random_normal 从正态分布输出随机值. random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name ...

  6. SharePoint 2010 图表控件

    需求: 统计每周的事件创建数量及关闭数量 以折线的形式显示 一张图表显示两条折线 知识点: 图表控件的使用 外部内容类型 数据库(sql)view(数据源) 结果:

  7. django通过url传递参数(编辑操作页面)

    在做到编辑部分时,想到的办法是在编辑上跳转到页面时给他一个包含唯一标识id的url,然后通过这个url中的id去查询出该条数据,将数据内容显示在编辑页面.   1.编辑按钮 <button on ...

  8. css3自定义placeholder字体颜色

    ::-webkit-input-placeholder{color:#f00;} ::-moz-placeholder{color:#f00;} :-moz-placeholder{color:#f0 ...

  9. vs2010使用中遇到的一些问题[xyytIT]

    1. 代码编辑器行号显示: 设置方法:点击菜单栏的"工具"-->"选项",弹出选项窗体-->标上选项窗体左下方的"显示所有设置" ...

  10. Ubuntu安装libevent

    背景: 版本: libevent 2.1.6beta linux下: 按照github官方做法: $ sudo apt-get install openssl $ mkdir build && ...