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 ...
随机推荐
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第1篇 VGGnet_test.py ----作者:Jiang Wu(吴疆),未经允许,禁止转载--- -- ...
- 合并石子,区间dp
#define INF 9999999 ],dp[][],ans=,s[]; int main() { scanf("%d",&n); ;i<=n;i++) scan ...
- AJPFX关于Java中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法.快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现.冒泡法是运用遍历数组进行比 ...
- JavaBean+jsp开发模式 --结合form表单 实例
1.创建form表单 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- springmvc学习经验
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring ...
- 1.2 the structure of a compiler
Compiler 1.2 the structure of a compiler Compiler : analysis and synthesis syntactically 语法上的 sema ...
- 深度探索C++对象模型——关于对象
引言 以前读<C++ Primer>的时候一直有一种感觉:该书虽然是C++入门书籍,初学者读之却觉晦涩,越往后读越是如此.等到稍加理解后再读该书,顿感醍醐灌顶,茅塞顿开.究其原因,在于原作 ...
- LINQ 基础语句
去全部集合 using (dat0216DataContext con = new dat0216DataContext()) { //LoList 是转换成 List集合 List<Us ...
- [VC]关于ocx打包为cab的使用
把打包的工具makecert.exe等放在c:/windows/system32/里才能在dos下面使用 注:如果本机使用可以codebase="http://localhost/testa ...
- Android(java)学习笔记123:Android MediaPlayer 播放prepareAsync called in state 8解决办法
1. 使用android MediaPlayer播放音频文件时,有时会出现prepareasync called in state 8错误. 以下方法可以避免这个异常出现. 第1种方法: priva ...