FZU 1692 Key problem (Construction matrix + rapid matrix power)

Source: Internet
Author: User

FZU 1692 Key problem (Construction matrix + rapid matrix power)

 

[Question]: 1. The question refers to a circle of n people. Each person initially has an ai apple and is now 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: The number of apples for each person after the m-round game

[Idea ]:

 

According to the question, we can list the number of apples for each person after a round.

A0 = a0 + R * an-1 + L * a1

A1 = a1 + R * a0 + L * a2

.............................

An-1 = an-1 + R * an-2 + L * a0

3. Based on the second idea, we can construct the following matrix:

1 L 0... R a0'

R 1 L... * a1'

......

... R 1 L an-2 an-2'

L... R 1 an-1 an-1'

4. Then, based on 3, we can use the Matrix to quickly obtain the final answer, but the n of the question is 100 at the maximum, and m is 10 ^ 9 at the maximum, the time complexity of each case is O (Logm * n ^ 3). When n is at most 100, it will

5. We found that in the initial matrix, the matrix is a cyclical homogeneous structure, that is, each row of the matrix can be pushed from the previous row, so we only need to use O (n ^ 2) the first line of time is obtained, and then the remaining n-1 rows are obtained by recursion. The total time complexity is O (Logm * n ^ 2)

Code:

 

/*Problem id. FZU 1692RunID: 631373UserID: ACM_herongweiSubmit time: 2015-09-04 11:58:16Language: C++Length: 1806 Bytes.Result: Accepted*/#include 
 
  #include 
  
   #include 
   
    #include using namespace std;typedef long long LL;const int N=105;int arr[N];int n,m,L,R,MOD;struct mut{    LL mat[N][N];    mut()    {        memset(mat,0,sizeof(mat));    }    void init()    {        for(int i=0; i
    
     >=1;    }    return unit;}void solve(){    int ans=0;    mut unit,a;    scanf(%d %d %d %d %d,&n,&m,&R,&L,&MOD);    for(int i=0; i
     
      

 

 

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.