POJ3070 Fibonacci【矩阵快速幂】

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20098 Accepted: 13850

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

问题链接POJ3070 Fibonacci
问题简述:(略)
问题分析
????矩阵快速幂的模板题。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* POJ3070 Fibonacci */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int MOD = 1e4;
const int N = 2;

struct Matrix
{
    int m[N][N];
    Matrix() {}
    Matrix operator*(Matrix const &a)const
    {
        Matrix b;
        memset(b.m, 0, sizeof(b.m));
        for (int i = 0 ;i < N; i++)
            for (int j = 0; j < N; j++)
                for (int k = 0; k < N; k++)
                    b.m[i][j] = (b.m[i][j] + this->m[i][k] * a.m[k][j]) % MOD;
        return b;
    }
};

Matrix pow_mod(Matrix a, int n)
{
    Matrix b;
    memset(b.m, 0, sizeof(b.m));
    for (int i = 0; i < N; i++)
        b.m[i][i] = 1;
    while (n > 0)
    {
        if (n & 1) b = b * a;
        a = a * a;
        n >>= 1;
    }
    return b;
}

int main()
{
    Matrix a;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            a.m[i][j] = 1;
    a.m[1][1] = 0;

    int n;
    while (~scanf("%d", &n) && n != -1)
    {
        Matrix b = pow_mod(a, n);
        printf("%d\n", b.m[0][1]);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10261279.html

时间: 2024-10-09 11:18:21

POJ3070 Fibonacci【矩阵快速幂】的相关文章

POJ3070:Fibonacci(矩阵快速幂模板题)

http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 10000 using namespace std; struct m { int a[3][3]; } init,res; int n; m Mult(m x,m

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>

poj 3070 Fibonacci 矩阵快速幂

题目链接:http://poj.org/problem?id=3070 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 t

HDU1588-Gauss Fibonacci(矩阵快速幂+等比数列二分求和)

题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum,并对m取模. 思路: 设A = |(1, 1),(1, 0)| sum = f(b) + f(k + b) + f(2k + b)...+f((n-1)k + b) (f(x) 为Fibonacci数列) sum = A^b + A^(k + b) + A^(2k + b)...+ A^((n-1)k + b) sum = A^b(1 + A^k + A^2k

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

题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int N=2,M=2,P=2; const int MOD=10000; struct Matrix { ll m[N][N]; }; Matrix

UVA10229Modular Fibonacci(矩阵快速幂)

题目链接 题目大意:给你i和m,求Mi, Mi = (F(i - 1) + F(i - 2)) % 2^m; 解题思路:因为Mi = (F(i - 1) % 2^m + F(i - 2)% 2^m) % 2^m = (M(i - 1) + M(i - 2)) % 2^m.类似于求fibonacci数加上取模,只是n很大,所以要用矩阵快速幂快速求解.参考 代码: #include <cstdio> #include <cstring> typedef long long ll; co

$loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\sum_{i=1}^{n}a_i\cdot(n-i)$这样一个式子是可以在矩阵快速幂中推出来的(类似这个形式的都可,,,就随着编号递增系数递减这样子$QwQ$ 具体来说就是表示成$\sum_{i=1}^{n}\sum_{j=1}^{i}a_j$,就欧克辣(具体实现后面港,,, 但是问题在于,它是$\s

POJ 3070 Fibonacci(矩阵快速幂)

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

HDU 3306 Another kind of Fibonacci(矩阵快速幂)

题目地址:HDU 3306 没什么智商的题目,只要把构造矩阵硬算出来就行. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <m

HDU 1588 Gauss Fibonacci(矩阵快速幂)

题目地址:HDU 1588 用于构造斐波那契的矩阵为 1,1 1,0 设这个矩阵为A. sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*k+b) <=>sum=A^b+A^(k+b)+A^(2*k+b)+A^(3*k+b)+........+A^((n-1)*k+b) <=>sum=A^b+A^b*(A^k+A^2*k+A^3*k+.......+A^((n-1)*k))(1) 设矩阵B为A^k; 那么(1)式为 sum=A^b