矩阵快速幂 [POJ 3070 NYOJ 148] Fibonacci

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:

.

纯模板题、- -

#include <iostream>
#include <cstdio>
#include <time.h>
using namespace std;
#define MOD 10000
#define N 2

void mul(int a[N][N],int b[N][N])
{
    int i,j,k;
    int c[N][N]={0};
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            for(k=0;k<N;k++)
            {
                c[i][j]=(c[i][j]+a[i][k]*b[k][j])%MOD;
            }
        }
    }
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            a[i][j]=c[i][j];
        }
    }
}
int main()
{
    int n;
    while(scanf("%d",&n),n+1)
    {
        int a[N][N]={{0},{1}},b[N][N]={{1,1},{1,0}};     //首项和递推矩阵
        while(n)                                         //二分快速幂
        {
            if(n&1) mul(a,b);
            mul(b,b);
            n>>=1;
        }
        printf("%d\n",a[1][1]);
    }
    return 0;
}
时间: 2024-10-11 00:46:50

矩阵快速幂 [POJ 3070 NYOJ 148] Fibonacci的相关文章

矩阵快速幂 POJ 3070 Fibonacci

题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f;

矩阵快速幂 poj 3070

#include <iostream> #include <cstdio> using namespace std; const int MOD = 10000; struct Matrix { int m[2][2]; }; Matrix Mul(Matrix a, Matrix b) { Matrix tmp; for(int i=0; i<2; i++) for(int j=0; j<2; j++) { tmp.m[i][j] = 0; for(int k=0;

矩阵快速幂 POJ 3735 Training little cats

题目传送门 1 /* 2 题意:k次操作,g:i猫+1, e:i猫eat,s:swap 3 矩阵快速幂:写个转置矩阵,将k次操作写在第0行,定义A = {1,0, 0, 0...}除了第一个外其他是猫的初始值 4 自己讲太麻烦了,网上有人讲的很清楚,膜拜之 5 详细解释:http://www.cppblog.com/y346491470/articles/157284.html 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #inc

矩阵快速幂——POJ - 3735

题目链接 题目含义 对于n只猫,现在我们有g,e,s三种操作 g是让第a只猫得到一个花生 e是让第a只猫的花生全部没有 s是让第a只猫和第b只猫的花生互换 一共有K次操作,这还不算完 要我们重复m次这些操作后,得出的每只猫的花生个数 题目分析 如果不用重复m次操作的话,这道题可以说十分简单 但如果要重复m次,尤其m是个很大的数,那我们就要需要一个幂矩阵代替m 让每次的状态乘以幂矩阵就变成下一次的状态 而这个幂矩阵其实就是单位矩阵的变形,跟线性代数里的初等矩阵差不多 题目代码 #include<i

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 (矩阵快速幂求斐波那契数列的第n项)

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

POJ 3070 Fibonacci(矩阵快速幂)

题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. 1 //3070 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 struct Matrix 9 { 10 int v[2][2]; 11 }; 12 int n; 13 14 Matrix matrix_mul(Matrix a,Matrix b) 1

【POJ 3070】Fibonacci(矩阵快速幂)

[POJ 3070]Fibonacci(矩阵快速幂) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12333   Accepted: 8752 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

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