(矩阵快速幂)hdu4686 Arc of Dream

(写了三天博客之后,终于学会了插入链接~hhhhhh)题目链接如下:

传送门——hdu4686 Arc of Dream

(也学会了复制题目!23333)题目描述:(居然是watashi大佬出的题,赶紧膜一发....)

Problem Description
An Arc of Dream is a curve defined by following function:

where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-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 10 18, and all the other integers are no more than 2×10 9.
 

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)

题目很裸,就是求一个函数F(n)第n项的值,思路很简单,由题中所给关系求出递推式:aibi = (AX*BX)*(ai-1*bi-1) + (AX*BY)*ai-1 + (AY*BX)*bi-1 + (AY*BY),并且Fi = Fi-1 + ai*bi,因此相当于Fn =  (AX*BX)*(ai-1*bi-1) + (AX*BY)*ai-1 + (AY*BX)*bi-1 + (AY*BY),用矩阵相乘简化这个递推式,设计一个五维向量(ai*bi, ai, bi, 1, Fi),他可以由它的前一项与一个常数矩阵相乘得出,再利用矩阵快速幂,注意数据范围是long long, 顺利ac~
P.S:注意要加一句,n=0时输出结果0,并且,千万不要自作聪明地认为这句判断加在哪都无所谓!!当时不注意,写了这样一个极难发现的bug,WA了6发,WA到一度怀疑人生相信玄学,结果发现是n=0输出的时间出了问题T_T  mdzz
代码如下~(写的很恶心 但改成这样就ac了 再改又wa 于是不敢动了orz)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod = 1000000007;

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

struct mat {
    ll a[5][5];
};

void print(mat ans) {
    for(int i = 0;i < 5;i++) {
        for(int j = 0;j < 5;j++)
            printf("%d ", ans.a[i][j]);
        printf("\n");
    }
    return;
}

mat init0() {//初始化
    mat tt;
    for(int i = 0;i < 5;i++)
        for(int j = 0;j < 5;j++) tt.a[i][j] = 0;
    return tt;
}

mat init1() {//初始化
    mat tt;
    for(int i = 0;i < 5;i++) {
        for(int j = 0;j < 5;j++) {
            if(i == j)
                tt.a[i][j] = 1;
            else
                tt.a[i][j] = 0;
        }
    }
    return tt;
}

mat mul(mat x, mat y) {
    mat tmp;
    for(int i = 0;i < 5;i++) {
        for(int j = 0;j < 5;j++) {
            tmp.a[i][j] = 0;
            for(int k = 0;k < 5;k++) {
                tmp.a[i][j] = (tmp.a[i][j] + x.a[i][k]*y.a[k][j]%mod) % mod;
            }
        }
    }
    return tmp;
}

mat qpow(mat tt, ll b) {
    mat res = init1();
    while(b) {
        if(b & 1) {
            res = mul(res, tt);
        }
        b >>= 1;
        tt = mul(tt, tt);
    }
    return res;
}

int main() {
    while(~scanf("%I64d", &n)) {
        scanf("%I64d %I64d %I64d", &a0, &ax, &ay);
        scanf("%I64d %I64d %I64d", &b0, &bx, &by);
        if(n == 0)
            printf("0\n");
        else {
            mat coef = init0();
            coef.a[0][0] = ( (ax%mod) * (bx%mod) ) % mod;
            coef.a[0][1] = ( (ax%mod) * (by%mod) ) % mod;
            coef.a[0][2] = ( (ay%mod) * (bx%mod) ) % mod;
            coef.a[0][3] = ( (ay%mod) * (by%mod) ) % mod;
            coef.a[1][1] = ax % mod;
            coef.a[1][3] = ay % mod;
            coef.a[2][2] = bx % mod;
            coef.a[2][3] = by % mod;
            coef.a[3][3] = 1;
            coef.a[4][0] = ( (ax%mod) * (bx%mod) ) % mod;
            coef.a[4][1] = ( (ax%mod) * (by%mod) ) % mod;
            coef.a[4][2] = ( (ay%mod) * (bx%mod) ) % mod;
            coef.a[4][3] = ( (ay%mod) * (by%mod) ) % mod;
            coef.a[4][4] = 1;
            coef = qpow(coef, n-1);
            mat mat1 = {((a0%mod)*(b0%mod))%mod, 0, 0, 0, 0,
                        a0%mod,    0, 0, 0, 0,
                        b0%mod,    0, 0, 0, 0,
                        1,     0, 0, 0, 0,
                        ((a0%mod)*(b0%mod))%mod, 0, 0, 0, 0
                        };
            mat ans = mul(coef, mat1);
            printf("%I64d\n", ans.a[4][0] % mod);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值