• 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;
    }
    
    

     

  • 相关阅读:
    bzoj 1093: [ZJOI2007]最大半连通子图
    bzoj 1266 1266: [AHOI2006]上学路线route
    poj 2104 K-th Number
    洛谷 P3313 [SDOI2014]旅行
    cogs 306. [SGOI] 糊涂的记者
    cogs 1164. 跑步
    洛谷 1821: [JSOI2010]Group 部落划分 Group
    洛谷 U3357 C2-走楼梯
    洛谷 P3014 [USACO11FEB]牛线Cow Line
    洛谷 P2982 [USACO10FEB]慢下来Slowing down
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770759.html
Copyright © 2020-2023  润新知