NOD 1113 Matrix Fast Power

Source: Internet
Author: User

Base time limit: 3Second space limit: 131072 KB score: gives a matrix of n * n, where the elements are positive integers. The M-Order of the matrix is obtained. Because the M-time calculation results are too large, only the results of each mod (10^9 + 7) need to be output. Input
Line 1th: 2 numbers n and m, separated by a space in the middle. n is the size of the matrix, M is the M-th square. (2 <= n <=, 1 <= M <= 10^9) 2-n + 1 rows: N number per row, corresponding to 1 rows in the n * n Matrix. (0 <= N[i] <= 10^9)
Output
Total n rows, number of n per row, corresponding to the results of M-time mod (10^9 + 7).
Input example
2 31 11 1
Output example
4 44 4

Learn the matrix multiplication
#include <cstdio>#include<cstdlib>#include<iostream>using namespaceStd;typedefLong LongLL;Const intmod=1e9+7; typedefstruct{    intm[111][111];} Matrix;intN,k;matrixoperator*(Matrix A,matrix b) {matrix res;    LL x;  for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++) {x=0;  for(intk=0; k<n;k++) {x= (x+ (LL) a.m[i][k]*b.m[k][j])%MOD; } Res.m[i][j]=x%MOD; }    }    returnRes;} Matrix Fast_cover (Matrix A,intk)    {matrix S;  for(intI=0;i<111; i++) s.m[i][i]=1;//Unit Matrix     while(k) {if(k&1) s=s*A; A=a*A; K>>=1; }    returns;}intMain () { while(SCANF ("%d%d", &n,&k)! =EOF)        {Matrix A;  for(intI=0; i<n;i++)        {             for(intj=0; j<n;j++) {scanf ("%d",&A.m[i][j]); }} a=Fast_cover (a,k);  for(intI=0; i<n;i++)        {             for(intj=0; j<n-1; j + +) printf ("%d", A.m[i][j]); printf ("%d\n", a.m[i][n-1]); }    }    return 0;}

NOD 1113 Matrix Fast Power

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.