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+的代码题,偶尔还会写数学题.他找到 ...
随机推荐
- AtCoder Grand Contest 028 A:Two Abbreviations
题目传送门:https://agc028.contest.atcoder.jp/tasks/agc028_a 题目翻译 给你两个串\(s\)与\(t\),长度分别为\(n,m\).问你存不存在一个串长 ...
- 洛谷 P4512 [模板] 多项式除法
题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...
- 洛谷P1525关押罪犯——并查集
题目:https://www.luogu.org/problemnew/show/P1525 并查集+贪心,从大到小排序,将二人分在不同房间,找到第一个不满足的即为答案. 代码如下: #include ...
- C# 性能分析工具
http://msdn.microsoft.com/zh-cn/vstudio/aa497289(en-us).aspx Performance This section includes infor ...
- Python之模块介绍
模块介绍 模块,是用一些代码实现的某个功能的代码集合. 类似与函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用,提供了代码的重用性和代码间的耦合.对于一个复杂的功能,可能需要多个函 ...
- node url
var url = require("url") url模块提供的三个方法: url.parse(urlStr[, parseQueryString][, slashesDenot ...
- Linux命令总结_touch创建文件
1.touch命令,用来创建文件或者修改文件时间戳 格式:touch [选项]... 文件... 选项 : -a 或--time=atime或--time=access或--time=use 只 ...
- [bzoj2440]完全平方数(二分+mobius反演)
解题关键:由容斥原理得,num=1的倍数的数量−一个质数平方数(9,25,49...)的倍数的数量+两个质数的积平方数(36,100,225...)的数量−三个质数...... 这道题用莫比乌斯的正向 ...
- 阿里云服务器,无法通过公网ip访问实例
昨天得知阿里云esc又打折了,赶紧入手了一波,因为以前有部署过的经验,所以很快就部署上了项目,就在欣喜的访问时,却无访问. 我按照下面步骤一步一步的检测, 1.服务器上是否成功部署jdk和tomcat ...
- java之装箱拆箱
参考http://how2j.cn/k/number-string/number-string-wrap/22.html 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer ...