Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9630   Accepted: 6839

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:

.

求斐波那契序列的公式。

由于该矩阵的特殊结构使得a(n+1)[0][0] = a(n)[0][0]+a(n)[0][1], a(n+1)[0][1] = a(n)[1][1], a(n+1)[1][0] = a(n)[0][1]+a(n)[1][0], a(n+1)[1][1] = a(n)[1][0];

code:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a)) using namespace std; int n;
struct matrix
{
int a[][];
void init()
{
a[][] = a[][] = a[][] = ;
a[][] = ;
}
}; matrix mamul(matrix a,matrix b)
{
matrix c;
for(int i = ;i<;i++)
{
for(int j = ;j<;j++)
{
c.a[i][j] = ;
for(int k = ;k<;k++)
c.a[i][j]+=(a.a[i][k]*b.a[k][j]);
c.a[i][j]%=;
}
}
return c;
} matrix mul(matrix s, int k)
{
matrix ans;
ans.init();
while(k>=)
{
if(k&)
ans = mamul(ans,s);
k = k>>;
s = mamul(s,s);
}
return ans;
} int main()
{
while(scanf("%d",&n)==&n>=)
{
if(n==) puts("");
else
{
matrix ans;
ans.init();
ans = mul(ans,n-);
printf("%d\n",ans.a[][]%);
}
}
return ;
}

下面代码只是测试公式,无法解决取模的问题,因为中间为double型,无法取模:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a)) using namespace std; double Pow(double a,int n)
{
double ans = ;
while(n>=)
{
if(n&)
ans = a*ans;
n = n>>;
a = a*a;
}
return ans;
} int main()
{
int n;
double a = (sqrt(5.0)+1.0)/;
double b = (-sqrt(5.0)+1.0)/;
double c = (sqrt(5.0))/;
while(scanf("%d",&n)==)
{
int ans = (int)(c*(Pow(a,n)-Pow(b,n)))%;
printf("%d\n",ans);
}
return ;
}

poj3070 (斐波那契,矩阵快速幂)的更多相关文章

  1. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  2. 「GXOI / GZOI2019」逼死强迫症——斐波那契+矩阵快速幂

    题目 [题目描述] ITX351 要铺一条 $2 \times N$ 的路,为此他购买了 $N$ 块 $2 \times 1$ 的方砖.可是其中一块砖在运送的过程中从中间裂开了,变成了两块 $1 \t ...

  3. 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  4. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  5. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  6. POJ3070 斐波那契数列 矩阵快速幂

    题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...

  7. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  8. Fibonacci PKU logn 求斐波那契的快速方法!!!

    矩阵的快速幂 #include<cstdio> using namespace std; struct matrix { ][]; }ans,base; matrix multi( mat ...

  9. CF 316E3 Summer Homework(斐波那契矩阵+线段树)

    题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...

随机推荐

  1. Vmware vsphere 网络架构

    VMware vSphere架构下服务器会虚拟出交换机来供ESX Host虚拟机来使用,虚拟交换机有两种,vSwitch虚拟交换机和vNetwork分布式虚拟交换机,每个ESX Host均有一个标准v ...

  2. 浅析C# new和override的区别

    C#中new和override是继承中经常用到的两个关键字,但是往往有时候容易把这两个关键字的作用搞混淆. new C# new关键字表示隐藏,是指加上new关键字的属性或函数将对本类和继承类隐藏基类 ...

  3. Linux 之 编译器 gcc/g++参数详解

    2016年12月9日16:48:53 ----------------------------- 内容目录: [介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++ ...

  4. hdu 2010 - 水仙花数

    题意: 数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求输出所有在m和n范围内的水仙花 ...

  5. linux下samba的安装与使用

    samba挺好用的,配置项不多,正好适合我这种懒人使用. 下面是CentOS上面的安装与使用方法. 安装:yum -y install samba        安装一个名字叫samba的软件,安装过 ...

  6. Win7系统开放C盘下文件夹Everyone权限

    安装软件时遇到这样的情况:我就是管理员权限啊,怎么会安装有问题呢? 后来知道,用户名即使分配了你是管理员权限,有些文件还是有限制的(特别是C盘) 昨天遇到一个问题,有个文件夹里的隐藏文件就是无法显示, ...

  7. 电脑中的Bois是什么

    电脑中的Bois是什么 BOIS= Basic Input/Output System,基本输入输出系统,全称是ROM-BOIS,是只读存储器基本输入/输出系统的简写,它实际是一组被固化到电脑中,为电 ...

  8. 自然语言14_Stemming words with NLTK

    https://www.pythonprogramming.net/stemming-nltk-tutorial/?completed=/stop-words-nltk-tutorial/ # -*- ...

  9. 10月21日下午PHP常用函数

    函数四要素:返回类型  函数名  参数列表  函数体 //最简单的函数定义方式 function Show() { echo "hello"; } Show();//输出结果为he ...

  10. HTML学习笔记——post表单

    1>form1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...