hdoj 4686 Arc of Dream(矩阵快速幂)

一个不难构造的矩阵快速幂,自己硬是找了两天才找到自己错在细节上的东西。


自己犯得错:

1.指数是long long,自己却传了个int

2.构造单位矩阵前,只是把对角线赋值为1,却忘了memset其他值都为0


希望以后别因为这些小错,耽误那么多的时间!


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll a0, ax, ay, b0, bx, by;
struct node
{
    ll s[5][5];
    void init()
    {
        memset(s, 0, sizeof(s));
        s[0][0] = s[0][1] = s[4][4] = 1;
        s[1][1] = (ax*bx)%mod; s[1][2] = (ax*by)%mod;
        s[1][3] =(ay*bx)%mod; s[1][4] = (ay*by)%mod;
        s[2][2] = ax%mod; s[2][4] = ay%mod;
        s[3][3] = bx%mod; s[3][4] = by%mod;
    }
};

node mul(node a, node b)
{
    node t;
    memset(t.s, 0, sizeof(t.s));
    for(int i = 0; i < 5; i++)
        for(int j = 0; j < 5; j++)
            for(int k = 0; k < 5; k++)
                t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j]%mod)%mod;
    return t;
}

node mt_pow(node p, ll n)
{
    node q;
    memset(q.s, 0, sizeof(q.s));
    for(int i = 0; i < 5; i++)
        q.s[i][i] = 1;
    while(n)
    {
        if(n&1) q = mul(p, q);
        p = mul(p, p);
        n /= 2;
    }
    return q;
}

int main(void)
{
    ll n;
    while(~scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &a0, &ax, &ay, &b0, &bx, &by))
    {
        if(n == 0) { puts("0"); }
        else
        {
            node base;
            base.init();
            node ans = mt_pow(base, n);
            ll p[5] = {0, a0*b0%mod, a0%mod, b0%mod, 1}, res = 0;
            for(int i = 0; i < 5; i++)
                res = (res + ans.s[0][i]*p[i]%mod)%mod;
            printf("%lld\n", res);
        }
    }
    return 0;
}


Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4174    Accepted Submission(s): 1301


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)
 

Source
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值