1 条题解

  • 0
    @ 2025-8-24 21:24:04

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar 奔波儿霸
    **

    搬运于2025-08-24 21:24:03,当前版本为作者最后更新于2018-08-06 08:56:53,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    解题思路

    既然广义斐波那契,而且数据范围这么大,那么我们使用矩阵快速幂来进行求解。大家都知道斐波那契的初始矩阵如下

    [1110]\begin{bmatrix}1&1\\1&0\end{bmatrix}

    那么这道题我们怎么推矩阵呢?先确定目标矩阵如下

    [FnFn1]\begin{bmatrix} F_n & F_{n-1}\end{bmatrix}

    然后推导过程如下:

    Fn=p×Fn1+q×Fn2F_n = p\times F_{n-1} + q\times F_{n-2} \downarrow $$\begin{bmatrix}F_{n-1}&F_{n-2}\end{bmatrix}\times\begin{bmatrix}p&1\\q&0\end{bmatrix} $$\downarrow $$\begin{bmatrix}(p\times F_{n-1}+q\times F_{n-2})&(F_{n-1}\times 1+F_{n-2}\times0)\end{bmatrix} $$\downarrow [FnFn1]\begin{bmatrix} F_n & F_{n-1}\end{bmatrix}

    吐槽一下

    你谷的公式渲染不咋地,我写的老长老长的公式自己默认换行,无法显示QAQ

    建议你谷更新一下QWQ

    附上代码

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    typedef long long LL;
    LL n, Mod, p, q, a1, a2, ans;
    struct mat{
    	LL m[4][4];
    }Ans, base;
    inline void init() {
    	Ans.m[1][1] = a2, Ans.m[1][2] = a1;
    	base.m[1][1] = p, base.m[2][1] = q, base.m[1][2] = 1;
    }
    inline mat mul(mat a, mat b) {
    	mat res;
    	memset(res.m, 0, sizeof(res.m));
    	for(int i=1; i<=2; i++) {
    		for(int j=1; j<=2; j++) {
    			for(int k=1; k<=2; k++) {
    				res.m[i][j] += (a.m[i][k] % Mod) * (b.m[k][j] % Mod);
    				res.m[i][j] %= Mod;
    			}
    		}
    	}
    	return res;
    }
    inline void Qmat_pow(int p) {
    	while (p) {
    		if(p & 1) Ans = mul(Ans, base);
    		base = mul(base, base);
    		p >>= 1;
    	}
    }
    
    int main() {
    	scanf("%lld%lld%lld%lld%lld%lld", &p, &q, &a1, &a2, &n, &Mod);
    	if(n == 1) {
    		cout<<a1;
    		return 0;
    	}
    	if(n == 2) {
    		cout<<a2;
    		return 0;
    	}
    	init();
    	Qmat_pow(n-2);
    	ans = Ans.m[1][1];
    	ans %= Mod;
    	printf("%lld", ans);
    }
    
    • 1

    信息

    ID
    347
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者