HDU Multi-school 9th HDU 4965Fast matrix calculation "matrix operations + Math Trivia"

Source: Internet
Author: User

difficulty,,, indeed,,, was not difficult to

The problem is that there is an optimization of the matrix operation

The title is to give a n*k matrix A to a k*n matrix B (1<=n<=1000 && 1=<k<=6), first multiply them by the C matrix, then calculate c^ (N*n)

Equivalent

ababababababab...= (AB) ^ (n*n)

As

A (BA) ^ (n*n-1) B

K is relatively small because BA is k*k by the matrix.

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm
> #include <iostream> using namespace std;
int a[1111][1111];
int b[1111][1111];
int c[1111][1111];
int tmp[1111][1111];
int sum[1111][1111];
	void times (int k,int n,int m,int aa[][1111],int bb[][1111],int cc[][1111]) {memset (tmp,0,sizeof (TMP)); for (int i=1;i<=k;i++) {for (int. j=1;j<=m;j++) {for (int k=1;k<=n;k++) {tmp[i][j]= (Tmp[i][j]+aa[i][k]*bb[k]
			[J]%6]%6;
}}} for (int i=1;i<=k;i++) for (int j=1;j<=m;j++) cc[i][j]=tmp[i][j]%6;
			} void Quick (int cc[][1111],int mi,int N) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j++) if (i==j) sum[i][j]=1;
	else sum[i][j]=0;
		while (MI) {if (mi&1) times (n,n,n,cc,sum,sum);
		Times (N,N,N,CC,CC,CC);
	mi>>=1;
	}} int main () {#ifndef Online_judge freopen ("G:/in.txt", "R", stdin);
Freopen ("G:/myout.txt", "w", stdout);
	#endif int n,k;
while (~SCANF ("%d%d", &n,&k)) {		if (n==0 && k==0) return 0;
			for (int i=1;i<=n;i++) {for (int j=1;j<=k;j++) {scanf ("%d", &a[i][j]);
			}} for (int i=1;i<=k;i++) {for (int j=1;j<=n;j++) {scanf ("%d", &b[i][j]);
		}} times (K,n,k,b,a,c);
		Quick (c,n*n-1,k);
		Times (N,k,k,a,sum,sum);
		Times (N,k,n,sum,b,sum);
		int ans=0;
		for (int. i=1;i<=n;i++) for (int j=1;j<=n;j++) ANS+=SUM[I][J];
	cout<<ans<<endl;
 }
}


 

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.