题目

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. 【转】Spring Boot @Condition 注解,组合条件你知道吗

    原文:https://www.cnblogs.com/FraserYu/p/11280420.html 上一篇文章 你应该知道的 @ConfigurationProperties 注解的使用姿势,这一 ...

  2. 虚拟环境上的jupyterhub开机启动设置

    为了让jupyterhub 开机启动,或者以服务的方式启动,折腾了好久.环境 ubuntu 16.04anaconda >= 4.5python35 jupyterhub 0.9.4node 6 ...

  3. Java: 线程池(ThreadPoolExecutor)中的参数说明

    最近在看<阿里巴巴Android开发手册>,里面有这样几句话: [强制]新建线程时,必须通过线程池提供(AsyncTask 或者ThreadPoolExecutor或者其他形式自定义的线程 ...

  4. 时间管理GTD

    时间管理—重要紧急四象限法则https://www.jianshu.com/p/30b2ab0dc20e<番茄工作法图解>—— 让你的工作高效起来https://www.jianshu.c ...

  5. 几个简单易用的IDEA快捷键

    常见的几个Idea的代码快捷键 格式化代码: Ctrl + Alt + L Optimize Imports(优化包引用): Ctrl-Alt-O 单行注释(//): Ctrl-/ 块注释(/*... ...

  6. aardio调用dll

    刚知道aardio这个不错的玩具,可惜作者停更了,贴一个调用dll的例子备用吧 vc代码 extern "C" _declspec(dllexport) int _stdcall ...

  7. Scrapy的 Shell终端

    crapy Shell Scrapy终端是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取的数据. ...

  8. SpringBoot项目从Git拉取代码并完成编译打包启动的sh自动脚本

    操作步骤: 1.进入/home/servers/codes/xxxx-dev/目录,从git上将项目clone下来: 2.确保/usr/local/xxx/xxxx-dev目录存在: 3.确保sh脚本 ...

  9. Kubernetes+Istio

    Kubernetes+Istio   微服务.SpringCloud.k8s.Istio杂谈   一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产 ...

  10. SpringBoot系列教程web篇之重定向

    原文地址: SpringBoot系列教程web篇之重定向 前面介绍了spring web篇数据返回的几种常用姿势,当我们在相应一个http请求时,除了直接返回数据之外,还有另一种常见的case -&g ...