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。

点击打开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;
}




目录
相关文章
|
6月前
|
Java
hdu-4883- (Best Coder) TIANKENG’s restaurant
hdu-4883- (Best Coder) TIANKENG’s restaurant
30 0
|
6月前
Strange fuction(HDU--2899)
Strange fuction(HDU--2899)
UVA10474 大理石在哪儿 Where is the Marble?
UVA10474 大理石在哪儿 Where is the Marble?
[POJ3678] Katu Puzzle | 2-SAT 入门
Description Katu Puzzle is presented as a directed graph G ( V , E ) with each edge e ( a , b ) labeled by a boolean operator op (oneofAND,OR,XOR) and an integer c ( 0 ≤ c ≤ 1 ) . One Katu is solvable if one can find each vertex Vi a value X i ( 0 ≤ X i ≤ 1 )
|
测试技术
HDU-1847,Good Luck in CET-4 Everybody!(巴什博弈)
HDU-1847,Good Luck in CET-4 Everybody!(巴什博弈)
|
人工智能 Java C++
UVA - 10474 Where is the Marble
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them.
1415 0