题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256
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
题意:
求
(2√+3√)2n
%1024向下取整的值
思路:
首先平方拿下来,即求
(5+26√)n
%1024,
(5+26√)n
=
an+bn6√
,
令
F(n)=an+bn6√
联立可以推出矩阵为 (52125) * (an−1bn−1) = (anbn) 。
到此并没有结束,由于出现了根号,double取模存在误差,所以不能正常取模,应当求向下取整的值。
(5+26√)n+(5−26√)n=2an , 6√≈2.4 ,因此 (5−26√)n≈0 , (5+26√)n 向下取整等于2 an−1 .
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<ctype.h>
#include<time.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#include<map>
#include<vector>
#include<sstream>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
void fre(){freopen("in.txt","r",stdin);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define zero(a) fabs(a)<=eps
#define equal(a,b) zero(a-b)
const ld pi=acos(-1);
double e=2.718281828;
const double eps=1e-6;
const int MOD=1024;
const int N=2;
//ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
struct Matrix{
int mat[N][N];
Matrix operator*(const Matrix& m)const{
Matrix tmp;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
tmp.mat[i][j]=0;
for(int k=0;k<N;k++)
{
tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
tmp.mat[i][j]%=MOD;
}
}
}
return tmp;
}
};
int Pow(Matrix &m,int k)
{
if(k==1)
return 9;
k--;
Matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(int i=0;i<N;i++)
ans.mat[i][i]=1;
while(k)
{
if(k&1)
ans=ans*m;
k>>=1;
m=m*m;
}
int x=(ans.mat[0][0]*5+ans.mat[0][1]*2)%MOD;
return (2*x-1)%MOD;
}
int main()
{
//fre();
int cas,n;
Matrix m;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
m.mat[0][0]=5;m.mat[1][1]=5;
m.mat[1][0]=2;m.mat[0][1]=12;
printf("%d\n",Pow(m,n));
}
return 0;
}