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. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  2. 停课day1

    一早上只做了一个calculator 还是参照题解,好惭愧 f[1]=0; flag[1]=true;    for (int i=2,N=num[n];i<p;i++) {        fo ...

  3. spring 中的@Import注解和@ImportResource注解

    概述:@Import注解是引入带有@Configuration的java类. @ImportResource是引入spring配置文件.xml 案例的核心代码如下: package com.timo. ...

  4. DOM创建和删除节点

    一.创建节点 3步 1.创建空元素对象: var newElem=document.createElement("标签名"); 例如:var a=document.createEl ...

  5. 【20160811】noip模拟-未完

    T1 T2 T3 小奇回地球 [问题描述] 简单来说,它要从标号为1的星球到标号为n的星球,某一些星球之间有航线.由于超时空隧道的存在,从一个星球到另一个星球时间可能会倒流,而且,从星球a到b耗费的时 ...

  6. codevs3160 最长公共子串

    传送门:http://codevs.cn/problem/3160/ [题解] CTSC前复习模板 sa的模板..记住基数排序就够了(还有height) 还有就是sa[i]表示排名为i的后缀是啥..r ...

  7. szoj461【四校联考0430】挑战

    传送门:(涉及版权忽略) [题解] 我们发现n的范围很小,提示我们可以折半,然后我们就会了O(T2^(n/2)*n)的做法,然而会T. 考虑如何优化.直接排序会多一个log(2^(n/2))也就是n, ...

  8. 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会

    [算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...

  9. [FZU2261]浪里个浪

    TonyY是一个喜欢到处浪的男人,他的梦想是带着兰兰姐姐浪遍天朝的各个角落,不过在此之前,他需要做好规划. 现在他的手上有一份天朝地图,上面有n个城市,m条交通路径,每条交通路径都是单行道.他已经预先 ...

  10. 亮相SIGGRAPH 太极拳三维教学App制作揭秘

    http://news.hxsd.com/CG-animation/201208/663303.html 编者按:<My Tai Chi>是一系列基于移动平台的三维互动产品,由北京七星汇工 ...