Problem of Precision
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1549 Accepted Submission(s): 942
Problem Description
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
求(sqrt(2) + sqrt(3)) ^ 2n MOD 1024
这图片是我盗的,公式推的很溜,不能直接用(Xn*Yn*sqrt(6))%1024,double涉及精度问题,这样算误差较大,直接用2*Xn-1即可,自己还是太菜,T_T,= =
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))
const int inf=0x3f3f3f3f;
const int mod=1024;
ll ans[3][3],a[3][3],b[2];
int t,n;
void multi(ll a[][3],ll b[][3])//矩阵乘积
{
ll tmp[3][3];
mem(tmp,0);
for1(i,2)
for1(j,2)
for1(k,2)
tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%mod;
for1(i,2)
for1(j,2)
a[i][j]=tmp[i][j];
}
void init()//初始化数组
{
mem(ans,0);
for1(i,2)
ans[i][i]=1;
a[1][1]=5,a[1][2]=12,a[2][1]=2,a[2][2]=5;
}
void pow(int x)//矩阵快速幂
{
while(x)
{
if(x&1)multi(ans,a);
x>>=1;
multi(a,a);
}
}
int main()
{
b[0]=5,b[1]=2;
sf("%d",&t);
while(t--)
{
init();//每次都要初始化数组
sf("%d",&n);
pow(n-1);
int x=((ans[1][1]*b[0]+ans[1][2]*b[1])*2-1)%mod;//计算Xn
pf("%d\n",x);
}
return 0;
}