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

题目链接

再水一发,构造啊,初始化啊。。。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[5][5],p[5][5];
LL a1,b1,a0,b0;
int qmod(LL n)
{
LL c[5][5];
int i,j,k;
LL ans = 0;
while(n)
{
if(n&1)
{
memset(c,0,sizeof(c));
for(i = 0; i < 5; i ++)
{
for(j = 0; j < 5; j ++)
{
for(k = 0; k < 5; k ++)
{
c[i][j] += mat[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(mat,c,sizeof(mat));
}
memset(c,0,sizeof(c));
for(i = 0; i < 5; i ++)
{
for(j = 0; j < 5; j ++)
{
for(k = 0; k < 5; k ++)
{
c[i][j] += p[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(p,c,sizeof(p));
n >>= 1;
}
ans = ((mat[0][0]*a0)%MOD*b0)%MOD;
ans = (ans + (mat[0][1]*a1)%MOD*b1)%MOD;
ans = (ans + (a1*mat[0][2])%MOD)%MOD;
ans = (ans + (b1*mat[0][3])%MOD)%MOD;
ans = (ans + mat[0][4])%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,0,sizeof(p));
a0 %= MOD;
ax %= MOD;
ay %= MOD;
b0 %= MOD;
bx %= MOD;
by %= MOD;
p[0][0] = p[0][1] = 1;
p[1][1] = (ax*bx)%MOD;
p[1][2] = (ax*by)%MOD;
p[1][3] = (ay*bx)%MOD;
p[1][4] = (ay*by)%MOD;
p[2][2] = ax;
p[2][4] = ay;
p[3][3] = bx;
p[3][4] = by;
p[4][4] = 1;
for(i = 0; i < 5; i ++)
{
for(j = 0; j < 5; j ++)
mat[i][j] = i == j ? 1:0;
}
a1 = (a0*ax + ay)%MOD;
b1 = (b0*bx + by)%MOD;
if(n == 0)
printf("0\n");
else
printf("%d\n",qmod(n-1));
}
return 0;
}

HDU 4686 Arc of Dream(快速幂矩阵),布布扣,bubuko.com

时间: 2024-12-18 11:07:49

HDU 4686 Arc of Dream(快速幂矩阵)的相关文章

hdu 4686 Arc of Dream 自己推 矩阵快速幂

A.mat[0][0] = 1, A.mat[0][1] = 1, A.mat[0][2] = 0, A.mat[0][3] = 0, A.mat[0][4] = 0; A.mat[1][0] = 0, A.mat[1][1] = AX*BX%Mod, A.mat[1][2] = AX*BY%Mod, A.mat[1][3] = AY*BX%Mod, A.mat[1][4] = AY*BY%Mod; A.mat[2][0] = 0, A.mat[2][1] = 0, A.mat[2][2] =

HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目已经给出, Ai*Bi=Ax*Bx*(Ai-1*Bi-1)+Ax*By*Ai-1+Bx*Ay*Bi-1+Ay*By AoD(n)=AoD(n-1)+AiBi 构造向量I{AoD(i-1),Ai*Bi,Ai,Bi,1} 初始向量为I0={0,A0*B0,A0,B0,1} 构造矩阵A{ 1,0,0,0,

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

Problem Description An Arc of Dream is a curve defined by following function: 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. Eac

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

题目地址:HDU 4686 我去..因为忘记把函数里的k定义成64位的,导致TLE了一晚上...晕.. 这题没什么技巧,就是根据公式构造就行. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #in

HDU 4686 Arc of Dream(矩阵加速递推)

题目大意:就是给你你个有两个递推公式乘起来的式子,让你求出第n项的结果. 注意这种递推的需要把式子乘起来然后再构造矩阵. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2092    Accepted Submission(s): 664 Problem Description An Arc of Dream

HDU 4686 Arc of Dream

矩阵快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct Matrix { long long A[10][10]; Matrix operator*(Matrix b); }; const long long MOD=1000000007; long long R,C; long long N,A0,AX,

HDU 3306 Another kind of Fibonacci(快速幂矩阵)

题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 10007 #defin

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo