HDU 1575 Tr A (Matrix high-speed power)

Source: Internet
Author: User

Title Address: HDU 1575

Matrix high-speed power naked problem.

Beginner matrix high-speed power. I have learned the power of high speed. Today a look at the matrix of high-speed power, the original principle is the same, this is good to do more. Is the use of two points of the idea of continuous multiplication. It just turns the number into a matrix.

The code is as follows:

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include < stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include < Set> #include <algorithm>using namespace std;const int mod=9973;int n;struct matrix{int ma[20][20];}    Init, Res;matrix Mult (Matrix x, Matrix y) {matrix tmp;    int I, j, K;            for (i=0;i<n;i++) {for (j=0;j<n;j++) {tmp.ma[i][j]=0;            for (k=0;k<n;k++) {tmp.ma[i][j]= (tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod; }}} return tmp;}    Matrix Pow (Matrix x, int k) {matrix tmp;    int I, J;        for (i=0;i<n;i++) {for (j=0;j<n;j++) {tmp.ma[i][j]= (i==j);        }} while (k) {if (k&1) Tmp=mult (tmp,x);        X=mult (X,X);    k>>=1; } return TMP;    int main () {int T, I, J, ans, K;    scanf ("%d", &t); WhiLe (t--) {scanf ("%d%d", &n,&k);            for (i=0;i<n;i++) {for (j=0;j<n;j++) {scanf ("%d", &init.ma[i][j]);        }} res=pow (Init,k);        ans=0;        for (i=0;i<n;i++) {ans= (ans+res.ma[i][i])%mod;    } printf ("%d\n", ans); } return 0;}


HDU 1575 Tr A (Matrix high-speed 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.