题意很简单,给出公式,求结果,将公式转化后可以用矩阵快速幂求出来,细节问题,错了很多次,不够熟练。
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#include<stdio.h>
#include<string>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define LL __int64
#define M 1000000007
LL matrix[5][5];
class Matrix
{
public:
LL c[5][5];
Matrix()
{
for(LL i = 0;i < 5;i ++)
for(LL j = 0;j < 5;j ++)
c[i][j] = 0;
}
void deal()
{
for(LL i = 0;i < 5;i ++)
for(LL j = 0;j < 5;j ++)
c[i][j] = matrix[i][j];
}
};
Matrix mul(const Matrix & t1,const Matrix & t2)
{
LL i,j,k;
Matrix t3;
for(i = 0;i < 5;i ++)
{
for(j = 0;j < 5;j ++)
{
for(k = 0;k < 5;k ++)
{
t3.c[i][j] = (t3.c[i][j] + t1.c[i][k]*t2.c[k][j])%M;
}
}
}
return t3;
}
Matrix power(LL n)
{
Matrix a,b;
b.deal();
if(n==1)
return b;
else if(n%2==1)
{
a=(power(n/2));
return mul(mul(a,a),b);
}
else
{
a=(power(n/2));
return mul(a,a);
}
}
int main()
{
LL a0,ax,ay,b0,bx,by,a,b,c,d;
LL n;
while(scanf("%I64d",&n) != EOF)
{
scanf("%I64d%I64d%I64d",&a0,&ax,&ay);
scanf("%I64d%I64d%I64d",&b0,&bx,&by);
if(n == 0)
{
puts("0");
continue;
}
a = ax*bx%M;
b = ax*by%M;
c = ay*bx%M;
d = ay*by%M;
memset(matrix,0,sizeof(matrix));
matrix[0][0] = a;
matrix[0][4] = 1;
matrix[1][0] = b;
matrix[1][1] = ax;
matrix[2][0] = c;
matrix[2][2] = bx;
matrix[3][0] = d;
matrix[3][1] = ay;
matrix[3][2] = by;
matrix[3][3] = 1;
matrix[4][4] = 1;
Matrix ans;
ans.c[0][0] = a0*b0%M;
ans.c[0][1] = a0;
ans.c[0][2] = b0;
ans.c[0][3] = 1;
n--;
if(n)
{
ans = mul(ans,power(n));
}
printf("%I64d\n",(ans.c[0][0] + ans.c[0][4])%M);
}
return 0;
}