HDU 2256 Problem of Precision (矩阵快速幂)(推算)
Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1375 Accepted Submission(s): 826

first line of input gives the number of cases, T. T test cases follow,
each on a separate line. Each test case contains one positive integer n.
(1 <= n <= 10^9)
1
2
5
这个题目算是矩阵快速幂的比较难推的一个题目。题目要求 (sqrt(2)+sqrt(3))的 2^n并%1024,要求出值来并不难,构造矩阵即可,但是要mod1024就有问题了,小数不能直接mod,但是如果你取整之后再mod,结果绝逼出问题,因为浮点数的精度问题。

所以从斌牛的博客上看到如此推算,推算第一块不难,而且很容易求出Xn 和 Yn,但是问题又出来了,要是求出来后,直接用(int)(Xn+Yn*sqrt(6))%1024,又会出问题,还是浮点数取整问题,我一开始就这么算的,导致结果奇葩。看来在mod的时候有浮点数要格外注意,直接处理的话,不管怎么取整,都会出问题。
所以分割线下面的推算就避开了这个问题,这个确实好难想到,通过变换一下,得到最终的结果必定是2Xn-(0.101...)^n,因为最终mod是用不大于浮点数的最大整数在mod,所以最终结果就是2Xn-1.第二条确实好难想到!
题解转载于 http://www.cnblogs.com/kkrisen/p/3437710.html;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll N = ; ll f1,f2,k;
ll mod = ;
ll n; struct Fast_Matrax {
ll a[N][N];
Fast_Matrax() {
memset(a,,sizeof(a));
}
void init() {
for(int i=; i<N; i++)
for(int j=; j<N; j++)
a[i][j]=(i==j);
}
Fast_Matrax operator * (const Fast_Matrax &B)const {
Fast_Matrax C;
for(int i=; i<N; i++)
for(int k=; k<N; k++)
for(int j=; j<N; j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j]%mod+mod)%mod;
return C;
}
Fast_Matrax operator ^ (const ll &t)const {
Fast_Matrax A=(*this),res;
res.init();
ll p=t;
while(p) {
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
} ans,tmp,x;
int main() {
x.a[][]=;x.a[][]=;
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
if(n<=){
puts("");
}
else {
tmp.a[][]=;tmp.a[][]=;
tmp.a[][]=;tmp.a[][]=;
ans=(tmp^(n-))*x;
printf("%lld\n",(*ans.a[][]-)%mod);
}
}
return ;
}
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(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
- HDU 2256 Problem of Precision(矩阵)
Problem of Precision [题目链接]Problem of Precision [题目类型]矩阵 &题解: 参考:点这里 这题做的好玄啊,最后要添加一项,之后约等于,但是有do ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 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 2256Problem of Precision(矩阵快速幂)
题意 求$(\sqrt{2} + \sqrt{3})^{2n} \pmod {1024}$ $n \leqslant 10^9$ Sol 看到题解的第一感受:这玩意儿也能矩阵快速幂??? 是的,它能q ...
- hdu 2256 Problem of Precision
点击打开hdu 2256 思路: 矩阵快速幂 分析: 1 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 3 这里很多人会直接认为结果等于(an+bn*sqrt(6))% ...
- hdu 4686 Arc of Dream(矩阵快速幂)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...
随机推荐
- bzoj 3771 Triple FFT 生成函数+容斥
Triple Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 847 Solved: 482[Submit][Status][Discuss] Desc ...
- remove computer from join with powershell
Removes the local computer from its domain. Remove-Computer [-UnjoinDomainCredential] <PSCredenti ...
- 在Maven中怎么配置外部Jar
转摘自:http://liugang594.iteye.com/blog/1677712 假设我们有一个Maven的project,其中有些Jar包不是来自Maven库的,是存在本地的某些Jar文件, ...
- ByteUtil 工具类
ByteUtil 工具类 import java.io.FileOutputStream; import java.io.OutputStream; import java.nio.charset.C ...
- salt搭建lamp架构
install_httpd: pkg.installed: - name: httpd httpd_running: service.running: - name: httpd - enable: ...
- USACO_1.1_Greedy_Gift_Givers_(模拟+水题)
描述 http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=gift1 给出不超过$10$个人,每个人拿出一定数量的钱平分给特定的人,求最后每个人 ...
- [vue-router] Failed to resolve async component default: Error: Loading chunk 0 failed.
在整合laravel5.4 和vue2.1的时候遇到一个奇怪的问题 Uncaught SyntaxError: Unexpected token < Error: Loading chunk 0 ...
- elastaticsearch
# https://elasticsearch-dsl.readthedocs.io/en/latest/ # 文档:https://es.xiaoleilu.com/054_Query_DSL/70 ...
- SAR图像处理 MSTAR数据库利用问题
本人最近刚接手的新活,打算从事SAR目标识别的研究,从美国的mstar数据库中下到一些资源,但是这些数据并不是原始图片的格式,需要转换,由于刚刚接触,并不太了解,所以特此请教!如有感兴趣的朋友可以一起 ...
- CocoaPods详解之----使用篇
http://blog.csdn.net/meegomeego/article/details/24005567 作者:wangzz 原文地址:http://blog.csdn.net/wzzvict ...