题目

P1349 广义斐波那契数列

解析

把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了

\[\begin{bmatrix}f_2\\f_1
\end{bmatrix}\begin{bmatrix}
p&q\\
1&0\\
\end{bmatrix}^{n-2}=\begin{bmatrix}f_n\\f_{n-1}
\end{bmatrix}\]

水题

代码

#include <bits/stdc++.h>
#define int long long using namespace std; const int N = 100; int n, m, a1, a2, p, q; struct matrix {
int a[N][N];
matrix() {
memset(a, 0, sizeof a);
} void InitMatrix() {
a[1][1] = p, a[1][2] = q;
a[2][1] = 1;
} matrix operator * (const matrix &oth) const {
matrix ans;
for (int k = 1; k <= 2; ++k)
for (int i = 1; i <= 2; ++i)
for (int j = 1; j <= 2; ++j)
ans.a[i][j] = (ans.a[i][j] + (a[i][k] * oth.a[k][j]) % m) % m;
return ans;
} } init; matrix qpow(matrix a, int b) {
matrix ans = init;
while (b) {
if (b & 1) ans = ans * a;
b >>= 1, a = a * a;
}
return ans;
} template<class T>inline void read(T &x) {
x = 0; int f = 0; char ch = getchar();
for ( ; !isdigit(ch); ch = getchar()) f |= (ch == '-');
for ( ; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
x = f ? -x : x;
return;
} signed main() {
read(p), read(q), read(a1), read(a2), read(n), read(m);
if (n <= 2) {
printf("%lld", n == 1 ? a1 : a2);
return 0;
}
init.InitMatrix();
init = qpow(init, n - 3);
printf("%lld\n", (a2 * init.a[1][1] + a1 * init.a[1][2]) % m);
return 0;
}

P1349 广义斐波那契数列(矩阵乘法)的更多相关文章

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

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

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

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

  3. Codevs 1574 广义斐波那契数列(矩阵乘法)

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

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

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

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

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

  6. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  7. 洛谷——P1349 广义斐波那契数列

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

  8. P1349 广义斐波那契数列

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

  9. Luogu P1349 广义斐波那契数列

    解题思路 既然广义斐波那契,而且数据范围这么大,那么我们使用矩阵快速幂来进行求解.大家都知道斐波那契的初始矩阵如下 $$\begin{bmatrix}1&1\\1&0\end{bmat ...

随机推荐

  1. 三个面向对象相关的装饰器@property@staticmathod@classmethod

    @property 先看实例: from math import pi class Circle: def __init__(self,r): self.r = r @property def per ...

  2. Ubuntu16.04.1上搭建分布式的Redis集群,并使用C#操作

    为什么要集群: 通常为了,提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis是一个很好的Cache工具,当然了还有Memcached,这里只讲Redis.在我们的电 ...

  3. Dubbo自定义Filter统一处理异常

    Dubbo版本:2.7 使用自定义Filter注意事项 1.自定义名称不能和默认Filter相同,否则可能不生效 2.只用定义Filter类和META-INF下的文本文件,不用添加配置,@Activa ...

  4. Why you need to understand garbage collection

    Why you need to understand garbage collection I’ve been interviewing lots of C# developers recently, ...

  5. TX-LCN5.0.2分布式事务框架源码分析-关键线索罗列-txc部分

    1.注解TxcTransaction2.在其注解接口附近查找aop配置:TransactionAspect3.runTransaction是在执行事务业务代码时的包装逻辑4.transactionSe ...

  6. EF获取当天的数据集合

    ).DefaultIfEmpty().Count(); 主要是使用了: DbFunctions.DiffDays

  7. Vintage_坏客户定义

    python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...

  8. django文章对本项目有用的收集

    1.在django中使用自定义标签实现分页功能 https://www.cnblogs.com/MnCu8261/p/5943609.html https://www.cnblogs.com/bail ...

  9. [Linux.centOS].安装Redis 腾讯云

    环境 { "服务器运营商":"腾讯云", "操作系统":"CentOS 7.5 64位", "CPU" ...

  10. MNN配置

    1.github链接:https://github.com/alibaba/MNN/tree/master/tools/converter 2.教程 (1)使用教程:https://www.books ...