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+的代码题,偶尔还会写数学题.他找到 ...
随机推荐
- bzoj 5281 [Usaco2018 Open]Talent Show——0/1分数规划
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5281 把分子乘1000,就能在整数里做了. 这种水题也花了这么久…… #include< ...
- 重学JAVA基础(二):Java反射
看一下百度的解释: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息 ...
- Python中正则匹配使用findall时的注意事项
在使用正则搜索内容时遇到一个小坑,百度搜了一下,遇到这个坑的还不少,特此记录一下. 比如说有一个字符串 "123@qq.comaaa@163.combbb@126.comasdf111@a ...
- 03_通过OpenHelper获取SqliteDatabase对象
MyOpenHelper openHelper = new MyOpenHelper(this); 类似于java的File file = new File();只是声明这个东西,但是文件还并没有真正 ...
- POI 中的CellType类型以及值的对应关系
操作使用POI接口,了解CellType的类型和值的对应关系. CellType 类型 值 CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1 CELL_T ...
- CF-811B
B. Vladik and Complicated Book time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- POJ-3069
Saruman's Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10994 Accepted: 5555 D ...
- c 无回显读取字符/不按回车即获取字符
最近课程设计要使用各种有趣的函数,这是其中一个 #include <conio.h> 使用方法 char c; c=getch(); 这样按下输入一个字符不按回车就ok了
- DropDownlist数据SelectedIndexChanged触发问题解决
1.设置DropDownlist的AutoPostBack为True 2.绑定DropDownlist数据时出现了重复项, 在载入数据时保存数据状态应该写在Load事件中的if (!IsPostBac ...
- 根据xml文件自动生成xsd文件
根据xml生成xsd文档 1. 找到vs自带的xsd.exe工具所在的文件夹位置: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin 注意 ...