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+的代码题,偶尔还会写数学题.他找到 ...
随机推荐
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- 七 vue学习 async/await
1: javaScript async/await: 调用async函数的时候,是异步的,函数后面的代码继续执行.! async / await是ES7的重要特性之一,也是目前社区里公认的优秀异步解 ...
- Windows WMIC命令使用详解2
Windows WMIC命令使用详解(附实例) https://blog.csdn.net/aflyeaglenku/article/details/77878525 第一次执行WMIC命令时,Win ...
- C# 32位程序在64位系统下运行中解决重定向问题
在64位的Windows操作系统中,为了兼容32位程序的运行,64位的Windows操作系统采用重定向机制.目的是为了能让32位程序在64位的操作系统不仅能操作关键文件文夹和关键的注册表并且又要避免与 ...
- There is no resul…
There is no result type defined for type 'json' mapped with name 'success'. 这个错误是json初学者很容易遇到的错误:现在把 ...
- 7.13实习培训日志 Docker
静态博客github地址 静态博客github地址轻量版 Docker Docker镜像 Docker镜像概念 Docker镜像下载时的分层体现:一层层下载,下载过程中给出了每一层的 ID 的前 12 ...
- Linux之configure make make install
正常的编译安装/卸载: 源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). configure文件是一个可执行的脚本文件,它有很多选项, ...
- Guid string 转换
System.Guid.NewGuid().ToString(); //GUID带-分割// db1b98e9-6f93-41aa-84f8-5eb773e93d67System.Guid.NewGu ...
- httpd.conf------Apache主站点配置
Apache主站点配置 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. Apache主站点基本配置:ServerRoot “/mnt/sof ...
- 字节码操作-Javaassist
下面就是一个具体的demo来介绍利用Javaassist库来创建类,不过要先在工程里面导入Javaassist的架包, package JavaAasist; import java.lang.ref ...