HDU 2256Problem of Precision(矩阵快速幂)
题意
求$(\sqrt{2} + \sqrt{3})^{2n} \pmod {1024}$
$n \leqslant 10^9$
Sol
看到题解的第一感受:这玩意儿也能矩阵快速幂???
是的,它能qwq。。。。
首先我们把$2$的幂乘进去,变成了
$(5 + 2\sqrt{6})^n$
设$f(n) = A_n + \sqrt{6} B_n$
那么$f(n+1) = (A_n + \sqrt{6} B_n ) * (5 + 2\sqrt{6})$
乘出来得到
$A_{n + 1} = 5 A_n + 12 B_n$
$B_{n + 1} = 2A_n + B B_n$
那么不难得到转移矩阵
$$\begin{pmatrix} 5 & 12 \\ 2 & 5 \end{pmatrix}$$
这样好像就能做了。。
但是实际上后我们最终会得到一个类似于$A_n + \sqrt{6}B_n$的东西,这玩意儿还是不能取模
考虑如何把$\sqrt{6}$的影响消掉。
$(5 + 2 \sqrt{6})^n = A_n + \sqrt{6}B_n$
$(5 - 2 \sqrt{6})^n = A_n - \sqrt{6}B_n$
相加得
$(5 + 2 \sqrt{6})^n + (5 - 2 \sqrt{6})^n = 2A_n$
考虑到$0 < (5 - 2\sqrt{6})^n < 1$
那么
$$\lfloor (5 + 2\sqrt{6})^n \rfloor = 2A_n - 1$$
做完啦qwq
#include<cstdio>
#include<cstring>
#include<iostream>
#define Pair pair<int, int>
#define MP(x, y)
#define fi first
#define se second
// #include<map>
using namespace std;
#define LL long long
const LL MAXN = , mod = ;
inline LL read() {
char c = getchar(); LL x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int T, N;
struct Matrix {
LL m[][], N;
Matrix() {N = ; memset(m, , sizeof(m));}
Matrix operator * (const Matrix &rhs) const {
Matrix ans;
for(int k = ; k <= N; k++)
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
(ans.m[i][j] += 1ll * m[i][k] * rhs.m[k][j] % mod) % mod;
return ans;
}
}; Matrix fp(Matrix a, int p) {
Matrix base; base.m[][] = ; base.m[][] = ;
while(p) {
if(p & ) base = base * a;
a = a * a; p >>= ;
}
return base;
}
int main() {
T = read();
while(T--) {
N = read();
Matrix a;
a.m[][] = ; a.m[][] = ;
a.m[][] = ; a.m[][] = ;
a = fp(a, N - );
LL ans = ( * a.m[][] + * a.m[][]) % mod;
printf("%I64d\n", ( * ans - ) % mod);
}
return ;
} /**/
HDU 2256Problem of Precision(矩阵快速幂)的更多相关文章
- 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 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[ ...
- HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others) ...
- HDU 6395 Sequence 【矩阵快速幂 && 暴力】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 5667 Sequence【矩阵快速幂+费马小定理】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...
随机推荐
- OGG 11g Checkpoint 详解
OGG Checkpoint 详解 定位中断的位置,下次启动从中断的位置开始恢复. 1.target 端配置: 2.一条记录对应一个replicat 一. Extract Check ...
- BZOJ1382:[Baltic2001]Mars Maps
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...
- shell中利用ftp 上传文件夹功能
我们知道ftp 只能用来上传或者下载文件,一次单个或者多个,怎么实现将文件夹的上传和下载呢? 可以利用先在remote ip上建立一个相同的文件夹目录,然后将文件放到各自的目录中去 1.循环遍历出要上 ...
- ubuntu下mysql的安装与配置
1.安装,安装的过程中会提示你设置 MySql的"root"密码 sudo apt-get install mysql-server mysql-client 2.把 /etc/ ...
- 选择排序(java)
选择排序的思想是:内外两层循环,第一层循环从第一个数开始到倒数第一个数, 第二层循环从上一层的数开始, 与上一层循环的数比较,如果小于则交换位置. 代码如下: public class SelectS ...
- 02_SQliteOpenHelper介绍&oncreate方法介绍
file:///D:/BaiduNetdiskDownload/adt-bundle-windows-x86_64_20140101/adt-bundle-windows-x86_64_2014010 ...
- hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)
TIANKENG’s restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 130107/65536 K (Ja ...
- windows64下安装MySQLdb连接数据库
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 1.首先要下载:http://www.codegood.com/downloads,由于我的python是2 ...
- windows64位 redis安装 步骤
官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.com/MSOpenTech/redis/releases 在D盘 ...
- Invalidate()这个函数有什么用?
c++中的这个函数,一会是刷新窗口的作用,一会是使区域无效.我搞不懂这个函数究竟是有什么作用?谢谢赐教. void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使 ...