hdu----(4686)Arc of Dream(矩阵快速幂)

时间:2023-03-08 17:19:52
hdu----(4686)Arc of Dream(矩阵快速幂)

Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2010    Accepted Submission(s): 643

Problem Description
An Arc of Dream is a curve defined by following function:
hdu----(4686)Arc of Dream(矩阵快速幂)
where
a0 = A0
ai = ai-1*AX+AY
b0 = B0
bi = bi-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
Input
There are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 1018, and all the other integers are no more than 2×109.
Output
For each test case, output AoD(N) modulo 1,000,000,007.
Sample Input
1
1 2 3
4 5 6
2
1 2 3
4 5 6
3
1 2 3
4 5 6
Sample Output
4
134
1902
Author
Zejun Wu (watashi)
Source
这道题的分析,其实应该这样分析:
  我们看到这么个式子hdu----(4686)Arc of Dream(矩阵快速幂)    然后我们知道ai=ai-1*AX+AY   ;     bi=bi-1*BX+BY;
   ai*bi =AXBX*ai-1*bi-1+AXBY*ai-1+BXAY*bi-1+AY*BY;
   对于这样一个式子,我们不妨构造这样一个矩阵......
如:
  |ai*bi|     |AX*BX , AXBY , BXAY , AYBY, 0  |^n-1   | ai-1*ai-1 |
  |  ai  |     |  0       , AX    , 0        , AY   , 0  |           |  ai-1       |
  |  bi  |  = |  0       , 0     ,   BX    , BY    , 0  |   *      |  bi-1       |
  |   1  |     |  0       , 0     ,  0       ,    1   , 0  |           |     1        |
  |  sn  |     | AXBX  , AXBY,  BXAY,  AYBY, 1 |            |     sn-1   |
然后就是快速矩阵...
 #define LOCAL
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std; const LL mod=; struct node
{
LL mat[][];
void init(int v){
for(int i=;i<;i++){
for(int j=;j<;j++)
if(i==j)
mat[i][j]=v;
else
mat[i][j]=;
}
}
}; LL AO,BO,AX,AY,BX,BY,n;
node ans,cc; void init(node &a)
{
a.mat[][]=a.mat[][]=(AX*BX)%mod;
a.mat[][]=a.mat[][]=(AX*BY)%mod;
a.mat[][]=a.mat[][]=(BX*AY)%mod;
a.mat[][]=a.mat[][]=(AY*BY)%mod;
a.mat[][]=a.mat[][]=a.mat[][]=a.mat[][]=;
a.mat[][]=a.mat[][]=a.mat[][]=;
a.mat[][]=a.mat[][]=a.mat[][]=a.mat[][]=;
a.mat[][]=a.mat[][]=;
a.mat[][]=AX;
a.mat[][]=AY;
a.mat[][]=BX;
a.mat[][]=BY;
} void Matrix(node &a, node &b)
{
node c;
c.init();
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
}
}
} for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a.mat[i][j]=c.mat[i][j];
}
}
} void pow(node &a,LL w )
{
while(w>)
{
if(w&) Matrix(ans,a);
w>>=1L;
if(w==)break;
Matrix(a,a);
}
} int main()
{
LL sab;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%I64d",&n)!=EOF)
{
scanf("%I64d%I64d%I64d",&AO,&AX,&AY);
scanf("%I64d%I64d%I64d",&BO,&BX,&BY);
if(n==){ printf("0\n");
continue;
}
AO%=mod;
BO%=mod;
AX%=mod;
AY%=mod;
BX%=mod;
BY%=mod;
ans.init();
init(cc);
pow(cc,n-); sab=(AO*BO)%mod;
LL res=(ans.mat[][]*sab)%mod+(ans.mat[][]*AO)%mod+(ans.mat[][]*BO)%mod+ans.mat[][]%mod+(AO*BO)%mod;
printf("%I64d\n",res%mod);
}
return ;
}