The title asks for an integer part of (√2+√3) 2n and then mod 1024.
(√2+√3) 2n= (5+2√6) n
If the direct calculation, with a double value, when n is very large, the loss of precision will become larger, unable to get the desired result.
We find that (5+2√6) n+ (5-2√6) n is an integer (the 2√6 's even power will be offset off or minus), and (5-2√6) n is less than 1. So we just need to ask for a Sn-1. Make
an= (5+2√6) n; bn= (5-2√6) n.
Sn=an+bn sn is an integer.
sn* ((5+2√6) + (5-2√6)) =sn*10
sn*10= (5+2√6) n+1+ (5-2√6) n+1+ (5+2√6) n-1+ (5-2√6) n-1
Sn*10=sn+1+sn-1
Recursive type: sn=10*sn-1-sn-2
Then it transforms to the matrix to find the SN quickly.
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intMod=1024x768;Const intn=2;structmat{intmat[n][n];} A Mat Multiply (Mat A, Mat b) {mat C; memset (C.mat,0,sizeof(C.mat)); for(intK =0; K <2; ++k) for(inti =0; I <2; ++i)if(A.mat[i][k]) for(intj =0; J <2; ++j)if(B.mat[k][j]) c.mat[i][j]= (C.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod; returnC;} Mat Quickpower (Mat A,intk) {Mat C; memset (C.mat,0,sizeof(C.mat)); for(inti =0; I <2; ++i) c.mat[i][i]=1; for(; k; k >>=1) { if(k&1) C =Multiply (c,a); A=Multiply (a,a); } returnC;}voidInitmat (Mat &A) {a.mat[0][0]=Ten; a.mat[0][1]=-1; a.mat[1][0]=1; a.mat[1][1]=0;}intMain () {//freopen ("In.txt", "R", stdin); intT; scanf ("%d",&t); while(t--) { intN; scanf ("%d",&N); if(n==1) printf ("9\n"); Else if(n==2) printf ("97\n"); Else{Initmat (a); A=quickpower (a,n-2); intAns= (a.mat[0][0]*98+a.mat[0][1]*Ten-1)%1024x768;//we're asking for s[n]-1. while(ans<0) ans+=1024x768; printf ("%d\n", ans); } } return 0;}
HDU 2256 Problem of Precision theory matrix fast Power