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

Arc of Dream

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


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<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1000000007;
struct node
{
    ll mx[5][5];
}a,b;
void init()
{
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        a.mx[i][j]=b.mx[i][j]=0;
}
node cheng(node x,node y)
{
    node tmp;
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            tmp.mx[i][j]=0;
            for(int k=0;k<5;k++)
            {
                tmp.mx[i][j]+=(x.mx[i][k]*y.mx[k][j]%mod);
                tmp.mx[i][j]%=mod;
            }
        }
    }
    return tmp;
}
void mx_pow(ll n)
{
    while(n)
    {
        if(n&1)b=cheng(a,b);
        a=cheng(a,a);
        n=n/2;
    }
}
int main()
{
   ll n;
   while(~scanf("%lld",&n))
   {
       init();
       ll A0,AX,AY,B0,BX,BY;
       scanf("%lld%lld%lld%lld%lld%lld",&A0,&AX,&AY,&B0,&BX,&BY);
       if(!n)
       {
           printf("0\n");continue;
       }
       A0%=mod;AX%=mod;AY%=mod;
       B0%=mod;BX%=mod;BY%=mod;
       a.mx[0][0]=AX*BX;a.mx[0][1]=AX*BY;
       a.mx[0][2]=AY*BX;a.mx[0][3]=AY*BY;a.mx[0][4]=0;
       a.mx[1][0]=0;a.mx[1][1]=AX;a.mx[1][2]=0;
       a.mx[1][3]=AY;a.mx[1][4]=0;
       a.mx[2][0]=0;a.mx[2][1]=0;a.mx[2][2]=BX;
       a.mx[2][3]=BY;a.mx[2][4]=0;
       a.mx[3][0]=0;a.mx[3][1]=0;a.mx[3][2]=0;
       a.mx[3][3]=1;a.mx[3][4]=0;
       a.mx[4][0]=AX*BX;a.mx[4][1]=AX*BY;
       a.mx[4][2]=AY*BX;a.mx[4][3]=AY*BY;a.mx[4][4]=1;
       b.mx[0][0]=A0*B0;b.mx[1][0]=A0;b.mx[2][0]=B0;
       b.mx[3][0]=1;b.mx[4][0]=A0*B0;
       for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
       {
           a.mx[i][j]%=mod;
           b.mx[i][j]%=mod;
       }
       mx_pow(n-1);
       printf("%lld\n",b.mx[4][0]);
   }
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值