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 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.

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)

Source

2013 Multi-University Training Contest 9

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long int LL;

const LL mod=1000000007LL;

struct Matrix
{
    int x,y;
    LL m[6][6];
    Matrix() {x=y=5;memset(m,0,sizeof(m));}
    void one()
    {
        for(int i=0;i<5;i++) m[i][i]=1LL;
    }
    void show()
    {
        cout<<x<<" * "<<y<<endl;
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<y;j++)
                cout<<m[i][j]<<",";
            cout<<endl;
        }
    }
};

Matrix Mul(Matrix& a,Matrix& b)
{
    Matrix ret;
    ret.x=a.x; ret.y=b.y;
    for(int i=0;i<a.x;i++)
    {
        for(int j=0;j<b.y;j++)
        {
            LL temp=0;
            for(int k=0;k<b.y;k++)
            {
                temp=(temp+(a.m[i][k]*b.m[k][j])%mod)%mod;
            }
            ret.m[i][j]=temp%mod;
        }
    }
    return ret;
}

Matrix quickPow(Matrix m,LL x)
{
    Matrix e;
    e.one();
    while(x)
    {
        if(x&1LL) e=Mul(e,m);
        m=Mul(m,m);
        x/=2LL;
    }
    return e;
}

LL n,A0,B0,AX,AY,BX,BY;

Matrix init_matrix()
{
    Matrix ret;
    ret.m[0][0]=1;
    ret.m[1][0]=AY; ret.m[1][1]=AX;
    ret.m[2][0]=BY; ret.m[2][2]=BX;
    ret.m[3][0]=(BY*AY)%mod; ret.m[3][1]=(AX*BY)%mod;
    ret.m[3][2]=(BX*AY)%mod; ret.m[3][3]=(AX*BX)%mod;
    ret.m[4][3]=1LL; ret.m[4][4]=1LL;
    return ret;
}

Matrix Beg()
{
    Matrix beg;
    beg.m[0][0]=1;
    beg.m[1][0]=A0;
    beg.m[2][0]=B0;
    beg.m[3][0]=A0*B0%mod;
    return beg;
}

int main()
{
    while(cin>>n)
    {
        cin>>A0>>AX>>AY>>B0>>BX>>BY;
        A0=A0%mod; AX=AX%mod; AY=AY%mod;
        B0=B0%mod; BX=BX%mod; BY=BY%mod;
        Matrix m=init_matrix();
        m=quickPow(m,n);
        Matrix beg=Beg();
        LL ans=0;
        for(int i=0;i<5;i++)
            ans=(ans+beg.m[i][0]*m.m[4][i]%mod)%mod;
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-21 00:00:24

HDOJ 4686 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,

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

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

【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        

S - Arc of Dream 矩阵快速幂

An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0 b i = b i-1*BX+BY What is the value of AoD(N) modulo 1,000,000,007? InputThere are multiple test cases. Process to the End of File. Each test case con

hdu4686 Arc of Dream 矩阵快速幂

An Arc of Dream is a curve defined by following function: wherea0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BYWhat is the value of AoD(N) modulo 1,000,000,007? 矩阵快速幂 1 #include<stdio.h> 2 #include<string.h> 3 typedef long long ll; 4 const int mod

HDOJ 5411 CRB and Puzzle 矩阵快速幂

直接构造矩阵,最上面一行加一排1.快速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 133    Accepted Submission(s): 63 Problem Description CRB is now playing Jigsaw Puzzle. There

hdoj 1575 Tr A 【矩阵快速幂】

Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3020    Accepted Submission(s): 2251 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有

HDOJ 233 Matrix 5015【矩阵快速幂】

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1355    Accepted Submission(s): 806 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may