更好的阅读体验

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

「Luogu 1349」广义斐波那契数列的更多相关文章

  1. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

  2. 洛谷P1349 广义斐波那契数列(矩阵快速幂)

    P1349 广义斐波那契数列 https://www.luogu.org/problemnew/show/P1349 题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定 ...

  3. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

  4. 矩阵乘法快速幂 codevs 1574 广义斐波那契数列

    codevs 1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 广义的斐波那契数列是指形如 ...

  5. HDU 5451 广义斐波那契数列

    这道题目可以先转化: 令f(1) = 5+2√6 f(2) = f(1)*(5+2√6) ... f(n) = f(n-1)*(5+2√6) f(n) = f(n-1)*(10-(5-2√6)) = ...

  6. P1349 广义斐波那契数列(矩阵加速)

    P1349 广义斐波那契数列 题目描述 广义的斐波那契数列是指形如an=pan-1+qan-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an ...

  7. 洛谷——P1349 广义斐波那契数列(矩阵加速)

    P1349 广义斐波那契数列 题目描述 广义的斐波那契数列是指形如$an=p\times a_{n-1}+q\times a_{n-2}$?的数列.今给定数列的两系数$p$和$q$,以及数列的最前两项 ...

  8. codevs1574广义斐波那契数列

    1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 广义的斐波那契数列是指形如an=p* ...

  9. P1349 广义斐波那契数列(矩阵乘法)

    题目 P1349 广义斐波那契数列 解析 把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了 \[\begin{bmatrix}f_2\\f_1 \end{bmatrix}\begin{bmatr ...

随机推荐

  1. PCA 算法核心:高维度向量向低维度投影

    Principal Component Analysis:主成分分析 步骤 5 步: 1.去平均值,也就是将向量中每一项都减去各自向量的平均值 2.计算矩阵的方差,协方差,特征值, 3,.把特征值从大 ...

  2. 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离

    分库分表(6)--- ShardingSphere实现分表+ 读写分离 有关分库分表前面写了五篇博客: 1.分库分表(1) --- 理论 2.分库分表(2) --- ShardingSphere(理论 ...

  3. Spring中@Import的各种用法以及ImportAware接口

    @Import 注解 @Import注解提供了和XML中<import/>元素等价的功能,实现导入的一个或多个配置类.@Import即可以在类上使用,也可以作为元注解使用. @Target ...

  4. BZOJ 4597: [Shoi2016]随机序列

    4597: [Shoi2016]随机序列 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 255  Solved: 174[Submit][Status ...

  5. uniapp 与C# 加解密

    1 uni-app操作 (1) 打开HBuilderX的视图->显示终端 cd 切换到你项目的根目录 执行命令 npm install crypto-js 安装成功后你的项目根目录会生成node ...

  6. LeetCode初级算法--字符串01:反转字符串

    LeetCode初级算法--字符串01:反转字符串 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  7. 第3章(2) Linux下C编程风格

    Linux内核编码风格在内核源代码的Documentation/CodingStyle目录下(新版本内核在Documentation/process/coding-style.rst). 变量命名采用 ...

  8. Meterpreter后渗透之信息收集

    在获得目标的Meterpreter shell后 进行信息收集是后渗透工作的基础 记录一下关于meterpreter 信息收集的使用 环境: kali linux 192.168.190.141 xp ...

  9. javascript input只输入数字和字母

    <input type="text" placeholder="请输入您的用户名..."> <script type="text/j ...

  10. 利用WinRM实现内网无文件攻击反弹shell

    利用WinRM实现内网无文件攻击反弹shell 原文转自:https://www.freebuf.com/column/212749.html 前言 WinRM是Windows Remote Mana ...