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. iOS设置导航栏标题

    方法一:在UIViewController中设置self.title. 方法二:设置self.navigationItem.titleView.

  2. c# long转 datetime

    ; DateTime start = , , , , , , DateTimeKind.Utc); DateTime date = start.AddMilliseconds(time).ToLoca ...

  3. C#图片压缩处理算法

    原文链接:http://blog.csdn.net/szstephenzhou/article/details/38900345

  4. PHP向mysql中插入数据的方法

    require "database.php"; $po_code = "YMWF2015-6-25-1"; $customer = "youmei&q ...

  5. 去除inline-block之间的间距

    a标签的父容器添加: font-size: 0; -webkit-text-size-adjust:none;

  6. 一个sendMessage

    Message 1.判断是否同意协议.2.验证验证码是否正确.3.验证手机是否符合规则,符合规则就用message()发送短信,验证码的有效期以及使用的短信模板,在配置文件中进行管理.返回值下标为st ...

  7. .Net多文件同时上传(Jquery Uploadify)

    前提:领导给了我一个文件夹,里面有4000千多张产品图片,每张图片已产品编号+产品名称命名,要求是让我把4000多张产品图片上传到服务器端,而且要以产品编码创建n个文件夹,每张图片放到对应的文件夹下. ...

  8. app之间的跳转,进入二级界面

    功能实现:A跳到B并打开B中指定页面.http://blog.csdn.net/dollyyang/article/details/50325307 点击页面判断是否安装app并打开,否则跳转app ...

  9. c语言打印空心菱形

    ***算法:把菱形的中心看成坐标的原点(,),由此可以知道,如果|x| + |y| <= n;则打印输出"*"号,否则打印输出" " int mai(){ ...

  10. python 实现彻底删除文件夹和文件夹下的文件

    python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...