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

.

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:  .

题意:就是要求第n个Fibonacci数对10000的取余,由于n很大,简单模拟肯定不行,那么就引出了Fibonacci的另一种表示方法(题中已给),根据描述,我们只要求出2*2的矩阵{{1,1},{1,0}}^n就可以了。

由此引出新算法:矩阵快速幂(根据快速幂改编而来,快速幂计算的是数的n次方,而这个求的是矩阵的n次方)。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e4;

typedef long long LL;

struct node
{
    LL m[2][2];
}ans, tmp, cnt;

node Multiply(node a, node b) ///计算两个矩阵相乘之后的矩阵
{
    int i, j, k;

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            cnt.m[i][j] = 0;
            for (k = 0; k < 2; k++)
                cnt.m[i][j] = (cnt.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
        }
    }

    return cnt;
}

LL Matrix_power(LL n)
{
    ans.m[0][0] = ans.m[1][1] = 1;
    ans.m[0][1] = ans.m[1][0] = 0;
    tmp.m[0][0] = tmp.m[0][1] = tmp.m[1][0] = 1;
    tmp.m[1][1] = 0;

    while (n) ///和快速幂求法一致
    {
        if (n % 2 != 0)
            ans = Multiply(ans, tmp);

        tmp = Multiply(tmp, tmp);

        n /= 2;
    }

    return ans.m[0][1]; ///由于第n个Fibonacci数存在于{Fn+1,Fn,Fn,Fn-1}中,所以我们返回(0,1)位置上的元素即可
}

int main ()
{
    LL n, num;

    while (scanf("%lld", &n), n != -1)
    {
        num = Matrix_power(n);
        printf("%lld\n", num);
    }

    return 0;
}
时间: 2024-10-07 07:41:17

POJ 3070 Fibonacci(矩阵快速幂模板)的相关文章

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 (矩阵快速幂乘/模板)

题意:给你一个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

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

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

POJ 3070 Fibonacci 矩阵快速求法

就是Fibonacci的矩阵算法,不过增加一点就是因为数字很大,所以需要取10000模,计算矩阵的时候取模就可以了. 本题数据不强,不过数值本来就限制整数,故此可以0ms秒了. 下面程序十分清晰了,因为分开了几个小函数了,适合初学者参考下. #include <stdio.h> const int MOD = 10000; void mulOneMatrix(int F[2][2]) { int a = F[0][0]; int b = F[1][0]; F[0][0] = (a+b)%MOD

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

3070 Fibonacci 矩阵快速幂

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int m=10000; int fib(int n) { int t[2][2]={1,1,1,0}; int p[2][2]; int a[2][2]={1,0,0,1}; int i,j,k; while(n) { if(n%2==1) { for(i=0;i<2;i++) for(j=0;j

矩阵快速幂模板篇

转载请注明出处:http://blog.csdn.net/u012860063 或许你们看不太懂,纯属自用: 第一种: Description Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n-1) + f(n-2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci seq

51nod1113(矩阵快速幂模板)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: 1 #include <iostream> 2 #define ll long long 3 using namespace std; 4 5 const int mod = 1e9+7; 6 const int MAXN = 1e2+10; 7 int n, m; 8 9 typedef struct

矩阵快速幂 模板

矩阵快速幂模板 1 #include<stdio.h> 2 #include<math.h> 3 #include<set> 4 #include<string.h> 5 using namespace std; 6 int const mod=1e9+7; 7 struct matrix 8 { 9 long long m[3][3]; 10 matrix() 11 { 12 memset(m,0,sizeof(m)); 13 } 14 matrix op