1. 程式人生 > >「Luogu 1349」廣義斐波那契數列

「Luogu 1349」廣義斐波那契數列

更好的閱讀體驗

Portal

Portal1: Luogu

Description

廣義的斐波那契數列是指形如\(an=p \times a_{n-1}+q \times a_{n-2}\)的數列。今給定數列的兩係數\(p\)和\(q\),以及數列的最前兩項\(a_1\)和\(a_2\),另給出兩個整數\(n\)和\(m\),試求數列的第\(n\)項\(a_n\)除以\(m\)的餘數。

Input

輸入包含一行6個整數。依次是\(p\),\(q\),\(a_1\),\(a_2\),\(n\),\(m\),其中在\(p\),\(q\),\(a_1\),\(a_2\)整數範圍內,\(n\)和\(m\)在長整數範圍內。

Output

輸出包含一行一個整數,即\(a_n\)除以\(m\)的餘數。

Sample Input

1 1 1 1 10 7

Sample Output

6

Hint

數列第\(10\)項是\(55\),除以\(7\)的餘數為\(6\)。

Solution

基本斐波那契數列矩陣是\(T = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}\);

廣義斐波那契數列矩陣是\(F = \begin{bmatrix} p & 1 \\ q & 0 \end{bmatrix}\)。

那麼要求的就是:

\[\begin{aligned} F_i & = F_{i - 1} \times T \\\\ & = \begin{bmatrix} f_{i - 1} & f_{i - 2} \\ 0 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \\\\ & = \begin{bmatrix} f_{i - 1} + f_{i - 2} & f_{i - 1} \\ 0 & 0 \end{bmatrix} \\\\ & = \begin{bmatrix} f_i & f_{i - 1} \\ 0 & 0 \end{bmatrix} \end{aligned}\]

然後就可以用矩陣快速冪來解決了。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;

struct Matrix {
    LL a[2][2];
    inline void clear() {//矩陣清空
        memset(a, 0, sizeof(a));
    }
    inline void init() {//單位矩陣
        memset(a, 0, sizeof(a));
        for (int i = 0; i < 2; i++)
            a[i][i] = 1;
    }
};
LL n, p, q, a1, a2, mod;
Matrix F, a, ans;
inline LL Plus(LL x, LL y) {
    x += y;
    if (x >= mod) x -= mod;
    return x;
}
inline LL power(LL x, LL y) {//快速冪
    LL ret = 0;
    while (y) {
        if (y & 1) ret = (ret + x) % mod;
        x = (x + x) % mod;
        y >>= 1;
    }
    return ret;
}
Matrix operator * (Matrix a, Matrix b) {//矩陣乘法
    Matrix ret;
    ret.clear();
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            for (int k = 0; k < 2; k++)
                ret.a[i][j] = Plus(ret.a[i][j] % mod, power(a.a[i][k], b.a[k][j])% mod) % mod;
    return ret;
}
inline Matrix Matrix_Power(Matrix a, LL x) {//矩陣快速冪
    Matrix ret;
    ret.init();
    while (x) {
        if (x & 1) ret = ret * a;
        x >>= 1;
        a = a * a;
    }
    return ret;
}
int main() {
    scanf("%lld%lld%lld%lld%lld%lld", &q, &p, &a1, &a2, &n, &mod);
    F.a[0][0] = a1, F.a[0][1] = a2;
    a.a[0][0] = 0, a.a[1][0] = 1, a.a[0][1] = p; a.a[1][1] = q;
    ans = F * Matrix_Power(a, n - 2);
    printf("%lld\n", ans.a[0][1] % mod);
    return 0;
}