首页 > 其他 > 详细

Fibonacci (poj 3070 矩阵快速幂)

时间:2015-03-28 08:52:17      阅读:206      评论:0      收藏:0      [点我收藏+]

Language:
Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10099   Accepted: 7211

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

题意:求非波拉契数列第n项mod10000

思路:数据太大,用到矩阵快速幂。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 10000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

typedef vector<ll>vec;
typedef vector<vec>mat;

ll n;

mat mul(mat &A,mat &B)
{
    mat C(A.size(),vec(B[0].size()));
    for (int i=0;i<A.size();i++)
    {
        for (int k=0;k<B.size();k++)
        {
            for (int j=0;j<B[0].size();j++)
                C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
        }
    }
    return C;
}

mat pow(mat A,ll n)
{
    mat B(A.size(),vec(A.size()));
    for (int i=0;i<A.size();i++)
        B[i][i]=1;
    while (n>0)
    {
        if (n&1) B=mul(B,A);
        A=mul(A,A);
        n>>=1;
    }
    return B;
}

void solve()
{
    mat A(2,vec(2));
    A[0][0]=1;A[0][1]=1;
    A[1][0]=1;A[1][1]=0;
    A=pow(A,n);
    pf("%lld\n",(A[1][0]%mod));
}

int main()
{
    int i,j;
    while (scanf("%lld",&n))
    {
        if (n==-1) break;
        solve();
    }
    return 0;
}


Fibonacci (poj 3070 矩阵快速幂)

原文:http://blog.csdn.net/u014422052/article/details/44684711

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!