HDU - 1588 -- Gauss Fibonacci【矩阵快速幂升级版,如何构造核心矩阵】

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 “Problem GF” told by Angel is actually “Gauss Fibonacci”.
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.

Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.

Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)

The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.

Input

The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.

Output

For each line input, out the value described above.

Sample Input

2 1 4 100
2 0 4 100

Sample Output

21
12

思路:

在这里插入图片描述

如果你不会构造核心矩阵,那么这篇文章将会帮助你如何构造核心矩阵

AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Mat{
    ll m[10][10];
}A, B, D, FB, TK, T;
int k, b, n, M;
// 矩阵a与矩阵b相乘
Mat Mult(Mat a, Mat b) {
    Mat res;
    memset(res.m, 0, sizeof(res.m));
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            for (int k = 0; k < 10; ++k) {
                res.m[i][j] = (res.m[i][j] + a.m[i][k] * b.m[k][j]) % M;
            }
        }
    }
    return res;
}
Mat fast_power(Mat a, ll b) {
    Mat res;
    memset(res.m, 0, sizeof(res.m));
    for (int i = 0; i < 10; ++i) res.m[i][i] = 1;
    while(b > 0) {
        if (b & 1) {
            res = Mult(res, a);
        }
        b >>= 1;
        a = Mult(a, a);
    }
    return res;
}
// T,FB,TK,D, B。
void init() {
    T.m[0][0] = 0;  T.m[0][1] = 1;  T.m[1][0] = 1;  T.m[1][1] = 1;
    A.m[0][0] = 0; A.m[0][1] = 1;
    FB = Mult(A, fast_power(T, b));
    TK = fast_power(T, k);
    for (int i = 0; i < 2; ++i) {
        D.m[i][i] = 1;
        B.m[i][i] = B.m[i][i + 2] = 1;
    }
    for (int i = 2; i < 4; ++i) {
        for (int j = 0; j < 2; ++j) {
            D.m[i][j] = D.m[i][j + 2] = TK.m[i - 2][j];
        }
    }
}
void solve() {
    while(scanf("%d%d%d%d", &k, &b, &n, &M) != EOF) {
        init();
        Mat tmp = Mult(B, fast_power(D, n - 1));
        Mat sum;
        memset(sum.m, 0, sizeof(sum.m));
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                sum.m[i][j] = tmp.m[i][j];
            }
        }
        Mat ans = Mult(FB, sum);
        printf("%lld\n", ans.m[0][0]);
    }
}
int main() {
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值