HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)

时间:2023-12-21 09:35:50

Arc of Dream

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

Problem Description
An Arc of Dream is a curve defined by following function:
HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
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)

很明显是要构造矩阵,然后用矩阵快速幂求解。

|   AX   0   AXBY   AXBY  0  |

|   0   BX  AYBX    AYBX  0  |

{a[i-1]   b[i-1]   a[i-1]*b[i-1]  AoD[i-1]  1}* |   0   0   AXBX    AXBX   0  |  = {a[i]   b[i]   a[i]*b[i]  AoD[i]  1}

|    0   0     0          1     0    |

|  AY  BY   AYBY   AYBY   1   |

然后就可以搞了

注意n==0的时候,输出0

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/20 12:21:51
File Name :F:\2013ACM练习\2013多校9\1001.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MOD = 1e9+;
struct Matrix
{
int mat[][];
void clear()
{
memset(mat,,sizeof(mat));
}
void output()
{
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++)
printf("%d ",mat[i][j]);
printf("\n");
}
}
Matrix operator *(const Matrix &b)const
{
Matrix ret;
for(int i = ;i < ;i++)
for(int j = ;j < ;j++)
{
ret.mat[i][j] = ;
for(int k = ;k < ;k++)
{
long long tmp = (long long)mat[i][k]*b.mat[k][j]%MOD;
ret.mat[i][j] = (ret.mat[i][j]+tmp);
if(ret.mat[i][j]>MOD)
ret.mat[i][j] -= MOD;
}
}
return ret;
}
};
Matrix pow_M(Matrix a,long long n)
{
Matrix ret;
ret.clear();
for(int i = ;i < ;i++)
ret.mat[i][i] = ;
Matrix tmp = a;
while(n)
{
if(n&)ret = ret*tmp;
tmp = tmp*tmp;
n>>=;
}
return ret;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long n;
int A0,AX,AY;
int B0,BX,BY;
while(scanf("%I64d",&n) == )
{
scanf("%d%d%d",&A0,&AX,&AY);
scanf("%d%d%d",&B0,&BX,&BY);
if(n == )
{
printf("0\n");
continue;
}
Matrix a;
a.clear();
a.mat[][] = AX%MOD;
a.mat[][] = (long long)AX*BY%MOD;
a.mat[][] = BX%MOD;
a.mat[][] = (long long)AY*BX%MOD;
a.mat[][] = (long long)AX*BX%MOD;
a.mat[][] = ;
a.mat[][] = AY%MOD;
a.mat[][] = BY%MOD;
a.mat[][] = (long long)AY*BY%MOD;
a.mat[][] = ;
a.mat[][] = a.mat[][];
a.mat[][] = a.mat[][];
a.mat[][] = a.mat[][];
a.mat[][] = a.mat[][];
//a.output();
a = pow_M(a,n-);
//a.output();
long long t1 = (long long)A0*B0%MOD;
long long ans = t1*a.mat[][]%MOD + t1*a.mat[][]%MOD;
if(ans > MOD)ans -= MOD;
ans += (long long)A0*a.mat[][];
ans %= MOD;
ans += (long long)B0*a.mat[][];
ans %= MOD;
ans += (long long)a.mat[][];
ans %= MOD;
printf("%d\n",(int)ans);
}
return ;
}