Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1159 Accepted Submission(s): 688
Total Submission(s): 1159 Accepted Submission(s): 688
归纳推理+矩阵快速幂;
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/452366445ca2fdd383e1e9275ee65a60.jpeg)
推理过程(引用大牛的):
1 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值
3 这里很多人会直接认为结果等于(an+bn*sqrt(6))%1024,但是这种结果是错的,因为这边涉及到了double,必然会有误差,所以根double有关的取模都是错误的思路
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
Statistic | Submit | Discuss | Note
#include <iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
const int N=1e6+7;
struct Matrix
{
int x,y;
int A[10][10];
void Clean()
{
x=y=0;
memset(A,0,sizeof(A));
}
Matrix operator *(const Matrix& B)const
{
Matrix tmp;
tmp.Clean();
tmp.x=x,tmp.y=B.y;
for(int i=0;i<x;i++)
{
for(int j=0;j<B.y;j++)
{
for(int k=0;k<x;k++)
{
(tmp.A[i][j]+=A[i][k]*B.A[k][j])%=1024;
}
}
}
return tmp;
}
}S,ans;
int power(int n)
{
ans.Clean();
ans.x=2;
ans.y=1;
ans.A[0][0]=5;
ans.A[1][0]=2;
S.Clean();
S.x=S.y=2;
S.A[0][0]=5;
S.A[0][1]=12;
S.A[1][0]=2;
S.A[1][1]=5;
while(n)
{
if(n&1)
ans=S*ans;
S=S*S;
n>>=1;
}
return (ans.A[0][0]*2-1)%1024;
}
int main()
{
int n;
int t;
cin>>t;
while(t--)
{cin>>n;
printf("%d\n",power(n-1));
}
return 0;
}