zoukankan      html  css  js  c++  java
  • HDU 1588 Gauss Fibonacci 矩阵

    首先fib数列可以很随意的推出来矩阵解法,然后这里就是要处理一个关于矩阵的等比数列求和的问题,这里有一个logn的解法,类似与这样

    A^0+A^1+A^2+A^3 = A^0 + A^1 + A^2 * (A^0 + A^1) 处理就好了。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <climits>
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef vector<int> VI;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    const int INF = INT_MAX / 3;
    const double eps = 1e-8;
    const LL LINF = 1e17;
    const double DINF = 1e60;
    const int maxn = 6;
    LL k, b, n, mod;
    
    struct Matrix {
        int n, m;
        LL data[maxn][maxn];
        Matrix(int n = 0, int m = 0): n(n), m(m) {
            memset(data, 0, sizeof(data));
        }
    };
    
    Matrix operator * (Matrix a, Matrix b) {
        int n = a.n, m = b.m;
        Matrix ret(n, m);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                for(int k = 1; k <= a.m; k++) {
                    ret.data[i][j] += a.data[i][k] * b.data[k][j];
                    ret.data[i][j] %= mod;
                }
            }
        }
        return ret;
    }
    
    Matrix operator + (Matrix a, Matrix b) {
        for(int i = 1; i <= a.n; i++) {
            for(int j = 1; j <= a.m; j++) {
                a.data[i][j] += b.data[i][j];
                a.data[i][j] %= mod;
            }
        }
        return a;
    }
    
    Matrix pow(Matrix mat, LL k) {
        if(k == 0) {
            Matrix ret(mat.n, mat.m);
            for(int i = 1; i <= mat.n; i++) ret.data[i][i] = i;
            return ret;
        }
        if(k == 1) return mat;
        Matrix ret = pow(mat * mat, k / 2);
        if(k & 1) ret = ret * mat;
        return ret;
    }
    
    Matrix calc(Matrix mat, LL p) {
        if(p == 0) return pow(mat, b);
        if(p == 1) return pow(mat, b) + pow(mat, k + b);
        int midval = (p + 1) % 2 == 0 ? (p / 2) : (p / 2) - 1;
        Matrix ret = calc(mat, midval);
        ret = ret + pow(mat, (midval + 1) * k) * ret;
        if((p + 1) & 1) ret = ret + pow(mat, p * k + b);
        return ret;
    }
    
    int main() {
        while(cin >> k >> b >> n >> mod) {
            Matrix A(2, 2);
            A.data[1][1] = A.data[1][2] = A.data[2][1] = 1;
            A.data[2][2] = 0;
            Matrix ans = calc(A, n - 1);
            cout << ans.data[2][1] << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    nmcli 命令的基本使用
    kvm虚拟机添加网卡
    Centos 7 系统详解
    Android 开发工具下载
    Android Studio升级3.6 Build窗口出现中文乱码问题解决方案
    Android 透明度alpha换算16进制
    python 安装: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    关于AndroidStudio 配置的默认路径的修改
    通过设置代理解决AndroidStudio无法下载gradle问题
    机器学习ROC图解读
  • 原文地址:https://www.cnblogs.com/rolight/p/4048871.html
Copyright © 2011-2022 走看看