Tower
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2380 Accepted Submission(s): 544
Recently in math class, the concept of volume was introduced to Alan. Consequently, he wants to compute the volume of his tower now. The lengths of cuboids bases (from top to bottom) are constructed by Alan in the following way:
1. Length a1 of the first square is one.
2. Next, Alan fixes the length a2 of the second square.
3. Next, Alan calculates the length an (n > 2) by 2*a2*(an-1)-(an-2). Do not ask why he chose such
a formula; let us just say that he is a really peculiar young fellow. For example, if Alan fixes a2 = 2, then a3 = 8 -a1 = 7; see Figure 1. If Alan fixes a2 = 1, then an = 1 holds for all n belong to N; see Figure 2.
Now Alan wonders if he can calculate the volume of tower of N consecutive building bricks. Help Alan and write the program that computes this volume. Since it can be quite large, it is enough to compute the answer modulo given natural number m.
這道題是比較基礎的矩陣快速冪,但是卡了常數。實質上還是求解(A(1))^2+(A(2))^2+……+(A(n))^2的值。這個與HDU 3306的求解結果是一樣的。
http://blog.csdn.net/u012294939/article/details/43984417
不過需要注意的就是矩陣中有負數,取模的時候一定要將負數的取模結果變成正數再進行計算。還有爲了防止超時,要減少取模的次數。所以在快速冪之前就應該對矩陣中的數進行取模,將負數的取模結果變成正數,這樣在快速冪的矩陣乘法中就只需考慮正數相乘。不需要再加mod然後再模mod了。這樣便減少了取模次數。
參考代碼:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
struct mat
{
ll d[4][4];
}A,B,E;
int t;
ll a2,n,m;
mat multi(mat &a,mat &b)
{
mat ans;
rep(i,0,4)
{
rep(j,0,4)
{
ans.d[i][j]=0;
rep(k,0,4)
if(a.d[i][k]&&b.d[k][j])
ans.d[i][j]=(ans.d[i][j]+a.d[i][k]*b.d[k][j])%m;
}
}
return ans;
}
mat quickmulti(mat &a,ll n)
{
mat ans=E;
while(n)
{
if(n&1)
{
n--;
ans=multi(ans,a);
}
else
{
n>>=1;
a=multi(a,a);
}
}
return ans;
}
int main()
{
CLR(E.d);
rep(i,0,4)
E.d[i][i]=1;
read(t);
while(t--)
{
scanf("%I64d%I64d%I64d",&a2,&n,&m);
if(n==1)
{
printf("1\n");
continue;
}
if(n==2)
{
printf("%d\n",(1+a2*a2)%m);
continue;
}
if(a2==1)
{
printf("%d\n",n%m);
continue;
}
CLR(A.d);
A.d[0][0]=1,A.d[0][1]=(4*((a2*a2)%m))%m,A.d[0][2]=((-4*a2)%m+m)%m,A.d[0][3]=1;
A.d[1][1]=A.d[0][1],A.d[1][2]=A.d[0][2],A.d[1][3]=A.d[0][3];
A.d[2][1]=(2*a2)%m,A.d[2][2]=-1+m;
A.d[3][1]=1;
CLR(B.d);
B.d[0][0]=(1+a2*a2)%m,B.d[1][0]=(a2*a2)%m,B.d[2][0]=a2%m,B.d[3][0]=1;
mat ans=quickmulti(A,n-2);
ans=multi(ans,B);
printf("%I64d\n",ans.d[0][0]);
}
}