• hdu4686 Arc of Dream 2013 Multi-University Training Contest 9矩阵快速幂


    Arc of Dream

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

    Problem Description
    An Arc of Dream is a curve defined by following function:
    where a0 = A0 ai = ai-1*AX+AY b0 = B0 bi = bi-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 1018,
    and all the other integers are no more than 2×109.
     
    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

    解法:

    因为a0=A0,ai=a(i-1)*Ax+Ay,

    b0=B0,bi=b(i-1)*Bx+By;

    所以

    ai*bi=a(i-1)*b(i-1)*Ax*Bx+Ay*Bx*b(i-1)+Ax*By*a(i-1)+Ay*By;

    Si=S(i-1)+ai*bi;

    则构造矩阵为(k=n-1)

    Sn      1  Ax*Bx Ay*BX Ax*By Ay*By     s(n-1)
     an*bn     0 Ax*Bx Ay*BX Ax*By Ay*By      a(n-1)*b(n-1)
     bn     0 0 Bx 0 By      b(n-1)
     an     0 0 0 Ax Ay      a(n-1)
     1     0 0 0 0 1      1

     所以构造矩阵为一个5*5的矩阵如上表的中间部分:

    代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <stdlib.h>
     6 #define mod 1000000007
     7 #define ll long long int
     8 using namespace std;
     9 int N;
    10 struct matrix
    11 {
    12        ll a[5][5];
    13 }origin,res;
    14 matrix multiply(matrix x,matrix y)
    15 {
    16        matrix temp;
    17        memset(temp.a,0,sizeof(temp.a));
    18        for(int i=0;i<N;i++)
    19        {
    20                for(int j=0;j<N;j++)
    21                {
    22                        for(int k=0;k<N;k++)
    23                        {
    24                                temp.a[i][j]+=x.a[i][k]*y.a[k][j]%mod;
    25                                temp.a[i][j]%=mod;
    26                        }
    27                }
    28        }
    29        return temp;
    30 }
    31 void calc(ll n)
    32 {
    33      memset(res.a,0,sizeof(res.a));
    34      for(int i=0;i<N;i++)
    35      res.a[i][i]=1;
    36      while(n)
    37      {
    38              if(n&1)
    39                     res=multiply(res,origin);
    40              n>>=1;
    41              origin=multiply(origin,origin);
    42      }
    43 }
    44 int main()
    45 {
    46     N=5;
    47     ll n;
    48     ll a0,ax,ay,b0,bx,by;
    49     while(cin>>n>>a0>>ax>>ay>>b0>>bx>>by){
    50     memset(origin.a,0,sizeof(origin.a));
    51     origin.a[0][0]=1;origin.a[0][1]=ax*bx%mod;
    52     origin.a[0][2]=ay*bx%mod;origin.a[0][3]=ax*by%mod;
    53     origin.a[0][4]=ay*by%mod;
    54     origin.a[1][1]=ax*bx%mod;origin.a[1][2]=ay*bx%mod;
    55     origin.a[1][3]=ax*by%mod;origin.a[1][4]=ay*by%mod;
    56     origin.a[2][2]=bx%mod;origin.a[2][4]=by%mod;
    57     origin.a[3][3]=ax%mod;origin.a[3][4]=ay%mod;
    58     origin.a[4][4]=1;
    59     if(n){
    60     calc(n-1);
    61     ll  sum=0;
    62     sum+=res.a[0][0]*a0%mod*b0%mod;
    63     sum+=res.a[0][1]*a0%mod*b0%mod;
    64     sum%=mod;
    65     sum+=res.a[0][2]*b0%mod;
    66     sum%=mod;
    67     sum+=res.a[0][3]*a0%mod;
    68     sum%=mod;
    69     sum+=res.a[0][4];
    70     sum%=mod;
    71     cout<<sum<<endl;
    72     }
    73     else cout<<0<<endl;
    74     }
    75 }
    View Code
  • 相关阅读:
    P7276-送给好友的礼物【dp】
    P4831-Scarlet loves WenHuaKe【组合数学】
    CF461D-Appleman and Complicated Task【并查集】
    P6499-[COCI2016-2017#2]Burza【状压dp】
    CF757F-Team Rocket Rises Again【最短路,DAG支配树】
    Loj#6053-简单的函数【Min25筛】
    P5325-[模板]Min_25筛
    2019.10.6 机房训练赛
    [CSP校内集训]v(记忆化搜索+map优化状压)
    [CSP校内集训]ac(树上启发式合并)
  • 原文地址:https://www.cnblogs.com/ERKE/p/3273134.html
Copyright © 2020-2023  润新知