hdu 4686(矩阵快速幂)

Arc of Dream

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


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
 思路:我们根据公式来构造矩阵即可


 代码:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define ll long long
const int mod=1000000007;
struct node
{
    ll a[5][5];
};
ll n;
node mul(node a,node b)
{
    node c;
    memset(c.a,0,sizeof(c.a));
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            for(int k=0;k<5;k++)
            {
                c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j])%mod)%mod;
            }
        }
    }
    return c;
}
node pow(node a,ll n1)
{
    node c;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            if(i==j)
            c.a[i][j]=1;
            else c.a[i][j]=0;
        }
    }
    while(n1)
    {
        if(n1&1)c=mul(c,a);
        a=mul(a,a);
        n1>>=1;
    }
    return c;
}
int main()
{  
    ll a0,b0,Ax,Ay,Bx,By;
    while(~scanf("%lld",&n))
    {
        scanf("%lld%lld%lld%lld%lld%lld",&a0,&Ax,&Ay,&b0,&Bx,&By);
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        node a,b;
        memset(a.a,0,sizeof(a));
        a.a[0][0]=(Ax*Bx)%mod;
        a.a[1][0]=(Ax*By)%mod;
        a.a[2][0]=(Ay*Bx)%mod;
        a.a[3][0]=(Ay*By)%mod;
        a.a[1][1]=Ax%mod;
        a.a[3][1]=Ay%mod;
        a.a[2][2]=Bx%mod;
        a.a[3][2]=By%mod;
        a.a[3][3]=1;
        a.a[0][4]=1;
        a.a[4][4]=1;
        ll a1=(a0*Ax+Ay)%mod;
        ll b1=(b0*Bx+By)%mod;
        ll f1=(a1*b1)%mod;
        ll s0=(a0*b0)%mod;
        memset(b.a,0,sizeof(b.a));
        b.a[0][0]=f1;
        b.a[0][1]=a1;
        b.a[0][2]=b1;
        b.a[0][3]=1;
        b.a[0][4]=s0;

        a=pow(a,n-1);
        b=mul(b,a);
        printf("%lld\n",b.a[0][4]);
    }
    return 0;
}

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值