ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 3070 Fibonacci (矩阵快速幂)

2019-08-27 20:04:17  阅读:226  来源: 互联网

标签:matrix int ll 3070 POJ Fibonacci ans include


题目链接:POJ 3070

Problem Description

In the Fibonacci integer sequence, \(F_0 = 0\), \(F_1 = 1\), and \(F_n = F_{n − 1} + F_{n − 2}\) for \(n \ge 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

title.

Given an integer \(n\), your goal is to compute the last \(4\) digits of \(F_n\).

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing \(n\) (where \(0 \le n \le 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 \(F_n\). If the last four digits of \(F_n\) are all zeros, print \(‘0’\); otherwise, omit any leading zeros (i.e., print \(F_n\) mod \(10000\)).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

Solution

题意

给定正整数 \(n\),求斐波那契数列第 \(n\) 项。

题解

矩阵快速幂

矩阵快速幂模板题。

\[ \left[ \begin{matrix} F_{n + 1} & F_n \\ F_n & F_{n - 1} \end{matrix} \right] = \left[ \begin{matrix} 1 & 1 \\ 1 & 0 \end{matrix} \right] ^ n \]

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 5;
const ll mod = 1e4;

struct Matrix {
    int n, m;
    ll a[maxn][maxn];
    Matrix(int n = 0, int m = 0) : n(n), m(m) {}
    void input() {
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                scanf("%lld", &a[i][j]);
            }
        }
    }
    void output() {
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                printf("%lld", a[i][j]);
                printf("%s", j == m? "\n": " ");
            }
        }
    }
    void init() {
        memset(a, 0, sizeof(a));
    }
    void unit() {
        init();
        for(int i = 1; i <= n; ++i) {
            a[i][i] = 1;
        }
    }
    Matrix operator *(const Matrix b) {
        Matrix c(n, b.m);
        c.init();
        for(int i = 1; i <= c.n; ++i) {
            for(int k = 1; k <= m; ++k) {
                for(int j = 1; j <= c.m; ++j) {
                    c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % mod;
                }
            }
        }
        return c;
    }
    Matrix qmod(ll b) {
        Matrix ans = Matrix(n, n);
        ans.unit();
        if(!b) return ans;
        while(b) {
            if(b & 1) ans = ans * (*this);
            *this = (*this) * (*this);
            b >>= 1;
        }
        return ans;
    }
};

int main() {
    int n;
    while(~scanf("%d", &n)) {
        if(n == -1) break;
        ll ans = 0;
        if(n) {
            Matrix m(2, 2);
            m.a[1][1] = 1;
            m.a[1][2] = 1;
            m.a[2][1] = 1;
            m.a[2][2] = 0;
            m = m.qmod(n - 1);
            ans = m.a[1][1] % mod;
        } 
        printf("%lld\n", ans);
    }
    return 0;
}

标签:matrix,int,ll,3070,POJ,Fibonacci,ans,include
来源: https://www.cnblogs.com/wulitaotao/p/11420478.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有