【ICPC-240】hdu 4686 Arc of Dream

点击打开hdu 4686

思路: 矩阵快速幂

分析:

1 题目给定一个式子求和,那么根据题目给定的,我们可以求出an*bn = (an-1*Ax+Ay)*(bn-1*Bx+By) => an-1*bn-1*Ax*Bx+an-1*Ax*By+bn-1*Ay*Bx+Ay*By

2 那么我们根据上面的等式可以推出矩阵的乘法

  

3 那么我们要求的是AoD(n)相当于求左边矩阵的n次幂,然后利用结果乘上初始值

4 注意特判n为0的时候,结果为0。然后注意初始的值

 

代码:

 


/************************************************
 * By: chenguolin                               * 
 * Date: 2013-08-26                             *
 * Address: http://blog.csdn.net/chenguolinblog *
 ***********************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef __int64 int64;
const int MOD = 1e9+7;
const int N = 7;

int64 n;
int64 a0 , ax , ay;
int64 b0 , bx , by;

struct Matrix{
    int64 mat[N][N];
    Matrix operator*(const Matrix &m)const{
        Matrix tmp;
        for(int i = 0 ; i < N ; i++){
            for(int j = 0 ; j < N ; j++){
                tmp.mat[i][j] = 0;
                for(int k = 0 ; k < N ; k++)
                    tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD;
                tmp.mat[i][j] %= MOD;
            }
        }
        return tmp;
    }
};

void init(Matrix &m){
    memset(m.mat , 0 , sizeof(m.mat));
    m.mat[0][0] = ax*bx%MOD;
    m.mat[0][1] = ax*by%MOD;
    m.mat[0][2] = bx*ay%MOD;
    m.mat[0][3] = 1; m.mat[1][1] = ax%MOD;
    m.mat[1][4] = 1; m.mat[2][2] = bx%MOD;
    m.mat[2][5] = 1; m.mat[3][3] = 1;
    m.mat[4][4] = 1; m.mat[5][5] = 1;
    m.mat[6][0] = 1; m.mat[6][6] = 1;
}

int Pow(Matrix &m){
    if(n == 0)
        return 0;
    Matrix ans;
    memset(ans.mat , 0 , sizeof(ans.mat));
    for(int i = 0 ; i < N ; i++)
        ans.mat[i][i] = 1;
    while(n){
        if(n%2)
            ans = ans*m;
        n /= 2;
        m = m*m;
    }
    int64 sum = 0;
    sum += ans.mat[N-1][0]*(a0*b0%MOD)%MOD;
    sum %= MOD;
    sum += ans.mat[N-1][1]*a0%MOD;
    sum %= MOD;
    sum += ans.mat[N-1][2]*b0%MOD;
    sum %= MOD;
    sum += ans.mat[N-1][3]*(ay*by%MOD)%MOD;
    sum %= MOD;
    sum += ans.mat[N-1][4]*ay%MOD;
    sum %= MOD;
    sum += ans.mat[N-1][5]*by%MOD;
    sum %= MOD;
    return sum%MOD;
}

int main(){
    Matrix m;
    while(scanf("%I64d" , &n) != EOF){
        scanf("%I64d%I64d%I64d" , &a0 , &ax , &ay);
        scanf("%I64d%I64d%I64d" , &b0 , &bx , &by);
        init(m);
        printf("%d\n" , Pow(m));
    }
    return 0;
}

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值