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,AY,B0,BX,BY;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    int i,j,k;
    memset(c.A,0,sizeof(c.A));
    for(i=1; i<=R; i++)
        for(j=1; j<=C; j++)
            for(k=1; k<=5; k++)
                c.A[i][j]=(c.A[i][j]+(A[i][k]*b.A[k][j])%MOD)%MOD;
    return c;
}

int main()
{
    int i,j;
    while(~scanf("%lld",&N))
    {

        scanf("%lld%lld%lld",&A0,&AX,&AY);
        scanf("%lld%lld%lld",&B0,&BX,&BY);

        A0=A0%MOD;
        AX=AX%MOD;
        AY=AY%MOD;

        B0=B0%MOD;
        BX=BX%MOD;
        BY=BY%MOD;

        if(N==0)
        {
            printf("0\n");
            continue;
        }

        Matrix JZ;
        JZ.A[1][1]=1;JZ.A[1][2]=0;JZ.A[1][3]=0;JZ.A[1][4]=0;JZ.A[1][5]=0;
        JZ.A[2][1]=1;JZ.A[2][2]=(AX*BX)%MOD;JZ.A[2][3]=0;JZ.A[2][4]=0;JZ.A[2][5]=0;
        JZ.A[3][1]=0;JZ.A[3][2]=(AX*BY)%MOD;JZ.A[3][3]=AX%MOD;JZ.A[3][4]=0;JZ.A[3][5]=0;
        JZ.A[4][1]=0;JZ.A[4][2]=(AY*BX)%MOD;JZ.A[4][3]=0;JZ.A[4][4]=BX%MOD;JZ.A[4][5]=0;
        JZ.A[5][1]=0;JZ.A[5][2]=(AY*BY)%MOD;JZ.A[5][3]=AY%MOD;JZ.A[5][4]=BY%MOD;JZ.A[5][5]=1;

        Matrix c;
        for(i=1; i<=9; i++)
        {
            for(j=1; j<=9; j++)
            {
                if(i==j) c.A[i][j]=1;
                else c.A[i][j]=0;
            }
        }

        long long b=N-1;
        R=5;
        C=5;
        while(b>0)
        {
            if(b%2==1) c=c*JZ,b--;
            else JZ=JZ*JZ,b=b/2;
        }
        Matrix CS;
        CS.A[1][1]=(A0*B0)%MOD;
        CS.A[1][4]=((B0*BX)%MOD+BY)%MOD;
        CS.A[1][3]=((A0*AX)%MOD+AY)%MOD;
        CS.A[1][2]=(CS.A[1][3]*CS.A[1][4])%MOD;
        CS.A[1][5]=1;

        R=1;
        C=5;
        if(N!=1) CS=CS*c;

        printf("%lld\n",CS.A[1][1]);
    }
    return 0;
}
时间: 2024-10-06 18:05:52

HDU 4686 Arc of Dream的相关文章

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

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(矩阵快速幂乘法)

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(矩阵加速递推)

题目大意:就是给你你个有两个递推公式乘起来的式子,让你求出第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 矩阵快速幂,线性同余 难度: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 自己推 矩阵快速幂

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] =

HDOJ 4686 Arc of Dream 矩阵高速幂

矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

HDOJ 4686 Arc of Dream 矩阵快速幂

矩阵快速幂: 根据关系够建矩阵 , 快速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

【HDOJ 4686】 Arc of Dream (矩阵快速幂)

[HDOJ 4686] Arc of Dream (矩阵快速幂) 两个公式 a(i) = a(i-1)*Ax+Ay b(i) = b(i-1)*Bx+By 求 0~(n-1) 的a(i)*b(i) 初始矩阵为                                       求幂矩阵为 a0                                                      Ax          0           0          0