HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256
思路:
(sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n;
这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6);
an+bn*(sqrt(6))=(5+2*sqrt(6))*(a(n-1)+b(n-1)*sqrt(6))
=(5*a(n-1)+12*b(n-1))+(2*a(n-1)+5*b(n-1))*sqrt(6);
显然,an=5*a(n-1)+12*b(n-1);bn=2*a(n-1)+5*b(n-1);
此时能够非常easy的构造出一个矩阵来高速求an和bn:
5,12
2,5
那么下一步应该怎么办呢?对于我等菜渣来说最好的办法当然是。。打表。。找规律。。
然后规律就是ans=2*an-1;
那么怎么证明呢?证明例如以下:
(5+2*sqrt(6))^n=an+bn*sqrt(6); (5-2*sqrt(6))^n=an-bn*sqrt(6);
(5+2*sqrt(6))^n+(5-2*sqrt(6))^n=2*an;
然后,因为
(5-2*sqrt(6))^n=(0.101....)^n<1;
再因为
(5+2*sqrt(6))^n=2*an-(5-2*sqrt(6))^n
可得
2*an-1<(5+2*sqrt(6))^n<2*an;
所以对(5+2*sqrt(6))^n向下取整的结果一定是2*an-1;
证明完成。
所以说仅仅要用矩阵高速幂求出an就可以。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
const int mod=1024;
struct matrix
{
int ma[3][3];
}init, res;
matrix Mult(matrix x, matrix y)
{
matrix tmp;
int i, j, k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
tmp.ma[i][j]=0;
for(k=0;k<2;k++)
{
tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;
}
}
}
return tmp;
}
matrix Pow(matrix x, int k)
{
int i, j;
matrix tmp;
for(i=0;i<2;i++) for(j=0;j<2;j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
int main()
{
int t, k;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
init.ma[0][0]=5;
init.ma[0][1]=12;
init.ma[1][0]=2;
init.ma[1][1]=5;
res=Pow(init,k-1);
int ans=(2*(res.ma[0][0]*5+res.ma[0][1]*2)-1)%mod;
printf("%d\n",ans);
}
return 0;
}

HDU 2256 Problem of Precision(矩阵高速幂)的更多相关文章
- HDU 2256 Problem of Precision (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 最重要的是构建递推式,下面的图是盗来的.貌似这种叫共轭数. #include <iostr ...
- HDU 2256 Problem of Precision(矩阵)
Problem of Precision [题目链接]Problem of Precision [题目类型]矩阵 &题解: 参考:点这里 这题做的好玄啊,最后要添加一项,之后约等于,但是有do ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...
- hdu 5411 CRB and Puzzle 矩阵高速幂
链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...
- HDU 2256 Problem of Precision (矩阵快速幂)(推算)
Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2256 Problem of Precision 数论矩阵快速幂
题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...
- HDU 2256 Problem of Precision( 矩阵快速幂 )
链接:传送门 题意:求式子的值,并向下取整 思路: 然后使用矩阵快速幂进行求解 balabala:这道题主要是怎么将目标公式进行化简,化简到一个可以使用现有知识进行解决的一个过程!菜的扣脚...... ...
- HDU 2256 Problem of Precision (矩阵乘法)
Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- Dynamic Programming for TSP
See how Dynamic programming working for TSP: Check this link: http://www.youtube.com/watch?v=IUzE1Mb ...
- MAC下PHP开发
ZendStudio 10.5安装: http://blog.sina.com.cn/s/blog_7c8dc2d50101nhvb.html PHP+MySQL+Apache开发环境安装:XAMPP ...
- Mysql分区的技能
1. 查看分区信息 (1)explain partitions select * from TDM_YTMF_BRAND_CATE_GDS_STC_D 语法:explain partitions se ...
- 【转】Spring中IoC的优点与缺点
1. 优点 我们知道,在Java基本教程中有一个定律告诉我们:所有的对象都必须创建:或者说:使用对象之前必须创建,但是现在我们可以不必一定遵循这个定律了,我们可以从Ioc容器中直接获得一个对象然后直接 ...
- Hadoop,HBase集群环境搭建的问题集锦(二)
10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...
- HTTP Content-type整理
文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流.不知道下载文件类型) application/octet-st ...
- java之八大排序
的关系: 1.直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的.如此反 ...
- linux下sar tool command note
linux下的sar工具简介 我习惯使用的命令是 : sar -r -f /var/log/sa/sa24 sar 既能报告当前数据,也能报告历史数据 不带选项执行会以10分钟为间隔报告自午夜 ...
- 职业-把工作当作职业 or 事业?
有这么一种说法,工作态度可以分为两种:一种是把工作当作职业,另一种是把工作当成事业.态度折射品质,态度影响成败. 把工作当作一份职业的人,以职业交换薪水,完全是为工作而工作,工作是为了糊口养家,上班是 ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...