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

Problem Description
Input
The
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)
Output
For each input case, you should output the answer in one line.
Sample Input
3
1
2
5
Sample Output
9 97 841
【分析】

这个题目算是矩阵快速幂的比较难推的一个题目。题目要求 (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 (矩阵快速幂)(推算)的更多相关文章

  1. HDU 2256 Problem of Precision (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 最重要的是构建递推式,下面的图是盗来的.貌似这种叫共轭数. #include <iostr ...

  2. 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 ...

  3. HDU 2256 Problem of Precision(矩阵)

    Problem of Precision [题目链接]Problem of Precision [题目类型]矩阵 &题解: 参考:点这里 这题做的好玄啊,最后要添加一项,之后约等于,但是有do ...

  4. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  5. HDU 2256 Problem of Precision 数论矩阵快速幂

    题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...

  6. HDU 2256 Problem of Precision( 矩阵快速幂 )

    链接:传送门 题意:求式子的值,并向下取整 思路: 然后使用矩阵快速幂进行求解 balabala:这道题主要是怎么将目标公式进行化简,化简到一个可以使用现有知识进行解决的一个过程!菜的扣脚...... ...

  7. HDU 2256Problem of Precision(矩阵快速幂)

    题意 求$(\sqrt{2} + \sqrt{3})^{2n} \pmod {1024}$ $n \leqslant 10^9$ Sol 看到题解的第一感受:这玩意儿也能矩阵快速幂??? 是的,它能q ...

  8. hdu 2256 Problem of Precision

    点击打开hdu 2256 思路: 矩阵快速幂 分析: 1 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 3 这里很多人会直接认为结果等于(an+bn*sqrt(6))% ...

  9. 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 ...

随机推荐

  1. 黑群晖DSM 6.1网卡支持列表

    黑群晖DSM 6.1网卡支持列表 Network Drivers====================================AMDamd8111e : AMD 8111 (new PCI ...

  2. [洛谷P2482][SDOI2010]猪国杀

    题目大意:猪国杀,又一道大模拟题 题解:模拟,对于一个没有玩过三国杀的人来说,一堆细节不知道,写的十分吃力 卡点:无数,不想说什么了,这告诉我要多玩游戏 C++ Code: #include < ...

  3. [Leetcode] count and say 计数和说

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  4. [Leetcode] word ladder 单词阶梯

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  6. 1040: [ZJOI2008]骑士~基环外向树dp

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...

  7. jsonArray与jsonObject

    最近两个星期接触最多的就是json和map了. 之前用到的json,就是一个键对应一个值,超级简单的一对一关系.现在用到的json那可以层层嵌套啊,刚开始接触的时候,确实有种崩溃的赶脚,不想去理,取个 ...

  8. 使用jQuery发送POST,Ajax请求返回JSON格式数据

    问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...

  9. Python基础(1)_初识Python

    一.为什么要编程 解放人力:让机器按照人们事先为其编写好的程序自发地去工作 二.什么是编程语言 编程语言就是程序员与计算机之间沟通的介质:程序员把自己想说的话用编程语言写到文件里,这其实就开发了一个程 ...

  10. hdu 1175 连连看 (深搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...