Fast power of Matrix: NetEase 2017 intern programming problem Magic bracelet __ Programming

Source: Internet
Author: User
Tags mul printf

Describe:
Small easy to have a magic bracelet with n numbers (forming a ring), when the Magic bracelet uses magic every time a strange change occurs: Each number will become itself with the back of a number of the and (the last number after a number is the first), Once a position has a number greater than or equal to 100, the 100 modulo (for example, a position becomes 103, will automatically change to 3). Now give the Magic bracelet composition, please calculate the Magic bracelet after the use of K magic the state of the ring.
Input Description:
The input data consists of two lines:
The first behavior is two integers n (2≤n≤50) and K (1≤k≤2000000000), separated by a space
The second action magic Bracelet initial n number, separated by a space. The range is 0 to 99.
Output Description:
The output Magic Bracelet uses the state after K times, separated by a space, with no spaces at the end of the line.
Input Example:
3 2
1 2 3
Output Example:
8 9 7

Analysis:
Because the evaluation of the problem is very regular, the number into a ring, and the current number is equal to the current number plus a number behind, naturally can be associated with the structure of the matrix, and see the number of operations so large, must either find the law, or fast power, and then I hit the table did not find any rules, so the matrix fast power.

#include <bits/stdc++.h> using namespace std; vector<vector<int> > Mul (vector<vector<int> >a,vector<vector<int> >b) {int M=A.S
    Ize (), N=a[0].size (), k=b[0].size ();
    vector<vector<int> > Res (m,vector<int> (k,0)); for (int i=0;i<m;i++) for (int j=0;j<k;j++) for (int f=0;f<n;f++) res[i][j] = (res[i][j]+a[i
    ][F]*B[F][J])%100;
return res;
    } vector<vector<int> > Matrix_pow (vector<vector<int> > Matrix,int k) {int n=matrix.size ();
    vector<vector<int> > mm (n,vector<int> (n,0));

    for (int i=0;i<n;i++) mm[i][i]=1;
        while (k) {if (k&1) mm = Mul (MATRIX,MM);
        Matrix = Mul (Matrix,matrix);
    k>>=1;
} return mm;
    } int main () {int n,k;
    scanf ("%d%d", &n,&k);
    vector<vector<int> > num;
    Vector<int> T;
    int tmp; for (int i=0;i<n;i++) {scanf ("%D ", &tmp);
    T.push_back (TMP);
    } num.push_back (t);
    vector<vector<int> > Matrix (n,vector<int> (n,0));
        for (int i=0;i<n;i++) {matrix[i][i]=1;
    matrix[(i+1)%n][i]=1;
    } vector<vector<int> > mm = Matrix_pow (matrix,k);
    vector<vector<int> > ans = mul (num,mm);
    printf ("%d", ans[0][0]);
    for (int i=1;i<ans[0].size (); i++) printf ("%d", ans[0][i]);

    Puts ("");
return 0;
 }

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.