Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1325 Accepted Submission(s): 796
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/452366445ca2fdd383e1e9275ee65a60.jpeg)
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
题意还是很简单,想了半天也不知道怎么玩,明显最后的答案是类似:a+b*sqrt(6),既然这样为了不损失精度,我们保留sqrt(6)到最后,直接先求第n项的a,b
我弱鸡的数学已经没办法了只有靠队友了。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int n,k;
int mod=1024;
struct Matrix
{
int m[10][10];
}M;
Matrix Mult(Matrix a,Matrix b)
{
Matrix ans;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ans.m[i][j]=0;
for(int k=0;k<n;k++)
{
ans.m[i][j]+=a.m[i][k]*b.m[k][j];
ans.m[i][j]%=mod;
}
}
}
return ans;
}
Matrix quickpow(Matrix a,int b)
{
Matrix ans;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
ans.m[i][j]=1;
else
ans.m[i][j]=0;
}
}
while(b)
{
if(b&1)
ans=Mult(ans,a);
a=Mult(a,a);
b/=2;
}
return ans;
}
int a[10];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
Matrix M,N;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
M.m[i][j]=N.m[i][j]=0;
}
M.m[0][0]=5,M.m[0][1]=12;
M.m[1][0]=2,M.m[1][1]=5;
N.m[0][0]=5,N.m[1][0]=2;
int m=n;
n=2;
M=quickpow(M,m-1);
N=Mult(M,N);
printf("%d\n",(2*N.m[0][0]-1)%mod);
}
return 0;
}