【HDU4686】Arc of Dream

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46252

傻逼矩阵加速。。QwQ

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define mod 1000000007
using namespace std;
LL n;
int ax, ay, a0, bx, by, b0, qwq;
struct Matrix{
    int s[5][5];
    void clear(){
        memset(s, 0, sizeof(s));
    }
}ans, A;

int mul(int a, int b){
//    cout<<a<<" "<<b<<" "<<a*b%mod<<endl;
    return (int)((LL)((LL)a*(LL)b) % mod);
}

Matrix operator * (Matrix a, Matrix b){
    Matrix c;
    c.clear();
    for (int i = 0; i <= 4; i++){
        for (int j = 0; j <= 4; j++){
            for (int k = 0; k <= 4; k++){
                c.s[i][j] = (c.s[i][j] + mul(a.s[i][k], b.s[k][j])) % mod;
            }
        }
    }
    return c;
} 

void Reset(){
    n--;
    ans.clear(); A.clear();
    ans.s[0][0] = 1; ans.s[0][1] = a0; ans.s[0][2] = b0; ans.s[0][3] = ans.s[0][4] = mul(a0, b0);
    A.s[0][0] = 1;
    A.s[0][1] = ay; A.s[1][1] = ax;
    A.s[0][2] = by; A.s[2][2] = bx;
    A.s[0][3] = A.s[0][4] = mul(ay, by);
    A.s[1][3] = A.s[1][4] = mul(ax, by);
    A.s[2][3] = A.s[2][4] = mul(bx, ay);
    A.s[3][3] = A.s[3][4] = mul(ax, bx);
    A.s[4][4] = 1;
    while (n){
        if (n&1) ans = ans * A;
        A = A * A;
        n >>= 1;
    }
}

int main(){
    while(~scanf("%lld", &n)){
        scanf("%d%d%d\n%d%d%d", &a0, &ax, &ay, &b0, &bx, &by);
        if (n == 0) { printf("0\n"); continue; }
        Reset();
        printf("%d\n", ans.s[0][4]);
    }
    return 0;
}
View Code

 

posted @ 2016-04-19 08:29  Lukaluka  阅读(302)  评论(0编辑  收藏  举报