HDU 4990 Reading comprehension 矩阵快速幂
题意:
给出一个序列,
\(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 矩阵快速幂的更多相关文章
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- hdu4990 Reading comprehension 矩阵快速幂
Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...
- HDU.1575 Tr A ( 矩阵快速幂)
HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- HDU 4990 Reading comprehension 简单矩阵快速幂
Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- 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 ...
随机推荐
- 第二十章 排查和调试Web程序 之 设计异常处理策略
1. 概述 本章内容包括: 多层架构中的异常处理.使用global.asax 或 自定义的HttpHandler 或 web.config中的属性来显示特定的错误页.处理 first chance 异 ...
- 【Java】 jar解压与压缩
jar解压与压缩 命令格式:jar {c t x u f }[ v m e 0 M i ][-C 目录]文件名 # 解压,到当前目录 jar -xvf source.jar # 打包,不进行压缩 ja ...
- python中的uuid4
Help on function uuid4 in module uuid: uuid4() Generate a random UUID.
- ASP.NET WebForm & MongoDB
ASP.NET WebForm & MongoDB 最近在朋友介绍下,也跟着看AngularJS 买了一本三合一的书,Node.JS+MongoDB+AngularJS http://www. ...
- spring security 2.x HttpSessionEventPublisher 以及listener配置
在环境为spring security2.x时 *JDK6 spring 2* 正确的filter路径是:org.springframework.security.ui.session.HttpSes ...
- 使用JS的画布制作一个瞄准镜
<canvas width="600" height="500" id="myCanvas"></canvas> & ...
- spring maven 包
<spring-framework.version>.RELEASE</spring-framework.version> <dependency> <gro ...
- MyBatis归纳
SqlSessionTemplate详解 SqlSessionTemplate类是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL ...
- ubuntu下安装eclipse<转>
转载自http://my.oschina.net/u/1407116/blog/227084 http://my.oschina.net/u/1407116/blog/227087 一 JD ...
- freespace
这是一个总结freespace的博客,还不错,但算法目前看来比较老:https://blog.csdn.net/u010213183/article/details/70786516