HDU 4565 So Easy! 矩阵快速幂
题意:
求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值。
分析:
设\((a+\sqrt{b})^n=A_n+B_n \sqrt{b}\),
\((a+\sqrt{b})^{n+1}=(a+\sqrt{b})(A_n+B_n \sqrt{b})=(aB_n+A_n)+(A_n+aB_n) \sqrt{b}\),
所以有转移矩阵:
$\begin{bmatrix}
a & b \
1 & a
\end{bmatrix}
\begin{bmatrix}
A_n\
B_n
\end{bmatrix}
\begin{bmatrix}
A_{n+1}\
B_{n+1}
\end{bmatrix}$
如果把\(\sqrt{b}\)变为\(-\sqrt{b}\),就得到\((a- \sqrt{b})^n=A_n-B_n \sqrt{b}\)。
两式相加:\((a+\sqrt{b})^n+(a-\sqrt{b})^n=2A_n\)。
再由题中所给条件知道,\(a-\sqrt{b}\)是个小于\(1\)的数,所以\(\left \lceil (a+\sqrt{b})^n \right \rceil=2A_n\)。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a, b, n, m;
int mul(int a, int b) { return a * b % m; }
void add(int& a, int b) { a += b; if(a >= m) a -= m; }
struct Matrix
{
int a[2][2];
Matrix() { memset(a, 0, sizeof(a)); }
Matrix operator * (const Matrix& t) const {
Matrix ans;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
add(ans.a[i][j], mul(a[i][k], t.a[k][j]));
return ans;
}
};
Matrix pow_mod(Matrix a, int p) {
Matrix ans;
for(int i = 0; i < 2; i++) ans.a[i][i] = 1;
while(p) {
if(p & 1) ans = ans * a;
a = a * a;
p >>= 1;
}
return ans;
}
int main()
{
while(scanf("%d%d%d%d", &a, &b, &n, &m) == 4) {
a %= m; b %= m;
Matrix M;
M.a[0][0] = a; M.a[0][1] = b;
M.a[1][0] = 1; M.a[1][1] = a;
M = pow_mod(M, n - 1);
int ans = 0;
add(ans, mul(M.a[0][0], a));
add(ans, M.a[0][1]);
ans = mul(ans, 2);
printf("%d\n", ans);
}
return 0;
}
HDU 4565 So Easy! 矩阵快速幂的更多相关文章
- [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂
从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...
- hdu4565 So Easy! 矩阵快速幂
A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...
- 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) ...
- 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 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 ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
随机推荐
- P4878 道路修建-美国
http://www.tyvj.cn/p/4878道路修建 我想我经大神点拨后终于明白了...回学校再写吧 时间限制:1s 内存限制:256MB [问题描述] A国是一个商业高度发达的国家.它包含了n ...
- 架构演进历程及为什么选择Spring Cloud
单体式架构: 垂直拆分: 垂直拆分的特点: 分布式服务: 分布式服务的特点: SOA面向服务的架构: 服务治理: 微服务: 微服务结构: 服务调用方式: http客户端工具:
- ABAP事件的简单用法
1.1.事件: 用于捕获某类对象状态的改变来触发事件的方法,并进行处理 1.2.定义:可以在类或接口中进行声明 EVENTS|CLASS-EVENTS evt EXPORTING … VALUE(p ...
- u-boot剖析(一)----Makefile分析
由于u-boot比较庞大,所以我们分开来分析,对于一个大型的项目我们想快速的了解其代码架构和内容,最方便的方法就是分析Makefile,所以我们今天以三星的s3c2440来分析Makefile.我们今 ...
- Game Engine Architecture
- 洛谷 P1433 吃奶酪
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- python爬虫之路——对字符串的处理
对字符串的处理分类:分段,连接,剔除,提取,综合 连接:+,* +(加法)的使用 a='i' b=' love' c=' you' print(a+b+c) #return i love you *( ...
- SAP标准培训课程C4C10学习笔记(一)第一单元
C4C10:SAP Hybris Cloud for Customer Administration 课程目录: 第一单元是C4C的简介. 作为SAP推出的一个SaaS(Software as a s ...
- lwz程序人生之启程
本人14年小本科毕业. 第一次接触电脑是小学4年级.当时,是小学公开课,老师让我们去电脑室秀一下.现在,我都记不得当时我第一次看到电脑室好奇呢?还是没感觉呢? 到初中上电脑课的时候,我才真正了解到电脑 ...
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...