zoukankan      html  css  js  c++  java
  • Hdu1588Gauss Fibonacci矩阵

    题意:求 g(i)=k*i+b;  f(g(i)) for 0<=i<n。

     设 矩阵 A   ,  S = A^0 + A^1 + A^2 + ... + A^n 。

    将这个序列分成两半 ,A^0 + A^1 + A^2 + ... + A^(n/2)      +       A^(n/2 + 1) *(A^0 + ... + A^(n/2))  + A^n  or A^0 + A^1 + A^2 + ... + A^(n/2)      +       A^(n/2 + 1) *(A^0 + ... + A^(n/2))  

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    typedef long long LL;
    LL M;
    using namespace std;
    struct Matrix
    {
        LL m[4][4];
    };
    
    Matrix Mul(Matrix a, Matrix b)
    {
        Matrix ans;
        for (LL i = 0; i < 2; i++)
        for (LL j = 0; j < 2; j++){
            ans.m[i][j] = 0;
            for (LL k = 0; k < 2; k++)
                ans.m[i][j] += a.m[i][k] * b.m[k][j];
            ans.m[i][j] %= M;
        }
        return ans;
    }
    
    Matrix add(Matrix a, Matrix b)
    {
        for (LL i = 0; i < 2; i++)
        for (LL j = 0; j < 2; j++)
            a.m[i][j] += b.m[i][j], a.m[i][j] %= M;
        return a;
    }
    
    Matrix quick(Matrix a, LL b)
    {
        Matrix ans;
        for (LL i = 0; i < 2; i++)
        for (LL j = 0; j < 2; j++)
            ans.m[i][j] = (i == j);
        while (b){
            if (b & 1) ans = Mul(ans, a);
            a = Mul(a, a);
            b >>= 1;
        }
        return ans;
    }
    
    Matrix solve(Matrix a, LL len)
    {
        if (len == 1){
            Matrix ans;
            for (int i = 0; i<2; i++)
            for (int j = 0; j<2; j++)
                ans.m[i][j] = (i == j);
            return ans;
        }
        Matrix ans = solve(a, len >> 1);
        Matrix t = quick(a, (len >> 1));
        t = Mul(t, ans);
        ans = add(ans, t);
        if (len & 1) return add(ans, quick(a, len - 1));
        return ans;
    }
    
    void gao(LL n, LL k, LL b)
    {
        Matrix ans;
        ans.m[0][0] = 1; ans.m[0][1] = 1;
        ans.m[1][0] = 1; ans.m[1][1] = 0;
        Matrix  t = quick(ans, k);
        Matrix gg = solve(t, n);
        gg = Mul(gg, quick(ans, b));
        cout << gg.m[0][1] % M << endl;
    }
    
    int main()
    {
        LL n, k, b;
        while (cin >> k >> b >> n >> M){
            gao(n, k, b);
        }
        return 0;
    }
  • 相关阅读:
    day5模块
    day5时间复杂度
    day5冒泡排序
    day4正则表达式
    C语言 经典编程100题
    C语言 第八章 函数、指针与宏
    C语言 第七章 数组与字符串
    C语言 第六章 多重循环
    C语言 第五章 循环结构
    C语言 第四章 分支结构练习
  • 原文地址:https://www.cnblogs.com/yigexigua/p/4057570.html
Copyright © 2011-2022 走看看