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 ...
随机推荐
- Python源代码 -- C语言实现面向对象编程(基类&派生类&多态)
背景 python是面向对象的解释性语言.然而python是通过C语言实现的,C语言怎么跟面向对象扯上了关系? C语言能够实现面向对象的性质? 原文链接:http://blog.csdn.net/or ...
- ListView与Button共存问题
转载:http://blog.csdn.net/xinqiqi123/article/details/6458030 ListView 和 其它能触发点击事件的widget无法一起正常工作的原 ...
- 用Main方法调用freemarker生成文件
MyGenerator.java package com.comp.common; import java.io.BufferedWriter; import java.io.File; import ...
- 同时安装不同版本JDK遇到的问题
安装JDK1.8出现 Error opening registry key'software\Javasoft\Java Runtime Environment' java安装1.8后的问题:之前安装 ...
- vue 子组件引用
使用 ref 为子组件指定一个引用 ID.例如: <div id="parent"> <user-profile ref="profile"& ...
- mysql-cluster 7.3.5-linux 安装
[集群环境] 管理节点 10.0.0.19 数据节点 10.0.0.12 10.0.0.17 sql节点 10.0.0.18 10.0.0 ...
- div最小高度的2种写法
1.第一种写法: 原理:在IE6中,使用CSS定义div的高度的时候经常遇到这个问题,就是当div的最小高度小于一定的值以后,就会发现,无论你怎么设置最小高度,div的高度会固定在一个值不再发生变动, ...
- jQuery写一个简单的弹幕墙
概述 近几年由于直播,弹幕流行起来,之前看到过用js制作弹幕墙的思路,觉得很有趣.自己就花了点时间把他做成了更灵活的jQuery插件,现在分享出来. 详细 代码下载:http://www.demoda ...
- PHP-FastCGI详解
一.什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP server和动态脚本语言间通信的接口.多数流行的HTTP server都支持FastCGI,包括Apache.Nginx和 ...
- macOS Sierra Git Gui Crash 解决方法
本篇文章由:http://xinpure.com/macos-sierra-git-gui-crash-solution/ 问题描述 自从升级到 macOS Sierra 10.12 之后,git g ...