题意:

求\(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! 矩阵快速幂的更多相关文章

  1. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  2. 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 ...

  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. 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

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

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

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

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

  8. 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 ...

  9. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

随机推荐

  1. webpack分开打包和合并打包的瘦身

    webpack.config.js 记录一下优化webpack的几个点: 1.     devtool: false,   //产品阶段不应该有devtool entry: { bundle : pa ...

  2. js 回车提交表单

    一.整个页面用一个回车提交事件: <input type="button" value="回车提交" id="auto" onclic ...

  3. iOS - runtime 常用方法举例说明

    使用的自定义类,如下: #import <Foundation/Foundation.h> @interface Person : NSObject @property(nonatomic ...

  4. Nginx+Keepalived负载均衡+后端LNMP网站集群

    Centos6.4 x86,4台,地址是10.10.10.11-14,vip给的100,目标是在13和14安装nginx+keepalived,11和12安装nginx+mysql+php,做为web ...

  5. miniLCD12864 16引脚

    显示图片 main.c #include<reg51.h>#include"st7565.h"//---存一个图片--//unsigned char code pic[ ...

  6. Netweaver和CloudFoundry是如何运行Web应用的?

    Netweaver 在Jerry的微信公众号文章SAP Fiori应用的三种部署方式里提到SAP Fiori应用以BSP应用的方式部署在ABAP Front-End Server上.那么这些BSP应用 ...

  7. Intel 快速存储蓝屏

    今天电脑蓝屏,DPC Watchdog Violation 很烦.开bluescreen说是NT内核的问题 开windbg说是Intel快速存储的问题,顺手卸载快速存储 卸载前 卸载后 另外我看Int ...

  8. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)

    题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...

  9. 谷歌浏览器 加安全地址 快捷方式加参数 chrome

    --unsafely-treat-insecure-origin-as-secure="http://192.168.43.17:8080"

  10. VC++:鼠标的使用

    长期改变鼠标形状: SetClassLongPtr(GetSafeHwnd(), GCLP_HCURSOR, (LONG)LoadCursor(NULL, IDC_WAIT));//这个是x64下可以 ...