FZU 1692 Key problem

Source: Internet
Author: User

Idea: constructor matrix + rapid Power Analysis: 1 The question means that there are n people in a circle, and each person initially has an ai apple. Now I am playing m games, after each game, I personally can add R * A (I + N-1) % n + L * A (I + 1) % n Apples (wrong question ), q: After the m-round game, the number of apples for each person 2. Based on the question, we can list the number of apples for each person after the round. a0 = a0 + R * an-1 + L * a1 a1 = a1 + R * a0 + L * a2 ............................. an-1 = an-1 + R * an-2 + L * a03 based on the second idea, we can construct the following matrix 1 L 0 ...... R a0 a0 'r 1 L ......... * a1 '....................... = ................. R 1 L an-2 an-2 'l ........... R 1 an-1 an-1' 4 then we can use the matrix power to quickly obtain the final answer based on 3, but the n of the question is 100 at the maximum, and m is 10 ^ 9 at the maximum, therefore, the time complexity of each case is O (Logm * n ^ 3). When n is 100 at most, we can see 5 of TLE in the initial matrix, A matrix is a cyclical homogeneous structure, that is, each row of the matrix can be pushed from the previous row. Therefore, we only need to use the time of O (n ^ 2) to find the first row, then we are using recursion to find the remaining n-1 rows, so the total time complexity is O (Logm * n ^ 2) code:

/************************************************  * By: chenguolin                               *   * Date: 2013-08-30                             *  * Address: http://blog.csdn.net/chenguolinblog *  ************************************************/  #include<cstdio>  #include<cstring>  #include<iostream>  #include<algorithm>  using namespace std;    typedef __int64 int64;  const int N = 110;    int arr[N];  int n , m , L , R , MOD;    struct Matrix{      int64 mat[N][N];      Matrix operator*(const Matrix& ma)const{          Matrix tmp;          for(int i = 0 ; i < n ; i++){              tmp.mat[0][i] = 0;               for(int j = 0 ; j < n ; j++)                  tmp.mat[0][i] += mat[0][j]*ma.mat[j][i]%MOD;              tmp.mat[0][i] %= MOD;          }          for(int i = 1 ; i < n ; i++)              for(int j = 0 ; j < n ; j++)                  tmp.mat[i][j] = tmp.mat[i-1][(j-1+n)%n];           return tmp;      }  };    void init(Matrix &ma){      memset(ma.mat , 0 , sizeof(ma.mat));      ma.mat[0][1] = L ; ma.mat[0][n-1] = R;      ma.mat[n-1][0] = L ; ma.mat[n-1][n-2] = R;      ma.mat[0][0] = ma.mat[n-1][n-1] = 1;      for(int i = 1 ; i < n-1 ; i++){          ma.mat[i][i-1] = R;          ma.mat[i][i+1] = L;          ma.mat[i][i] = 1;      }  }    void Pow(Matrix &ma){      Matrix ans;      memset(ans.mat , 0 , sizeof(ans.mat));      for(int i = 0 ; i < n ; i++)          ans.mat[i][i] = 1;      while(m){          if(m&1)              ans = ans*ma;          m >>= 1;          ma = ma*ma;      }      for(int i = 0 ; i < n ; i++){          int64 sum = 0;          for(int j = 0 ; j < n ; j++)              sum += ans.mat[i][j]*arr[j]%MOD;          if(i) printf(" ");          printf("%I64d" , sum%MOD);      }       puts("");  }    int main(){      int cas;      Matrix ma;      scanf("%d" , &cas);      while(cas--){          scanf("%d%d%d" , &n , &m , &L);           scanf("%d%d" , &R , &MOD);           for(int i = 0 ; i < n ; i++)              scanf("%d" , &arr[i]);          init(ma);          Pow(ma);      }      return 0;  }  

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.