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 矩阵快速幂入门题,构造矩阵[fn,fn-1]*[1,1]在自己敲一遍模板就行了。
             [0 ,0 ] [1,0]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long int LL;
using namespace std; int n,mod=; struct Matrix
{
int a[][];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=(a[i][k]*p.a[k][j]%mod);
}
res.a[i][j]%=mod;
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,int k)
{
Matrix res;
for(int i=;i<;i++)
{
res.a[i][i]=;
}
while(k)
{
if(k&) res=res*base;
base=base*base;
k>>=;
}
return res;
} void init_Matrix()
{
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
} int main()
{
while(scanf("%d",&n)&&n!=-)
{
init_Matrix();
if(n==) printf("0\n");
else if(n==) printf("1\n");
else
{
ans=ans*quick_pow(base,n-);
printf("%d\n",ans.a[][]);
}
}
return ;
}

POJ 3070 Fibonacci的更多相关文章

  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 Fibonacci

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

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

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

  5. poj 3070 Fibonacci 矩阵快速幂

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

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

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

  7. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

  8. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

  9. 题解报告:poj 3070 Fibonacci

    题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...

随机推荐

  1. 安装GIT,集成到Powershell中

    1.首先安装GIT http://msysgit.github.io 下载最新版,然后安装.没什么好说的. 装完之后,把 安装路径/bin 加入到环境变量的Path中 2.删除Git的右键菜单 安装完 ...

  2. 配置opencv时计算机显示丢失opencv_world300d.dll如何解决

    在自己安装路径里找到opencv_world300d.dll文件: 然后把opencv_world300d.dll文件复制到C://Windows/System32里:

  3. JSP数据交互

    JSP数据交互   一.jsp中java小脚本 1.<% java代码段%> 2.<% =java表达式%>不能有分号 3.<%!成员变量和函数声明%>二.注释 1 ...

  4. cloudera learning8:MapReduce and Spark

    YARN:Yet Another Resource Negotiator, Hadoop集群的资源管理器,可以对运行在Hadoop上的MapReduce V2,Spark,Impala等进行内存和CP ...

  5. RHEL6.5 换源

    由于redhat的yum在线更新要收费,即没有注册的无法使用:将redhat的yum卸载.重装第三方源. 第三方源包括:网易,重庆大学,epel 等Author wood_man.2015.10.2 ...

  6. JavaScript增强AJAX基础

    <title>js类型</title> <meta http-equiv="content-type" content="text/html ...

  7. Android下Cocos2d创建HelloWorld工程

    最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...

  8. iOS中的__typeof与typeof

    做SDK开发引入AFNetworking那么多的文件不太合适,所以这几天在精简AFNetworking,并共享中github上,https://github.com/yjh4866/Simplifie ...

  9. Python之路----------random模块

    随机数模块: import random #随机小数 print(random.random()) #随机整数 print(random.randint(1,5))#他会打印5 #随机整数 print ...

  10. vpn打通结果ping不通

    A 是一台服务器 B 是一台openvpn A 设置路由 route add -net 172.16.1.0/24 gw 192.168.125.228 B 设置伪装 -A POSTROUTING - ...