Fibonacci

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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 <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define MOD 10000
int n; struct mat
{
int at[][];
} d; mat momu(mat a,mat b)
{
mat c;
memset(c.at,,sizeof(c.at));
for(int i=; i<n; i++)
{
for(int k=; k<n; k++)
{
if(a.at[i][k])
for(int j=; j<n; j++)
{
c.at[i][j]+=a.at[i][k]*b.at[k][j];
if(c.at[i][j]>MOD)
{
c.at[i][j]%=MOD;
}
}
}
}
return c;
} mat expo(mat p,int k)
{
if(k==)
return p;
mat e;
memset(e.at,,sizeof(e.at));
for(int i=; i<n; i++)
e.at[i][i]=;
if(k==)
return e;
while(k)
{
if(k&)
e=momu(p,e);
p=momu(p,p);
k>>=;
}
return e; } int main()
{
n=;
d.at[][]=;
d.at[][]=;
d.at[][]=;
d.at[][]=;
int t;
while(scanf("%d",&t)!=EOF)
{
if(t==-)
break;
mat dd=expo(d,t);
int ans=dd.at[][]%MOD;
printf("%d\n",ans);
}
return ;
}

约等于模板啦啦啦啦~~~~~~~~~~~

poj 3070的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

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

  2. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

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

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

  4. POJ 3070 + 51Nod 1242 大斐波那契数取余

    POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix ...

  5. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  6. (矩阵快速幂) Fibonacci -- poj -- 3070

    链接: http://poj.org/problem?id=3070   Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. poj 3070 矩阵计算Fibonacci

    地址 http://poj.org/problem?id=3070 大意是输入一个数字 输出位于Fibonacci数列该位置的数字模10000的结果 由于n比较大 0 ≤ n ≤ 1,000,000, ...

  8. POJ 3070 Fibonacci

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

  9. poj 3070 矩阵快速幂模板

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

随机推荐

  1. js常用方法

    若未声明,则都是js的方法 1.indexOf indexOf(str):默认返回字符串中第一次出现索引位置 的下标,没有则返回-1 indexOf(str,position):返回从position ...

  2. XHTML标签的嵌套规则分析

    在 XHTML 的语言里,我们都知道:ul 标签包含着 li.dl 标签包含着 dt 和 dd——这些固定标签的嵌套规则十分明确.但是,还有许多标签是独立的,它们没有被捆绑在一起,比如 h1.div. ...

  3. gulp系列文章一 fis vs grunt vs gulp,为什么要是gulp呢?

    gulp是最近火起来的前端构建工具,大有赶超grunt之势,它和grunt这种构建工具比较像. grunt是写一个Gruntfile.js来写配置代码,gulp则是写一个gulpfile.js来写配置 ...

  4. NC nc5.x报表设置合计行是否显示

    首先要先继承UI类 /** * 设置合计行是否显示 */ public TotalsReportUI() { super(); getReportBase().getBodyPanel().setTo ...

  5. Java Struts2 POI创建Excel文件并实现文件下载

    Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...

  6. Excel中添加并使用宏实现批量更新数据

    一.状况描述    当我们需要后台更新大量数据的时候,可以使用该功能.二.解決方案    (1)新建一个Excel文件,并另存为启用宏的Excel工作簿,扩展名为.xlsm.    (2)在Excel ...

  7. JSTL和EL

    JSTL和EL 1.EL表达式总是放在大括号里,而且前面有一个美元符前缀. person.name2.相似点:(1).都可以让我们在静态内蓉中插入动态信息.EL:室外温度是{emp}度 JSP:室外温 ...

  8. [10]APUE:信号

    [a] 常用信号 SIGABRT 调用 abort 函数时产生此信号,进程异常终止 SIGALRM 调用 alarm 或 setitimer 函数超时之后产生 SIGCHLD 子进程终止或 stop ...

  9. linux命令连接远程服务器

    ssh root@IP    ***连接远程ip cd 路径           ***进入指定路径 ll                   ***查看当前文件 ll -a             ...

  10. block的解析

    1. 操作系统中的栈和堆 我们先来看看一个由C/C++/OBJC编译的程序占用内存分布的结构: 栈区(stack):由系统自动分配,一般存放函数参数值.局部变量的值等.由编译器自动创建与释放.其操作方 ...