[POJ3070] Fibonacci|矩阵乘法

Fibonacci

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11024   Accepted: 7846

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

第一次写矩阵快速幂……留着当模板吧。

#include<iostream>
#include<cstdio>
using namespace std;
int n,a[2][2],b[2][2];
void mul(int a[2][2],int b[2][2],int ans[2][2])
{
    int t[2][2];
    for (int i=0;i<=1;i++)
        for (int j=0;j<=1;j++)
        {
            t[i][j]=0;
            for (int k=0;k<=1;k++)
                t[i][j]=(t[i][j]+a[i][k]*b[k][j])%10000;
        }
    for (int i=0;i<=1;i++)
        for (int j=0;j<=1;j++)
            ans[i][j]=t[i][j];
}
int main()
{
    while (scanf("%d",&n))
    {
        if (n==-1) return 0;
        a[0][0]=a[0][1]=a[1][0]=1; a[1][1]=0;
        b[0][0]=b[1][1]=1; b[0][1]=b[1][0]=0;
        while (n)
        {
            if (n&1) mul(a,b,b);
            n>>=1;
            mul(a,a,a);
        }
        printf("%d\n",b[1][0]);
    }
}
时间: 2025-01-03 16:50:34

[POJ3070] Fibonacci|矩阵乘法的相关文章

【poj3070】矩阵乘法求斐波那契数列

[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [分析] 这是我们熟悉的斐波那契数列,原来呢我们是递推求值的嘛,当然这是最水的想法~~可是!这里的n很大诶,有10^9,for一遍肯定是不可以的咯. 于是,我学会了用矩阵乘法求斐波那契数列(貌似是很经典的). 作为初学者的我觉得十分神奇!! 好,我们来看: 我们每次存两个数f[i-1]和f[i-2],

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

Fibonacci数列--矩阵乘法优化

Fibonacci数列 题目描述 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30000. 输入描述 第一行一个数T(1<=T<=10000). 以下T行,每行两个数,n,q(n<=109, 1<=q<=30000) 输出描述 文件包含T行,每行对应一个答案. 样例输入 3 6 2 7 3 7 11 样例输出 1 0 10 数据范围及提示 1<=T<

矩阵十题【六】 poj3070 Fibonacci

题目链接:http://poj.org/problem?id=3070 题目大意:给定n和10000,求第n个Fibonacci数mod 10000 的值,n不超过2^31.结果保留四位数字. 很简单的题,和之前做过的相比简单很多了. 构造最简单的斐波那契数列矩阵. #include<iostream> #include<cstring> #include<stdio.h> using namespace std; const int MAX = 2; struct M

矩阵乘法快速幂 codevs 1250 Fibonacci数列

codevs 1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30000. 输入描述 Input Description 第一行一个数T(1<=T<=10000). 以下T行,每行两个数,n,q(n<=109, 1<

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 a

CODEVS1533 Fibonacci数列 (矩阵乘法)

嗯,,,矩阵乘法最基础的题了. Program CODEVS1250; type arr=array[1..2,1..2] of longint; var T,n,mo:longint; a,b:arr; operator *(a,b:arr) c:arr; var i,j,k,sum:longint; begin fillchar(c,sizeof(c),0); for i:=1 to 2 do for j:=1 to 2 do begin sum:=0; for k:=1 to 2 do s

POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】

题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #include <iostream> #include <cstring> using namespace std; const int MOD = 10000; void mul(int f[2], int base[2][2]) { int c[2]; memset(c, 0, sizeof

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

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