Luogu P3390 [TEMPLATE] rapid matrix power, luogup3390 Matrix

Source: Internet
Author: User

Luogu P3390 [TEMPLATE] rapid matrix power, luogup3390 Matrix
Background

Rapid matrix power

Description

Given n * n matrix A, evaluate A ^ k

Input/Output Format

Input Format:

 

The first line, n, k

The number of n rows from 2nd to n + 1. The number of j rows in I + 1 indicates the elements in column j of row I in the matrix.

 

Output Format:

 

Output A ^ k

N rows in total, n numbers in each row, and j numbers in row I represent the elements in column j of row I. Each element is 10 ^ 9 + 7

 

Input and Output sample input sample #1:
2 11 11 1
Output sample #1:
1 11 1
Description

N <= 100, k <= 10 ^ 12, | matrix element | <= 1000 algorithm: rapid matrix power

 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 typedef long long ll; 5 ll x[999][999]; 6 ll ans[999][999]; 7 ll dx[999][999]; 8 const int p=1e9+7; 9 inline void anscf(int n)10 {11     for(int i=1;i<=n;i++)12      for(int j=1;j<=n;j++)13       dx[i][j]=ans[i][j],ans[i][j]=0;14 15     for(int i=1;i<=n;i++)16      for(int j=1;j<=n;j++)17       for(int k=1;k<=n;k++)18        ans[i][j]=(ans[i][j]+(x[i][k]*dx[k][j])%p)%p;19 }20 inline void xcf(int n)21 {22     for(int i=1;i<=n;i++)23      for(int j=1;j<=n;j++)24       dx[i][j]=x[i][j],x[i][j]=0;25 26     for(int i=1;i<=n;i++)27      for(int j=1;j<=n;j++)28       for(int k=1;k<=n;k++)29        x[i][j]=(x[i][j]+(dx[i][k]*dx[k][j])%p)%p;30 }31 inline void fastpow(ll n,ll w)32 {33     while(w)34     {35         if(w%2==1) anscf(n);36         w/=2;37         xcf(n);38     }39 }40 int main()41 {42     ll n,k;43     scanf("%lld%lld",&n,&k);44     for(int i=1;i<=n;i++)45      for(int j=1;j<=n;j++)46       scanf("%d",&x[i][j]),ans[i][j]=x[i][j];47     fastpow(n,k-1);48     for(int i=1;i<=n;i++)49     {50       for(int j=1;j<=n;j++)51        printf("%lld ",ans[i][j]);52       puts("");53     }54 }

 

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.