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 ...
随机推荐
- shell命令xargs解析
1.多行变成单行 -bash-3.2# cat test.txt a b c d e f g o p q -bash-3.2# cat test.txt |xargs a b c d e f g o ...
- 自己的一个js库
https://github.com/sqqihao/nono_framework.git 持续更新,慢慢写着,这样才能成长
- Java从零开始学四十一(反射简述二)
一.实例化Class类对象 实例化Class类对象的方法有三种: 第一种:通过forName()方法 第二种:类.class 第三种:对象.getClass() 二.Class类的常用方法 No. 方 ...
- SQL:1999基本语法(学习笔记)
SQL:1999基本语法 SELECT [DISTINCT] * | 列名称 [AS]别名,........ FROM 表名称1 [别名1][CROSS JOIN表名称2 别名2]| [NATURAL ...
- Flume wasn't able to parse timestamp header
来自:http://caiguangguang.blog.51cto.com/1652935/1384187 flume bucketpath的bug一例 测试的配置文件: 1 2 3 4 5 6 7 ...
- Wireshark基本介绍和TCP三次握手
转自:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. ...
- webapp 里主要的 mate 用途
一.meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). 1.http-equiv 属性的 Content-Type 值(显示字符集的设定) 说明:设定页面 ...
- html5 效果 按下鼠标数值自动增长
<!doctype html> <html> <head> <style> * { margin:0; padding:0; } div { margi ...
- Eclipse调试cas server 3.5.2.1
由于在配置CAS+LDAP总是报错,决定Eclipse调试cas server,跟踪问题出在哪里? ================================================== ...
- aop:declare-parents注解
http://www.blogjava.net/jackfrued/archive/2010/02/27/314060.html <aop:aspect> <aop:declare- ...