由 a(n) = 2*a(2)*a(n-1) - a(n-2)
let p = 2*a(2);
==>a(n) =p*a(n-1) - a(n-2)
==>a(n)^2 = p^2*a(n-1)^2 - 2*p*a(n-1)*a(n-2) + a(n-2)^2................(1)
let s(n) =sum( a(i) );
s(n) = s(n-1) + a(n)^2 ................................................................... (2)
a(n)*a(n-1) = p*a(n-1)^2 - a(n-1)*a(n-2)...................(3)
so we have:
a(n-1)^2 0 , 1 , 0 , 0 a(n-2)^2
a(n)^2 = 1 , p^2, -2*p, 0 * a(n-1)^2
a(n)*a(n-1) 0 , p , -1 , 0 a(n-1)*a(n-2)
s(n-1) 0 , 1 , 0 , 1 s(n-2)
use matrix to do this.
Problem is sovle now.
Ps: At begin I got TLE then I chang the methed but then Igot WA for amount of time becauseI used lld instand of I64d.BE remmenber to use I64d in hdu oj.
MY code :
#include<cstdio>
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define nMax 10
#define LL long long
LL mod;
template<typename T>
struct Matrix{
T mtix[nMax][nMax];
int n;
Matrix(int n=1):n(n) {
FOR(i,0,n-1) FOR(j,0,n-1) mtix[i][j] = 0;
FOR(i,0,n-1) mtix[i][i] = 1;
}
friend Matrix operator * (const Matrix& a,const Matrix& b) {
Matrix c(a.n);
FOR(i,0,a.n-1) FOR(j,0,a.n-1) {
c.mtix[i][j] = 0;
FOR(k,0,a.n-1) if(a.mtix[i][k] && b.mtix[k][j]){
c.mtix[i][j] += a.mtix[i][k] * b.mtix[k][j] ;
c.mtix[i][j]%=mod;
}
}
return c;
}
friend Matrix Exp(Matrix a,int n){
Matrix c(a.n);
Matrix b = a;
while(n){
if(n&1) c=c*b;
n >>= 1;
b = b*b;
}
return c;
}
void init(T p) {
p%=mod;
FOR(i,0,n-1) FOR(j,0,n-1) mtix[i][j] = 0 ;
mtix[1][1] = p*p%mod;
mtix[0][1] = mtix[1][0] = mtix[3][1] = mtix[3][3] = 1;
mtix[1][2] = ((-2*p) % mod + mod) % mod;
mtix[2][1] = p;
mtix[2][2] = ((-1 ) % mod + mod) % mod;
}
};
LL b[5];
int main(){
//freopen("input.txt","r",stdin);
int n;
LL a;
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d%d%I64d",&a,&n,&mod);
a%=mod;
if(mod == 1){
printf("0\n");
continue;
}
Matrix<LL> s(4);
s.init(a*2);
b[1]=a*a%mod,b[0]=1LL,b[2]=a,b[3]=1;
s = Exp( s ,n-1 );
LL ans = 0LL;
FOR(i,0,3) ans = (ans+s.mtix[3][i]*b[i]) % mod;
printf("%I64d\n",ans);
}
return 0;
}
![](https://img-my.csdn.net/uploads/201304/16/1366116975_6890.png)