HDU 2256(数论,构造二分矩阵)

Problem of Precision

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 240    Accepted Submission(s): 117

Problem Description
HDU 2256(数论,构造二分矩阵)_第1张图片
 

 

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

 

 

/************************************************************************ 打表出前几项的结果:9,97,969,9601,95049,940897 用x表示(sqrt(2.0)+sqrt(3.0))^2 先求出 x = 5+2*sqrt(6.0) -> (5*2-1)%1024=9 x^2 = 49+20*sqrt(6.0) -> (49*2-1)%1024=97 x^3 = 485+198*sqrt(6.0) -> (485*2-1)%1024=969 .... 现在我们就能找出规律了。 49 = 5*5+2*2*6 = 5*5+2*12 20 = 5*2 + 2*5 485 = 49*5+20*2*6 = 49*5+20*12 198 = 49*2 + 20*5 (如果不确定你可以在多做一位就知道了) | 5 12 | * | 5 | = | 49 | * | 5 12 | = | 485 | ... | 2 5 | | 2 | | 20 | | 2 5 | | 198 | 看上面的等式我想大家都应该知道怎么做了吧? 嘻嘻,祝你们快AC吧 (*^__^*) ************************************************************************/ #include <iostream> #include <math.h> using namespace std; #define N 2 #define ll __int64 #define MOD 1024 struct Mat { int martix[N][N]; }; Mat res,q,tp,tp1; Mat Martix_Mul(Mat &a,Mat &b) { int i,j,k; Mat c; for (i=0;i<N;i++) { for (j=0;j<N;j++) { c.martix[i][j]=0.0; for (k=0;k<N;k++) { if(a.martix[i][k] && b.martix[k][j]) c.martix[i][j]=(c.martix[i][j]+a.martix[i][k]*b.martix[k][j])%MOD; } } } return c; } void er_fun(ll x) { tp=res; tp1=q; while (x) { if(x&1) tp=Martix_Mul(tp,tp1); tp1=Martix_Mul(tp1,tp1); x>>=1; } } int main() { int t,i,j; ll n; int sum; for (i=0;i<N;i++) { for (j=0;j<N;j++) { res.martix[i][j]=(i==j); } } q.martix[0][0]=5; q.martix[0][1]=12; q.martix[1][0]=2; q.martix[1][1]=5; scanf("%d",&t); while (t--) { scanf("%I64d",&n); if(n==1) {printf("9/n");continue;} if(n==2) {printf("97/n");continue;} er_fun(n-1); sum=0; sum=(sum+tp.martix[0][0]*5)%MOD; if(sum<0) sum+=MOD; sum=(sum+tp.martix[0][1]*2)%MOD; if(sum<0) sum+=MOD; printf("%d/n",(sum*2-1)%MOD); } return 0; }

你可能感兴趣的:(HDU 2256(数论,构造二分矩阵))