# hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契

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

Source
HDOJ 2008 Summer Exercise（4）- Buffet Dinner

Recommend
lcy

f(n)=(2+3)2n=(5+26)n=An+Bn6$∵f(n)=(\sqrt2+\sqrt3)^{2n}=(5+2*\sqrt6)^n=A_n+B_n\sqrt6$

f(n1)=An1+Bn16$∴f(n−1)=A_{n−1}+B_{n−1}\sqrt6$

f(n)=(5+26)f(n1)$∵f(n)=(5+2\sqrt6)f(n−1)$

f(n)=(5An1+12Bn1)+(2An1+5Bn1)6$∴f(n)=(5A_{n−1}+12B_{n−1})+(2A_{n−1}+5B_{n−1})\sqrt6$

A1=5,B1=2.$A_1=5,B_1=2.$

(5+26))n=An+Bn6$∵(5+2\sqrt6))^n=A_n+B_n\sqrt6$

(526))n=AnBn6$∴(5−2\sqrt6))^n=A_n−B_n\sqrt6$

(5+26))n+(526)n=2An$∴(5+2\sqrt6))^n+(5−2\sqrt6)^n=2A_n$

(526))n<1$∵(5−2\sqrt6))^n<1$

(5+26))n=2An1$∴⌊(5+2\sqrt6))^n⌋=2A_n−1$

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define pi acos(-1.0)
#define EPS 1e-6    //log(x)
#define e exp(1.0); //2.718281828
//#define mod 1000000007
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
typedef long long LL;

#define debug(x) cout<<x<<endl;
#define debug2(x) cout<<x<<" ";

//#define MOD 10000007
LL MOD;
struct Mat{
int n,m;
LL mat[9][9];
};
Mat operator *(Mat a,Mat b){
Mat c;
memset(c.mat,0,sizeof(c.mat));
c.n = a.n,c.m = b.m;

for(int i=1;i<=a.n;i++){
for(int j=1;j<=b.m;j++){
for(int k=1;k<=a.m;k++){
c.mat[i][j] += (a.mat[i][k]*b.mat[k][j])%MOD;
c.mat[i][j] %= MOD;
}
}
}
return c;
}
Mat operator +(Mat a,Mat b){
Mat c;
memset(c.mat,0,sizeof(c.mat));
c.n = a.n,c.m = a.m;

for(int i=1;i<=a.n;i++){
for(int j=1;j<=a.m;j++){
c.mat[i][j] = (a.mat[i][j]+b.mat[i][j])%MOD;
}
}
return c;
}
Mat operator ^(Mat a,LL k){
Mat c;
memset(c.mat,0,sizeof(c.mat));
c.n = a.n,c.m = a.n;
for(int i=1;i<=a.n;i++)c.mat[i][i] = 1;

while(k){
if(k&1){
c = c*a;
}
a = a*a;
k>>=1;
}
return c;
}
void out(Mat a){
for(int i=1;i<=a.n;i++){
for(int j=1;j<=a.m;j++){
printf(j==a.m? "%I64d\n":"%I64d ",a.mat[i][j]);
}
}
}
LL quickPow(LL x, LL n, LL mm)
{
LL a = 1;
while (n)
{
a *= n&1 ? x : 1;
a %= mm;
n >>= 1 ;
x *= x;
x %= mm;
}
return a;
}
int main()
{

int T_T;
scanf("%d",&T_T);
MOD=1024;
Mat pp;
pp.n=pp.m=2;
pp.mat[1][1]=5;
pp.mat[1][2]=12;
pp.mat[2][1]=2;
pp.mat[2][2]=5;

while(T_T--){
scanf("%d",&n);
Mat ans=pp^(n-1);
LL out=(ans.mat[1][1]*5+ans.mat[1][2]*2)%MOD;
printf("%I64d\n",(2*out-1+MOD)%MOD);
}
return 0;
}

/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\  =  /O
____/---'\____
.'  \\|     |//  .
/  \\|||  :  |||//  \
/  _||||| -:- |||||-  \
|   | \\\  -  /// |   |
| \_|  ''\---/''  |   |
\  .-\__  -  ___/-. /
___. .'  /--.--\  . . __
."" '<  .___\_<|>_/___.'  >'"".
| | :  - \.;\ _ /;./ -  : | |
\  \ -.   \_ __\ /__ _/   .- /  /
======-.____-.___\_____/___.-____.-'======
=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I have a dream!A AC deram!!
orz orz orz orz orz orz orz orz orz orz orz
orz orz orz orz orz orz orz orz orz orz orz
orz orz orz orz orz orz orz orz orz orz orz

*/

`

• 本文已收录于以下专栏：

举报原因： 您举报文章：hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)