HDU-2256 Problem of Precision (神奇)
好强啊,像我这种就会以为是直接double乘一乘,我好菜。
/*
求
(5+2sqrt(6))^n
[x+y*sqrt(6)]*[5+2sqrt(6)]=5x+12y+(5y+2x)
x 5 12
y 2 5
*/
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
typedef long long LL;
int t,n,k,p=1024;
struct mat{
int a[5][5];
void init(){memset(a,0,sizeof(a));}
};
int add(int x,int y){
return ((x+y)%p+p)%p;
}
mat mul(mat x,mat y){
mat z;z.init();
rep(i,1,2) rep(j,1,2) rep(k,1,2)
z.a[i][j]=add(z.a[i][j],1ll*x.a[i][k]*y.a[k][j]%p);
return z;
}
mat power(mat x,int y){
mat ret=x;y--;
while(y){
if(y&1) ret=mul(ret,x);
x=mul(x,x);y>>=1;
}
return ret;
}
int calc(int x,int y){
return add(0,2*x-1);
}
int main(){
mat x,y;
cin>>t;
x.init();y.init();
x.a[1][1]=5,x.a[2][1]=2;
y.a[1][1]=5,y.a[1][2]=12;
y.a[2][1]=2,y.a[2][2]=5;
while(t--){
scanf("%d",&n);
if(n==1){
printf("%d\n",calc(x.a[1][1],x.a[2][1]));
continue;
}
mat ans=mul(power(y,n-1),x);
printf("%d\n",calc(ans.a[1][1],ans.a[2][1]));
}
return 0;
}
QwQwQ