zoukankan      html  css  js  c++  java
  • Fibnoccia 数列简单题

    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:

    .

    题目:一个矩阵快速幂就可以

    代码示例:

    #define ll long long
    const ll maxn = 1e6+5;
    const ll mod = 1e4;
    const double eps = 1e-9;
    const double pi = acos(-1.0);
    const ll inf = 0x3f3f3f3f;
    
    ll n;
    struct mat
    {
        ll a[2][2];
    };
    
    mat mul(mat A, mat B){
        mat r;
        memset(r.a, 0, sizeof(r.a));
        
        for(ll i = 0; i < 2; i++){
            for(ll j = 0; j < 2; j++){
                for(ll k = 0; k < 2; k++){
                    r.a[i][j] += (A.a[i][k]*B.a[k][j])%mod;
                    r.a[i][j] %= mod;
                }
            }
        }
        return r;
    }
    
    mat qpow(mat A, ll x){
        mat B;
        B.a[0][0] = B.a[1][1] = 1; // 单位矩阵
        B.a[0][1] = B.a[1][0] = 0;
        
        while(x){
            if (x&1) B = mul(B, A);
            A = mul(A, A);
            x >>= 1;
        }
        return B;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        
        while(~scanf("%lld", &n)){
            if (n == -1) break;
            
            mat a;
            a.a[0][0] = a.a[0][1] = a.a[1][0] = 1;
            a.a[1][1] = 0;
            
            if (n == 0) printf("0
    ");
            else if (n == 1) printf("1
    ");
            else {
                a = qpow(a, n-1);
                printf("%d
    ", a.a[0][0]%mod);
            }
            
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    【BZOJ1067】【SCOI2007】降雨量(线段树)
    【BZOJ3489】A simple rmq problem(树套树)
    【BZOJ1146】【CTSC2008】网络管理
    【BZOJ3236】【Ahoi2013】作业
    计算几何的一些板
    【BZOJ3173】【Tjoi2013】最长上升子序列(树状数组)
    解决phpmyadmin导入长脚本超时
    make报错make: *** [sapi/cli/php] Error 1
    wampserver配置redis在phpinfo()里面找不到
    阿里云服务器安装Apache环境外网不能访问
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8973153.html
Copyright © 2011-2022 走看看