HDU 4686 Arc of Dream(快速幂矩阵)

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

题目链接

再水一发,构造啊,初始化啊。。。wa很多次啊。。

#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define MOD 1000000007
#define LL long long
LL mat[][],p[][];
LL a1,b1,a0,b0;
int qmod(LL n)
{
LL c[][];
int i,j,k;
LL ans = ;
while(n)
{
if(n&)
{
memset(c,,sizeof(c));
for(i = ; i < ; i ++)
{
for(j = ; j < ; j ++)
{
for(k = ; k < ; k ++)
{
c[i][j] += mat[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(mat,c,sizeof(mat));
}
memset(c,,sizeof(c));
for(i = ; i < ; i ++)
{
for(j = ; j < ; j ++)
{
for(k = ; k < ; k ++)
{
c[i][j] += p[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(p,c,sizeof(p));
n >>= ;
}
ans = ((mat[][]*a0)%MOD*b0)%MOD;
ans = (ans + (mat[][]*a1)%MOD*b1)%MOD;
ans = (ans + (a1*mat[][])%MOD)%MOD;
ans = (ans + (b1*mat[][])%MOD)%MOD;
ans = (ans + mat[][])%MOD;
return ans;
}
int main()
{
LL n,ax,ay,bx,by;
int i,j;
while(cin>>n)
{
cin>>a0>>ax>>ay;
cin>>b0>>bx>>by;
memset(p,,sizeof(p));
a0 %= MOD;
ax %= MOD;
ay %= MOD;
b0 %= MOD;
bx %= MOD;
by %= MOD;
p[][] = p[][] = ;
p[][] = (ax*bx)%MOD;
p[][] = (ax*by)%MOD;
p[][] = (ay*bx)%MOD;
p[][] = (ay*by)%MOD;
p[][] = ax;
p[][] = ay;
p[][] = bx;
p[][] = by;
p[][] = ;
for(i = ; i < ; i ++)
{
for(j = ; j < ; j ++)
mat[i][j] = i == j ? :;
}
a1 = (a0*ax + ay)%MOD;
b1 = (b0*bx + by)%MOD;
if(n == )
printf("0\n");
else
printf("%d\n",qmod(n-));
}
return ;
}