zoukankan      html  css  js  c++  java
  • hust 1333

    题目描述

    We often encounter sequence like this, 0, 1, 1, 2, 3, 5, 8, ... , which is called Fibonacci sequence. It's defined by the recurrence F[0] = 0 F[1] = 1 F[n] = F[n-1] + F[n-2], for n > 1. Now you need to calculate the value of the kth term mod 1000000009 of the sequence defined by the recurrence below. G[0] = u G[1] = v G[n] = a * G[n-1] + b * G[n-2], for n > 1.

    输入

    The input has 30000 cases, one line per case. Each line contains five integers, uvabk. 0<=uvab,k<=1000000009.

    输出

    The value of G[k] mod 1000000009, one line per case.

    样例输入

    0 1 1 1 7
    3 5 2 7 6
    1 2 3 4 5
    

    样例输出

    13
    5879
    614
    又是一道矩阵快速幂的问题,在我们hust上,快速幂的题目还有不少啊!
    #include <iostream>
    #include <cstdio>
    using namespace std;
    const long long mod=1000000009;
    struct Mat
    {
        long long  matrix[2][2];
    };
    Mat Multi(const Mat& a, const Mat& b)
    {
    int i, j, k;
    Mat c;
    for (i = 0; i < 2; i++)
    {
       for (j = 0; j < 2; j++)
       {
        c.matrix[i][j] = 0;
        for (k = 0;k < 2; k++)
         c.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j] % mod;
        c.matrix[i][j] %= mod;
       }
    }
    return c;
    }int main()
    {
        long long  a, b, f[2];
        int n;
        //Mat stand = {0, 1, 0, 0, 0, 1, a3, a2, a1};
        Mat e = {1, 0, 0, 1};
        //scanf("%d", &tot);
        while(scanf("%lld%lld%lld%lld%d",&f[0],&f[1],&a,&b,&n)!=EOF)
        {
             //scanf("%d%d%d%d", &f[0], &f[1], &f[2], &n);
             Mat stand = {0, 1, b, a};
             if (n <= 1)
             {
                  printf("%lld
    ",f[n]);
                  continue;
             }
             Mat ans = e;
             Mat tmp = stand;
             n = n - 1;
             while(n)
             {
                if (n & 1)
                ans = Multi(ans, tmp);
                tmp = Multi(tmp, tmp);
                n >>= 1;
             }
             printf("%lld
    ", (ans.matrix[1][0] * f[0] + ans.matrix[1][1] * f[1] ) % mod);
         }
    return 0;
    }
    至少做到我努力了
  • 相关阅读:
    JavaScript中的几种继承方式对比
    JavaScript垃圾收集-标记清除和引用计数
    PHP安装sqlsrv扩展步骤,PHP如何连接上SQL
    HTML5的应用缓存
    实现跨域请求的4种方法
    JSON的详细介绍
    Ajax的方法和使用代码
    git常用命令
    关于BOM
    DOM之表格与表单基础分享
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3729368.html
Copyright © 2011-2022 走看看