题意:

给出一个序列,

\(f_n=\left\{\begin{matrix}
2f_{n-1}+1, n \, mod \, 2=1\\
2f_{n-1}, n \, mod \, 2=0
\end{matrix}\right.\)

求\(f_n \, mod \, m\)的值。

分析:

我们可以两个两个的递推,这样就避免了奇偶讨论了。

$\begin{bmatrix}

0 & 2 & 1 \

0 & 4 & 2\

0 & 0 & 1

\end{bmatrix}

\begin{bmatrix}

f_1\

f_2\

1

\end{bmatrix}

\begin{bmatrix}

f_3\

f_4\

1

\end{bmatrix}$

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL; LL n, MOD; LL mul_mod(LL a, LL b) { return a * b % MOD; } LL add_mod(LL& a, LL b) { a += b; if(a >= MOD) a -= MOD; } struct Matrix
{
LL a[3][3];
Matrix() { memset(a, 0, sizeof(a)); }
Matrix operator * (const Matrix& t) const {
Matrix ans;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 3; k++)
add_mod(ans.a[i][j], mul_mod(a[i][k], t.a[k][j]));
return ans;
}
}; Matrix pow_mod(Matrix a, LL n) {
Matrix ans;
for(int i = 0; i < 3; i++) ans.a[i][i] = 1;
while(n) {
if(n & 1) ans = ans * a;
a = a * a;
n >>= 1;
}
return ans;
} int main()
{
LL a0[3], a[3];
a0[0] = a0[2] = 1; a0[1] = 2;
Matrix M0;
M0.a[0][1] = 2; M0.a[1][1] = 4;
M0.a[0][2] = 1; M0.a[2][2] = 1;
M0.a[1][2] = 2; while(scanf("%lld%lld", &n, &MOD) == 2) {
Matrix M;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
M.a[i][j] = M0.a[i][j] % MOD;
for(int i = 0; i < 3; i++)
a[i] = a0[i] % MOD; M = pow_mod(M, (n - 1) / 2);
int x = ((n & 1) ^ 1);
LL ans = 0;
for(int i = 0; i < 3; i++)
add_mod(ans, mul_mod(M.a[x][i], a[i]));
printf("%lld\n", ans);
} return 0;
}

HDU 4990 Reading comprehension 矩阵快速幂的更多相关文章

  1. hdu 4990 Reading comprehension 二分 + 快速幂

    Description Read the program below carefully then answer the question. #pragma comment(linker, " ...

  2. hdu4990 Reading comprehension 矩阵快速幂

    Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...

  3. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  4. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  5. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  6. HDU 4990 Reading comprehension 简单矩阵快速幂

    Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...

  7. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  8. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  9. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

随机推荐

  1. Kendo 单页面应用(一)概述

    Kendo 单页面应用(一)概述 Kendo 单页面应用(Single-Page Application,缩写为 SPA)定义了一组类用于简化 Web 应用(Rich Client)开发,最常见的单页 ...

  2. 零基础逆向工程23_PE结构07_重定位表_IAT表(待补充)

    重定位表 待补充 IAT表 待补充

  3. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:4.技术简介之Spring

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Spring是一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...

  4. {Linux} boot仅剩余XX字节

    1. 查看已安装的linux-image各版本 dpkg --get-selections |grep linux-image   2. 查看我们当前使用的是哪一个版本: uname -a    3. ...

  5. ALTER AVAILABILITY GROUP (Transact-SQL)

    更改 SQL Server 中现有的 AlwaysOn 可用性组.              只有当前主副本支持大多数 ALTER AVAILABILITY GROUP 参数. 但是,只有辅助副本支持 ...

  6. MATLAB中容易忽略却经常遇到的小技巧总结

    1       如何产生一个列向量相同的矩阵 例如,列向量x=[1;2;3],要产生矩阵A=[x,x,x],即[1,1,1;2,2,2;3,3,3]. A = repmat(x,1,n) 2      ...

  7. RYU的GUI安装

    1. RYU安装 Ubuntu14.04 LTS 1.sudo apt-get install git python-pip libxml2-dev libxslt1-dev python2.7-de ...

  8. ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

    Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi ...

  9. guruguru

    6576: guruguru 时间限制: 1 Sec  内存限制: 128 MB提交: 28  解决: 12[提交] [状态] [讨论版] [命题人:admin] 题目描述 Snuke is buyi ...

  10. WPF DataGridCheckBoxColumn需要点两次才能修改checkbox状态

    如题,如果必须要用DataGridCheckBoxColumn使用一下方式就可以解决需要点击两次才能改状态的问题 <DataGridCheckBoxColumn> <DataGrid ...