• poj


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13732   Accepted: 9728

    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 <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 100 + 10;
    int n, mod;
    LL m;
    struct Matrix {
        LL m[3][3];
        int row, col;
    };
    Matrix ori, res;
    void init() {
        memset(res.m, 0, sizeof(res.m));
        ori.m[1][1] = ori.m[1][2] = ori.m[2][1] = 1;
    }
    Matrix multi(Matrix x, Matrix y) {
        Matrix z;
        memset(z.m, 0, sizeof(z.m));
        for (int i = 1; i <= 2; i++) {
            for (int k = 1; k <= 2; k++) {
                for (int j = 1; j <= 2; j++) {
                    z.m[i][j] += x.m[i][k]*y.m[k][j]%mod;
                }
                z.m[i][k] %= mod;
            }
        }
        return z;
    }
    Matrix pow_mod(Matrix a, LL x){
        Matrix b;
        memset(b.m, 0, sizeof(b.m));
        for (int i = 1; i <= 2; i++) {
            b.m[i][i] = 1;
        }
        while(x){
            if(x&1) b = multi(a,b);
            a = multi(a, a);
            x >>= 1;
        }
        return b;
    }
    int main() {
        while (scanf("%lld", &m), m != -1) {
            init(); mod = 10000;
            res = pow_mod(ori, m);
            printf("%lld
    ", res.m[1][2]%mod);
        }
        return 0;
    }
    
    

     

  • 相关阅读:
    从 OKR 工作法到 OKRs-E,落地OKR不能错过的转变
    OKR : 不要让目标仅仅成为口号
    [MySQL]Software caused connection abort: recv failed 问题分析与解决
    VS2010 vcpkgsrv.exe进程CPU占用高的解决方法
    Lync Server 2013企业版部署系列之九:前端部署后续工作
    Lync Server 2013企业版部署系列之八:安装lync server系统
    Lync Server 2013企业版部署系列之七:使用拓扑生成器规划拓扑
    Lync Server 2013企业版部署系列之六:AD准备
    Lync Server 2013企业版部署系列之五:前端服务器软件准备
    Lync Server 2013企业版部署系列之四:SQL准备
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770759.html
Copyright © 2020-2023  润新知