Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 614 Accepted Submission(s): 344
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
Source
Recommend
lcy
参考别人的解题思路,写的代码,感觉他的解题思路写的很清晰。下面便是他的解题思路:
解题思路原文
这个题目用快速幂+fmod是AC不了的,应该是浮点数精度的原因,推导的过程都体现在图上了,剩下的工作就是二分矩阵求解了。
![](https://img-my.csdn.net/uploads/201210/09/1349762851_8149.jpg)
#include<stdio.h>
int Matrix[3][3];
int tmp1[3][3],tmp2[3][3];
void fun(int n) //求矩阵的快速幂
{
int i,j,k;
n-=2;
Matrix[1][1]=tmp1[1][1]=5;
Matrix[1][2]=tmp1[1][2]=12;
Matrix[2][1]=tmp1[2][1]=2;
Matrix[2][2]=tmp1[2][2]=5;
while(n)
{
if(n%2)
{
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
tmp2[i][j]=Matrix[i][j];
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
{
Matrix[i][j]=0;
for(k=1;k<=2;k++)
Matrix[i][j]=(Matrix[i][j]+tmp1[i][k]*tmp2[k][j])24;
}
}
n/=2;
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
tmp2[i][j]=tmp1[i][j];
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
{
tmp1[i][j]=0;
for(k=1;k<=2;k++)
tmp1[i][j]=(tmp1[i][j]+tmp2[i][k]*tmp2[k][j])24;
}
}
}
int main(void)
{
int T,n;
// freopen("d:\\in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
fun(n);
if(n==1) //n==1属于特殊情况
printf("9\n");
else
printf("%d\n",(2 * (Matrix[1][1]*5+Matrix[1][2]*2 )-1)24);
}
return 0;
}