• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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;
}


 


 

posted @ 2013-08-26 20:25  Class Xman  阅读(199)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3