zoukankan      html  css  js  c++  java
  • POJ

    Fibonacci
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9776   Accepted: 6964

    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:

    .


    
    
    刚刚学习了快速幂和矩阵快速幂,然后就找了个例题,根据题目的一个快速计算斐波那契数列的方法在利用矩阵快速幂模板,0ms水过
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    const int MAXN = 2;
    struct Mat
    {
        int a[MAXN][MAXN];
    } ;
    
    Mat Multi(const Mat x, const Mat y, const int Mod)
    {
        Mat res;
        memset(res.a, 0, sizeof(res.a));
        for(int i = 0; i < MAXN; i++)
            for(int j = 0; j < MAXN; j++)
                for(int k = 0; k < MAXN; k++)
                    res.a[i][j] = (res.a[i][j] + x.a[i][k] * y.a[k][j]) % Mod;
        return res;
    }
    
    Mat Power(Mat base, int k, const int Mod)
    {
        Mat res;
        memset(res.a, 0, sizeof(res.a));
        for(int i = 0; i < MAXN; i++)
            res.a[i][i] = 1;
        while(k > 0) {
            if(k & 1)
                res = Multi(res, base, Mod);
            base = Multi(base, base, Mod);
            k = k >> 1;
        }
        return res;
    }
    
    int main()
    {
        int mod = 10000;
        int n;
        Mat base;
        base.a[0][0] = base.a[1][0] = base.a[0][1] = 1;
        base.a[1][1] = 0;
        while(cin >> n, n!=-1)
        {
            Mat res = Power(base, n, mod);
            printf("%d
    ", res.a[0][1]);
        }
        return 0;
    }
    


  • 相关阅读:
    直击微软第九频道著名主持Robert Green 对话一站式示例代码库大老板梁梅女士
    微软发布中文版一站式示例代码浏览器
    每日一例,练就编程高手
    微软发布Visual Studio 2012 示例代码浏览器
    微软发布Sample Browser for Windows 8版:5000示例代码,"触手可及"
    arthas使用总结
    前端如何生成二维码
    golang的helloworld以及nonmain package的troubleshooting
    监控文件的网页工具
    postfix + courierimap + squirrelMail 邮件服务器
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312748.html
Copyright © 2011-2022 走看看