Array Challenge
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 412 Accepted Submission(s): 221
Total Submission(s): 412 Accepted Submission(s): 221
Problem Description
There’s an array that is generated by following rule.
h0=2,h1=3,h2=6,hn=4hn−1+17hn−2−12hn−3−16
And let us define two arrays bnandan as below.
bn=3hn+1hn+9hn+1hn−1+9h2n+27hnhn−1−18hn+1−126hn−81hn−1+192(n>0)
an=bn+4n
Now, you have to print ⌊√(an)⌋ , n>1.
Your answer could be very large so print the answer modular 1000000007.
h0=2,h1=3,h2=6,hn=4hn−1+17hn−2−12hn−3−16
And let us define two arrays bnandan as below.
bn=3hn+1hn+9hn+1hn−1+9h2n+27hnhn−1−18hn+1−126hn−81hn−1+192(n>0)
an=bn+4n
Now, you have to print ⌊√(an)⌋ , n>1.
Your answer could be very large so print the answer modular 1000000007.
Input
The first line of input contains T (1 <= T <= 1000) , the number of test cases.
Each test case contains one integer n (1 < n <= 1015 ) in one line.
Each test case contains one integer n (1 < n <= 1015 ) in one line.
Output
For each test case print ⌊√(a_n )⌋ modular 1000000007.
Sample Input
3 4 7 9
Sample Output
1255 324725 13185773
题目解析:打表发现f
n=4*fn-1+17*fn-2-12*fn-3用矩阵快速幂即可解决问题。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
struct matrix
{
LL v[3][3];
matrix()
{
memset(v,0,sizeof(v));
}
};
matrix M,E,ans;
void Init()
{
for(int i=0; i<3; i++) E.v[i][i]=1;
M.v[0][0]=4;
M.v[0][1]=17;
M.v[0][2]=-12;
M.v[1][0]=1;
M.v[1][1]=0;
M.v[1][2]=0;
M.v[2][0]=0;
M.v[2][1]=1;
M.v[2][2]=0;
}
matrix mul(matrix a,matrix b)
{
matrix c;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
for(int k=0; k<3; k++)
c.v[i][j]=(c.v[i][j]%mod+a.v[i][k]*b.v[k][j]%mod+mod)%mod;
return c;
}
matrix pow(matrix p,LL k)
{
matrix tmp=E;
while(k)
{
if(k&1)
{
tmp=mul(tmp,p);
k--;
}
k>>=1;
p=mul(p,p);
}
return tmp;
}
int main()
{
LL n;
Init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
if(n==2) printf("31\n");
else if(n==3) printf("197\n");
else if(n==4) printf("1255\n");
else
{
ans=pow(M,n-4);
LL res=(ans.v[0][0]*1255%mod+ans.v[0][1]*197%mod+ans.v[0][2]*31%mod+mod)%mod;
printf("%lld\n",res);
}
}
return 0;
}
Source