zoukankan      html  css  js  c++  java
  • HDU4686—Arc of Dream

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686

    题目意思:给出一个n,算这个式子,给出A0,B0,AX,AY,然后存在以下的递推关系。

    a0 = A0
    ai = ai-1*AX+AY
    b0 = B0
    bi = bi-1*BX+BY

    构造矩阵的思路先算ai,bi,然后算Aod(i),然后再求和,说明这个一定是一个4*4的矩阵,我们可以构造以下系数矩阵(构造矩阵技巧可能会准备专门总结一下):

    [    1 ,          0 ,    0 , 0 ,  0]
    [Ax*Bx ,Ax*Bx , 0 , 0 ,  0]
    [Ay*Bx ,Ay*Bx ,Bx ,0 ,  0]
    [Ax*By ,Ax*By ,0 ,Ax ,  0]
    [Ay*By ,Ay*By ,By ,Ay ,1]

    有这个系数矩阵以后就很简单,就是普通矩阵快速幂,其实这个矩阵挺容易构造的。

    代码:

     1 //Author: xiaowuga
     2 #include <bits/stdc++.h>
     3 #define maxx INT_MAX
     4 #define minn INT_MIN
     5 #define inf 0x3f3f3f3f
     6 #define n 5 
     7 #define mod 1000000007
     8 using namespace std;
     9 typedef long long ll;
    10 ll a0,b0,ax,bx,ay,by;
    11 struct Matrix{
    12     ll mat[15][15];
    13     Matrix operator * (const Matrix & m) const{
    14         Matrix tmp;
    15         for(int i=0;i<n;i++)
    16             for(int j=0;j<n;j++){
    17                 tmp.mat[i][j]=0;
    18                 for(int k=0;k<n;k++){
    19                     tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%mod;
    20                     tmp.mat[i][j]%=mod;
    21                 }
    22             }
    23         return tmp;
    24     }
    25 };
    26 Matrix q_power(Matrix a,ll k){
    27     Matrix ans;
    28     memset(ans.mat,0,sizeof(ans.mat));
    29     for(int i=0;i<n;i++) ans.mat[i][i]=1;
    30     while(k){
    31         if(k&1) ans=ans*a;
    32         k/=2;
    33         a=a*a;
    34     }
    35     return ans;
    36     
    37 }
    38 int main() {
    39     ios::sync_with_stdio(false);cin.tie(0);
    40     ll T;
    41     while(cin>>T>>a0>>ax>>ay>>b0>>bx>>by){
    42         if(T==0){cout<<0<<endl;continue;}
    43         Matrix m;
    44         memset(m.mat,0,sizeof(m.mat));
    45         m.mat[0][0]=m.mat[0][4]=ax*bx%mod;
    46         m.mat[1][0]=ax*by%mod;m.mat[1][1]=ax%mod;m.mat[1][4]=ax*by%mod;
    47         m.mat[2][0]=bx*ay%mod;m.mat[2][2]=bx%mod; m.mat[2][4]=bx*ay%mod;
    48         m.mat[3][0]=by*ay%mod;m.mat[3][1]=ay%mod;m.mat[3][2]=by%mod;m.mat[3][3]=1;m.mat[3][4]=by*ay%mod;
    49         m.mat[4][4]=1;
    50         Matrix p=q_power(m,T-1);
    51         Matrix f;
    52         memset(f.mat,0,sizeof(f.mat));
    53         f.mat[0][0]=f.mat[0][4]=a0*b0%mod;f.mat[0][1]=a0%mod;f.mat[0][2]=b0%mod;f.mat[0][3]=1;
    54         f=f*p;
    55         cout<<f.mat[0][4]<<endl;
    56     }  
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    用wamp配置的环境,想用CMD连接mysql怎么连
    Mysql删除表
    MySQL创建表
    Leetcode 130. Surrounded Regions
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 110. Balanced Binary Tree
    Leetcode 98. Validate Binary Search Tree
    Leetcode 99. Recover Binary Search Tree
    Leetcode 108. Convert Sorted Array to Binary Search Tree
    Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/xiaowuga/p/7353231.html
Copyright © 2011-2022 走看看