hdu 2256 Problem of Precision(矩阵快速幂 推公式)
来源:互联网 发布:淘宝网店培训多少钱 编辑:程序博客网 时间:2024/06/06 05:44
题意:
给出n,求((sqrt(2)+sqrt(3))^(2*n))%1024
解题思路:
(sqrt(2)+sqrt(3))^2 = (5+2*sqrt(6))
(5+2*sqrt(6))^n = Xn+Yn*sqrt( 6 ) = ( Xn-1+Yn-1*sqrt( 6 ) ) * ( 5+2*sqrt( 6 ) )
= (5*Xn-1+12*Yn-1) + (2*Xn-1+5*Yn-1)*sqrt( 6 )
所以Xn = 5*Xn-1+12*Yn-1 Yn =2*Xn-1+5*Yn-1
矩阵就容易构造了
结果需要处理一下,不能直接算
(5-2*sqrt(6))^n = (0,101)^n 近似为0
Xn+Yn*sqrt( 6 ) = Xn+Yn-1+Xn-Yn-1-(Xn-Yn*sqrt( 6 )) = 2*Xn-(0.101..)^n
向下取整
所以最终结果是2*Xn-1
错误点:
直接认为结果等于(an+bn*sqrt(6))%1024,但是这种结果是错的,因为这边涉及到了double,必然会有误差,所以根double有关的取模都是错误的思路
#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <math.h>#include <algorithm>using namespace std;#define mod 1024struct matrix{int a[2][2];};matrix mul(matrix a,matrix b){matrix ret;memset(ret.a,0,sizeof(ret.a));for(int i = 0;i<2;i++){for(int j=0;j<2;j++){ret.a[i][j] = (a.a[i][0]*b.a[0][j]+a.a[i][1]*b.a[1][j])%mod;}}return ret;}matrix mpower(matrix a,__int64 x){matrix I;I.a[0][0] = I.a[1][1] = 1;I.a[0][1] = I.a[1][0] = 0;while(x){if(x&1) I = mul(I,a);x>>=1;a = mul(a,a);}return I;}int main(){int t;cin>>t;while(t--){matrix tmp;tmp.a[0][0] = 5;tmp.a[0][1] = 12;tmp.a[1][0] = 2;tmp.a[1][1] = 5;__int64 n;cin>>n;tmp = mpower(tmp,n-1);int x,y;x = (tmp.a[0][0]*5+tmp.a[0][1]*2)%mod;y = (tmp.a[1][0]*5+tmp.a[1][1]*2)%mod;int sum = (2*x-1+mod)%mod;cout<<sum<<endl;}return 0;}
0 0
- hdu 2256 Problem of Precision(矩阵快速幂 推公式)
- HDU 2256 Problem of Precision (矩阵快速幂)
- Hdu 2256 Problem of Precision[矩阵快速幂 + 数学]
- HDU 2256 Problem of Precision(矩阵快速幂)
- HDU - 2256 Problem of Precision 矩阵快速幂
- hdu 2256 Problem of Precision 矩阵快速幂
- HDU 2256 Problem of Precision(矩阵快速幂)
- HDU 2256 Problem of Precision(矩阵快速幂 数论 )
- HDU 2256 Problem of Precision(矩阵快速幂)
- HDU.2256 Problem of Precision (矩阵快速幂)
- hdu 2256 Problem of Precision(矩阵乘法+共轭公式)
- Problem of Precision(矩阵快速幂)
- [矩阵快速幂]hdu2256 Problem of Precision
- hdu 2256 Problem of Precision(矩阵快速幂,得出递推式有难度)
- HDU 2256 Problem of Precision 解题报告(矩阵快速幂 + 构造)
- HDU 2256 Problem of Precision(矩阵快速幂入门题)
- 【矩阵快速幂-求平方根表达式的值】HDU Problem of Precision 2256
- HDU 2256 Problem of Precision (矩阵快速幂+ 化简 +向下取整)
- 大数据时代的地理信息科学与科研能力培养
- 更简单、更安全的查询方法
- vs2013 ODBC连接Oracle 学习记录
- 开始反向思考——反其道而行之
- hadoop实现原理 (三) 远程过程调用
- hdu 2256 Problem of Precision(矩阵快速幂 推公式)
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
- UML简单介绍(四)——用例图的基本概念与解析
- hdu1251统计难题
- 图的邻接表存储 cpp实现
- task_rq(p)
- 固定链接更改后,文章打不开解决方案
- IE和火狐关于文件上传路径然后下载的问题(正常路径IE识别,火狐识别不出正常的路径)
- 多线程下载原理