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

Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 3126    Accepted Submission(s): 982

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

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  5390 5389 5388 5387 5386

题意:看公式就可以了。。。。ai*bi的数列前n项(0—n-1)求和。

解题思路:10^18次方。。果断利用矩阵快速幂。。关键是找出相乘的矩阵。


1


0


0


1


0


0


Ax


0


0


Ay


0


0


Bx


0


By


0


Ax*By


Bx*Ay


Ax*Bx


Ay*By


0


0


0


0


1

就是利用上述矩阵去乘以


Si-1


ai-1


bi-1


ai-1*bi-1


1

它们相乘的结果就是


Si


ai


bi


ai*b


1

所以直接对第一个矩阵进行N阶快速幂。乘以第二个矩阵,得到最后一个矩阵,输出第一个元素即可。

AC代码:

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
const int MAXN = 5;
const LL Modn = 1000000007;
LL res[MAXN][MAXN];
LL a[MAXN][MAXN];
LL A0,Ax,Ay,B0,Bx,By;

void Matmul(LL X[MAXN][MAXN],LL Y[MAXN][MAXN])
{
    LL t[MAXN][MAXN]={0};
    for(int i=0;i<MAXN;i++){
        for(int k=0;k<MAXN;k++)
            if(X[i][k])
            for(int j=0;j<MAXN;j++)
            t[i][j]=(t[i][j]+X[i][k]*Y[k][j]%Modn)%Modn;
    }
    for(int i=0;i<MAXN;i++)
        for(int j=0;j<MAXN;j++)
        X[i][j]=t[i][j];
}

void Matrix(LL X[MAXN][MAXN],LL n)
{
    for(int i=0;i<MAXN;i++)
        for(int j=0;j<MAXN;j++)
        res[i][j]=(i==j);
    while(n){
        if(n&1) Matmul(res,X);
        Matmul(X,X);
        n>>=1;
    }
}

void init()
{
    memset(a,0,sizeof(a));
    a[0][0]=a[0][3]=1;
    a[1][1]=Ax%Modn;a[1][4]=Ay%Modn;
    a[2][2]=Bx%Modn;a[2][4]=By%Modn;
    a[3][1]=Ax*By%Modn;a[3][2]=Bx*Ay%Modn;
    a[3][3]=Ax*Bx%Modn;a[3][4]=Ay*By%Modn;
    a[4][4]=1;
}

int main()
{
    LL N,S;
    while(scanf("%lld",&N)!=EOF){
        LL ans[5];
        scanf("%lld%lld%lld%lld%lld%lld",&A0,&Ax,&Ay,&B0,&Bx,&By);
        if(N==0){
            printf("0\n");
            continue;
        }
        ans[0]=0;ans[1]=A0;ans[2]=B0;ans[3]=A0*B0%Modn;ans[4]=1;
        init();
        Matrix(a,N);
        S=0;
        for(int i=0;i<MAXN;i++){
            S=(S+ans[i]*res[0][i]%Modn)%Modn;
        }
        printf("%lld\n",S);
    }
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-12-20 20:29:22

HDOJ Arc of Dream 4686【矩阵快速幂】的相关文章

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

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

hdu 4686 (矩阵快速幂)

题意: a 0 = A0          a i = a i-1*AX+AY          b 0 = B0          b i = b i-1*BX+BY 求AoD(N) mod 1000000007 思路: 做这道题时思路没有打开,一直纠结于如何把公式加一起..... 正确是做法是把AoD这个和加到你的构造矩阵里来递推计算 aibi=(Ax*Bx)*ai-1*bi-1 + (Ax*By)*ai-1 + (Ay*Bx)*bi-1 + Ay*By 代码: #include <cstd

Hdoj 1588 Gauss Fibonacci 【矩阵快速幂】

Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584 Accepted Submission(s): 1078 Problem Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a ver

【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        

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 矩阵快速幂,线性同余 难度: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,