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;
}

  

时间: 2024-11-08 20:26:21

HDU 1588 Gauss Fibonacci 矩阵的相关文章

HDU 1588 Gauss Fibonacci(矩阵快速幂)

题目地址:HDU 1588 用于构造斐波那契的矩阵为 1,1 1,0 设这个矩阵为A. sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*k+b) <=>sum=A^b+A^(k+b)+A^(2*k+b)+A^(3*k+b)+........+A^((n-1)*k+b) <=>sum=A^b+A^b*(A^k+A^2*k+A^3*k+.......+A^((n-1)*k))(1) 设矩阵B为A^k; 那么(1)式为 sum=A^b

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

矩阵十题【三】 HDU 1588 Gauss Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1588 题目大意:先要知道一组斐波那契数列 i 0 1 2 3 4 5 6 7 f(i) 0 1 1 2 3 5 8 13 下面给你一组数: k,b,n,M 现在知道一组公式g(i)=k*i+b:(i=0,1,2,3...n-1) 让你求出 f(g(i)) 的总和(i=01,2,3,...,n-1),比如给出的数据是2 1 4 100 2*0+1=1   f(1)=1 2*1+1=3   f(3)=2

HDU - 1588 Gauss Fibonacci (矩阵快速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 1588 Gauss Fibonacci(矩阵快速幂)

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2090    Accepted Submission(s): 903 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r

hdu 1588 Gauss Fibonacci(等比矩阵二分求和)

题目链接:hdu 1588 Gauss Fibonacci 题意: g(i)=k*i+b; f(0)=0f(1)=1f(n)=f(n-1)+f(n-2) (n>=2) 让你求:sum(f(g(i)))for 0<=i<n 题解: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,0,1}, (花括号表示矩阵...) // 也就是fib数的变化矩阵,F(x) = (A^x) * {1,0} = F(b) + (

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

hdu 1588 Gauss Fibonacci(矩阵嵌矩阵)

题目大意: 求出斐波那契中的 第 k*i+b 项的和. 思路分析: 定义斐波那契数列的矩阵 f(n)为斐波那契第n项 F(n) = f(n+1) f(n) 那么可以知道矩阵 A = 1 1 1  0 使得 F(n) = A * F(n+1) 然后我们化简最后的答案 sum = F(b) +   F(K+b) +  F (2*k +b).... sum = F(b) +  A^k F(b)    +   A^2k F(b)..... sum = (E+A^k + A^2k.....)*F(b) 那

HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob