hdu 4686

Arc of Dream

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


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

矩阵快速幂
ai = ai-1 * Ax + Ay;
bi = bi-1 * Bx + By;
ai * bi = ai-1 * bi-1 * (Ax * Bx) + ai-1 * (Ax * By) + bi-1 * (Bx * Ay) + AyBy;
Si = Si - 1 + ai * bi = Si - 1  + ai-1 * bi-1 * (Ax * Bx) + ai-1 * (Ax * By) + bi-1 * (Bx * Ay) + AyBy;

根据SI , 一定需要  Si - 1,  ai - 1,  bi - 1,  ai - 1 * bi - 1,  常数。
矩阵大小为5,
构造 初始矩阵A 为 记  S0
[1, A0,  B0,  A0 * B0,  A0 * B0]  即 [1, a0, b0, a0 * b0, S0]
[0 …… 0]
转换矩阵 B.   使得  A * B = S1 = [1,  a1,  b1, a1 * b1 , S1]
根据上面4个公式得。
第一列为  1, 0, 0, 0, 0;
第二列为  Ay,  Ax,   0,   0,   0;
第三列为  By,  0,   Bx,  0,   0;
第四列为  AyBy, Ax * By,   Bx * Ay,  Ax * Bx, 0;
第五列为  AyBy, Ax * By,   Bx * Ay,  Ax * Bx, 1;
N次递推得

现在要求 Sn - 1 ,   所以  init * Pow ^ n - 1.   然后取第一行, 第5个元素。
#include<stdio.h>
#include<string.h>
#define mod 1000000007
 struct matrix
{
    __int64 mat[6][6];
};
matrix multi(matrix a,matrix b,__int64 n)
{
    __int64 i,j,k;
    matrix ans;
    memset(ans.mat,0,sizeof(ans.mat));
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
          for(k=0;k<n;k++)
           {
            ans.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod;
            ans.mat[i][j]%=mod;
           }
    return ans;
}
matrix pow(matrix a,__int64 n,__int64 k)
{
    matrix ans=a;
    k--;
    while(k)
    {
        if(k&1)
            ans=multi(ans,a,n);
        a=multi(a,a,n);
        k/=2;
    }
    return ans;
}
int main()
{
    __int64 a0,b0,ax,ay,bx,by,n;
    while(scanf("%I64d",&n)!=EOF)
    {
        matrix a,b,ans;
        memset(a.mat,0,sizeof(a.mat));
        memset(b.mat,0,sizeof(b.mat));
        scanf("%I64d%I64d%I64d",&a0,&ax,&ay);
        scanf("%I64d%I64d%I64d",&b0,&bx,&by);
        a0%=mod; ax%=mod; ay%=mod; b0%=mod; bx%=mod; by%=mod;
        if(n==0)
        {//n=0的时候特殊处理一下。
            printf("0\n");
            continue;
        }
        if(n==1)
        {
            printf("%I64d\n",a0*b0%mod);
            continue;
        }
        a.mat[0][0]=1; a.mat[1][0]=ax*bx%mod; a.mat[1][1]=ax*bx%mod;
        a.mat[2][0]=ax*by%mod; a.mat[2][1]=ax*by%mod; a.mat[2][2]=ax;
        a.mat[3][0]=ay*bx%mod; a.mat[3][1]=ay*bx%mod; a.mat[3][3]=bx;
        a.mat[4][0]=ay*by%mod; a.mat[4][1]=ay*by%mod; a.mat[4][2]=ay;
        a.mat[4][3]=by;  a.mat[4][4]=1;

        b.mat[0][0]=a0*b0%mod; b.mat[0][1]=a0*b0%mod; b.mat[0][2]=a0;
        b.mat[0][3]=b0; b.mat[0][4]=1;

        ans=multi(b,pow(a,5,n-1),5);
        printf("%I64d\n",ans.mat[0][0]%mod);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值