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

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

Stanford Local 2006

【分析】:矩乘其实很简单,通过自己构造或者是搜索对于一个递推公式求出它所对应的常数矩阵,然后套个快速幂就可以迅速求解第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=10000;
typedef vector<ll> vec;
typedef vector <vec> mat;

mat mul(mat &a,mat &b)
{
    mat c(a.size(),vec(b[0].size()));
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
        for(int k=0;k<2;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=0;i<a.size();i++)
        res[i][i]=1;
    while(n)
    {
        if(n&1)
            res=mul(res,a);
        a=mul(a,a);
        n/=2;
    }
    return res;
}

ll solve(ll n)
{
    mat a(2,vec(2));
    a[0][0]=1;
    a[0][1]=1;
    a[1][0]=1;
    a[1][1]=0;
    a=Pow(a,n);
    return a[0][1];
}

int main()
{
    ll n;
    while(cin>>n&&n!=-1)
    {
        cout<<solve(n)<<endl;
    }
}

斐波那契快速幂

原文地址:https://www.cnblogs.com/Roni-i/p/8350614.html

时间: 2024-08-03 19:44:14

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

UVA10299- Modular Fibonacci(斐波那契数列+矩阵快速幂)

题目链接 题意:给出n和m,求出f(n) % m, f(x)为斐波那契数列. 思路:因为n挺大的,如果直接利用公式计算很有可能会TLE,所以利用矩阵快速幂求解,|(1, 1), (1, 0)| * |f(n - 1), f(n - 2)| = |f(n), f(n - 1)|,所以求f(n)相当于|f(1), f(0)|乘上n - 1次的|(1, 1), (1, 0)|. 代码: #include <iostream> #include <cstdio> #include <

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

HDU 4549 M斐波那契数列 (  矩阵快速幂 + 费马小定理  ) 题意:中文题,不解释 分析:最好的分析就是先推一推前几项,看看有什么规律 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef __int64 LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 100000

HDU 4549 M斐波那契数列(矩阵快速幂&amp;费马小定理)

ps:今天和战友聊到矩阵快速幂,想到前几天学长推荐去刷矩阵专题,挑了其中唯一一道中文题,没想到越过山却被河挡住去路... 题目链接:[kuangbin带你飞]专题十九 矩阵 R - M斐波那契数列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u 题意 Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2]

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

M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的值吗? Input 输入包含多组测试数据:每组数据占一行,包含3个整数a, b,

HDU4549 M斐波那契数列(矩阵快速幂+费马小定理)

Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,你能求出F[n]的值吗? Input 输入包含多组测试数据:每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 ) Output 对每组测试数据请输出一个整数F[n],由于F[n]可能很大,你只需输出F[n]对1000000007取模后的值即可

[P1962] 斐波那契数列 (矩阵快速幂)

题意:求出 f(n) mod 1000000007 的值,n 在long long 范围内: 解法:矩阵快速幂: 1.矩阵快速幂:  = X …………① 同理:  =  X …………② 我们把②式带入①式 得:   = X 附上代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define rg registe

斐波那契数列——矩阵的幂求解

题目: 斐波那契数列的递推公式如下: F(0) = 0; F(1) = 1; F(n + 2) = F(n + 1) + F(n); 求数列的第N项的值对10000取余的结果.( 0<=n<= 10^16) 求解斐波那契数列,如果N比较小的情况下,可以直接打表求解,但是对于N很大的情况下,并不适用. 所以,有些人会想到高精度计算,但是,N达到10^5以上时,时间复杂度难以想象,每计算一个数,需要进行高精度加法.然而还有求解对10000的取余的值. 我们可以用矩阵的幂来求解.斐波那契数列的递推公

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

Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的值吗? Input输入包含多组测试数据:每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 ) Output对每组测试数据请输出一个整数F[n],由于F[n]可能很大,你只需输出F[n]对1000000007取模后的值即可,

poj3070 (斐波那契,矩阵快速幂)

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