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[ ...
随机推荐
- SQL Server开窗函数之OVER子句、PARTITION BY 子句
开窗函数与聚合函数一样,都是对行的集合组进行聚合计算.它用于为行定义一个窗口(这里的窗口是指运算将要操作的行的集合),它对一组值进行操作,不需要使用GROUP BY子句对数据进行分组,能够在同一行中同 ...
- Redis key 键
1.set key value //设置.修改值 2.get key //如果key不存在,返回nil,表示空. 3.type key //返回key对应的value的数据类型 4.ren ...
- GNUPG
PGP (Pretty Good Privacy) 是由 Phil Zimmermann 于 1991 开发的一个用于数据加密和数字签名的程序,由于被广泛应用以至于后来形成一个开放的标准 OpenPG ...
- springboot集成shiro实现验证码校验
github:https://github.com/peterowang/shiro/ 这里实现验证码校验的思路是自己添加一个Filter继承FormAuthenticationFilter,Form ...
- swift基础-3
fallthrough 贯穿 case 可以不必写break 如果不需要知道区间内 每一项的值 可以使用 下划线 —— 来代替变量名 忽略 对该值的访问 for index in 1...5{ p ...
- 使用Calendar来获取当前日期和时间
1 package com.java.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 6 pub ...
- htmlparse
<html> <head> <style> textarea{ width:800p ...
- 关于学习Lisp的一点思考
以前读<黑客与画家>,其中对Lisp语言的赞美和推崇,让我燃起学习Lisp语言的强烈冲动,但很快发现在实际工作中应用的场景很少,出于功利心最终放弃了.直到上周未在家里读完了<大教堂与 ...
- freebsd快速删除磁盘数据
At the start, mark all system disks as empty. Repeat the following command for each hard drive: dd i ...
- Centos 7 搭建git服务器及使用gitolite控制权限
一.安装git yum install git git --version #查看git版本 二.升级git(可选,如果之前已经安装git,需要升级git到最新版本) git clone https: ...