zoukankan      html  css  js  c++  java
  • poj3070 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<string>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int maxn = 30;
    ll sz,mod,n,f[maxn],a[maxn],ans[maxn];
    struct mtx{
        ll v[maxn][maxn];
        void cler(){
            memset(v,0,sizeof(v));
        }
        mtx mul(mtx A,mtx B){
            mtx C;
            C.cler();
            for(int i = 1;i <= sz;i++){
                for(int j = 1;j <= sz;j++){
                    for(int k = 1;k <= sz;k++){
                        C.v[i][j] = (C.v[i][j] + A.v[i][k] * B.v[k][j]) % mod;
                    }
                }
            }
            return C;
        }
        mtx mi(mtx A,int n){
            mtx R;
            R.cler();
            for(int i = 1;i <= sz;i++) R.v[i][i] = 1;
            while(n){
                if(n&1) R = R.mul(R,A);
                n >>= 1;
                A = A.mul(A,A);
            }
            return R;
        }
        void get_tr(mtx A){
            memset(ans,0,sizeof(ans));
            for(int i = 1;i <= sz;i++){
                for(int j = 1;j <= sz;j++){
                    ans[j] = (ans[j] + f[i] * A.v[i][j]) % mod;
                }
            }
        }
    };
    int main(){
        sz = 2;
        mod = 10000;
        f[1] = f[2] = 1;
        a[1] = a[2] = 1;
        mtx A;
        while(1){    
            cin>>n;
            if(n == -1){
                return 0;
            }
            if(n <= 2){
                if(n==0) cout<<0<<endl;
                if(n==1||n==2) cout<<1<<endl;
                continue;
            }
            A.cler();
            for(int i = 1;i <= sz;i++) A.v[i][1] = a[i];
            for(int i = 2;i <= sz;i++) A.v[i-1][i] = 1;
            A = A.mi(A,n-sz);
            A.get_tr(A);
            cout<<ans[1]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    leetcode@ [279]Perfect Squares
    leetcode@ [160]Intersection of Two Linked Lists
    leetcode@ [209]Minimum Size Subarray Sum
    leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
    jmeter—JDBC request动态参数设置
    Jmeter JDBC Request--测试数据库连接 拒绝解决方案
    转JMeter ----数据库 not allowed to connect to this MySQL
    jmeter ---监控服务器CPU, 内存,网络数据
    在free bsd上跑JMeter 的 plugin "PerfMon Server Agent"
    解决Jmeter插件ERROR: java.io.IOException: Agent is unreachable via TCP的错误
  • 原文地址:https://www.cnblogs.com/hyfer/p/5974515.html
Copyright © 2011-2022 走看看