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

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:

.

Source

【分析】:矩乘其实很简单,通过自己构造或者是搜索对于一个递推公式求出它所对应的常数矩阵,然后套个快速幂就可以迅速求解第n项。最后输出的是矩阵最左上方的值。根据前面的一些思路,现在我们需要构造一个2 x 2的矩阵,使得它乘以(a,b)得到的结果是(b,a+b)。每多乘一次这个矩阵,这两个数就会多迭代一次。那么,我们把这个2 x 2的矩阵自乘n次,再乘以(0,1)就可以得到第n个Fibonacci数了。不用多想,这个2 x 2的矩阵很容易构造出来。
【代码】:

#include <iostream>
#include <cstddef>
#include <cstring>
#include <vector> using namespace std; typedef long long ll;
const int mod=;
typedef vector<ll> vec;
typedef vector <vec> mat; mat mul(mat &a,mat &b)
{
mat c(a.size(),vec(b[].size()));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++){
c[i][j]+=a[i][k]*b[k][j];
c[i][j]%=mod;
}
return c;
} mat Pow(mat a,ll n)
{
mat res(a.size(),vec(a.size()));
for(int i=;i<a.size();i++)
res[i][i]=;
while(n)
{
if(n&)
res=mul(res,a);
a=mul(a,a);
n/=;
}
return res;
} ll solve(ll n)
{
mat a(,vec());
a[][]=;
a[][]=;
a[][]=;
a[][]=;
a=Pow(a,n);
return a[][];
} int main()
{
ll n;
while(cin>>n&&n!=-)
{
cout<<solve(n)<<endl;
}
}

斐波那契快速幂

POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】的更多相关文章

  1. POJ 3070(求斐波那契数 矩阵快速幂)

    题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...

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

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

  3. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  4. 51nod1242 斐波那契数列 矩阵快速幂

    1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...

  5. hdu4549 M斐波那契数列 矩阵快速幂+快速幂

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...

  6. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  7. 洛谷P1962 斐波那契数列(矩阵快速幂)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...

  8. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

  9. 洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质

    P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306 这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位 ...

随机推荐

  1. 1250 Fibonacci数列(矩阵乘法快速幂)

    1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 定义:f0=f1=1, f ...

  2. Java多线程-join方法

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. 具体例子看链接 ...

  3. json对象数据列数

    // var len = data.length(); // alert(data.Rows.length); var colCount = (function count(){//一条记录中有几个键 ...

  4. 【Linked List Cycle II】cpp

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  5. Mac 因误使用chmod -R 777 命令更改 /usr/bin 造成终端不能实用,提醒进程已结束的完美解决方案!

    1.不用删除任何文件. 2.启动root用户权限 4.在用root用户登进去 5.在root用户中使用终端输入命令 chown root:wheel /usr/bin/login chmod u+s ...

  6. Mac: mac git 的安装 及实现自动补全

    1.检查是否装了brew $ brew list 如果没有,拷贝以下命令到终端 回车.可以安装好brewruby -e "$(curl -fsSL https://raw.githubuse ...

  7. 实用拜占庭容错算法PBFT

    实用拜占庭容错算法PBFT 实用拜占庭容错算法PBFT 96 乔延宏 2017.06.19 22:58* 字数 1699 阅读 4972评论 0喜欢 11 分布式架构遭遇的问题 分布式架构会遭遇到以下 ...

  8. Spring Cloud Feign 简单入门

    Feign是一个生命是的web service 客户端,使用方式非常简单:接口+注解,spring cloud feign 对feign惊醒了增强使它支持了spring mcv注解. 示例(以下示例结 ...

  9. PAT1023

    给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是1001555 ...

  10. 【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树

    题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node w ...