问题描述:
一句话题目:让你求(sqrt(2)+sqrt(3))^(2*n) mod 1024.
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 5Sample Output
9 97 841
题目分析:
先给出一个大佬的博客,自己太鶸了,这个公式和一开始的想法完全不一样,后来发现我最初的想法是错的(千万别想着用double 取模)
然后就可以根据那个矩阵来写代码了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int mod=1024;
struct matrix//构建矩阵
{
int f[3][3];
matrix operator* (const matrix &a) const {
matrix res;
for (int i=1;i<=2;i++) {
for (int j=1;j<=2;j++) {
res.f[i][j]=0;
for (int k=1;k<=2;k++)
res.f[i][j]=(res.f[i][j]+(*this).f[i][k]*a.f[k][j])%mod;
}
}
return res;
}
}a,b;
void init()//预处理矩阵
{
b.f[1][1]=5,b.f[1][2]=12;
b.f[2][1]=2,b.f[2][2]=5;
a.f[1][1]=5;
a.f[2][1]=2;
}
matrix fast_pow(matrix base,int k)//快速幂
{
matrix ans=base;
while (k) {
if (k&1)
ans=base*ans;
base=base*base;
k>>=1;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while (t--) {
int n;
scanf("%d",&n);
if (n==1) { printf("%d\n",9); continue;}
n-=2;
init();
struct matrix cur,ans;
cur=b;
cur=fast_pow(cur,n);
ans=cur*a;
printf("%d\n",(2*ans.f[1][1]-1)%mod);
}
return 0;
}