POJ 3233 Matrix Power Series Matrix multiplication

Source: Internet
Author: User

Even if it's a question... K is big, so we use sub-governance when adding up.

 


The rapid power of a matrix is also the idea of using sub-Governance

 


So it's a sub-governance + sub-governance question...

 


The main purpose of this question is to write a template to prevent future debugging of similar questions...

 

 

 


[Cpp]
# Include <functional>
# Include <algorithm>
# Include <iostream>
# Include <fstream>
# Include <sstream>
# Include <iomanip>
# Include <numeric>
# Include <cstring>
# Include <cassert>
# Include <cstdio>
# Include <string>
# Include <vector>
# Include <bitset>
# Include <queue>
# Include <stack>
# Include <cmath>
# Include <ctime>
# Include <list>
# Include <set>
# Include <map>
Using namespace std;
// Typedef long LL;
// Typedef _ int64 LL;
// Typedef long double DB;
// Typedef unisigned _ int64 LL;
// Typedef unsigned long ULL;
# Define EPS 1e-8
# Define maxn500050
# Define INF 0x3f3f3f
# Define PI acos (-1.0)
// # Define MOD 99991.
// # Define MOD 99990001.
// # Define MOD 1000000007.
# Define max (a, B) (a)> (B )? (A) (B ))
# Define min (a, B) (a) <(B )? (A) (B ))
# Define max3 (a, B, c) (max (a, B), c ))
# Define min3 (a, B, c) (min (a, B), c ))
# Define mabs (a) (a <0 )? (-A):)
# Define L (t) (t <1) // Left son t * 2
# Define R (t) (t <1 | 1) // Right son t * 2 + 1
# Define Mid (a, B) (a + B)> 1) // Get Mid
# Define lowbit (a) (a &-a) // Get Lowbit
Int gcd (int a, int B) {return B? Gcd (B, a % B): ;}
Int lcm (int a, int B) {return a * B/gcd (a, B );}
Struct matrix
{
Int ma [35] [35];
} First, result;
Int n, k, m;
/* Matrix multiplication */
Matrix operator * (matrix a, matrix B)
{
Matrix temp;
Memset (temp. ma, 0, sizeof (temp. ma ));
For (int I = 0; I <n; I ++)
For (int j = 0; j <n; j ++)
For (int k = 0; k <n; k ++)
Temp. ma [I] [j] = (temp. ma [I] [j] + (. ma [I] [k] * B. ma [k] [j]) % m;
Return temp;
}
/* Matrix addition */
Matrix operator + (matrix a, matrix B)
{
For (int I = 0; I <n; I ++)
For (int j = 0; j <n; j ++)
A. ma [I] [j] = (a. ma [I] [j] + B. ma [I] [j]) % m;
Return;
}
/* Rapid matrix power */
Matrix m_pow (matrix a, int n)
{
If (n = 1)
Return;
Matrix temp = m_pow (a, n/2 );
If (n & 1)
Return temp *;
Else
Return temp * temp;
 
}
/* Sub-governance solution! */
Matrix solve (matrix a, int now)
{
If (now = 1)
Return;
Int mid = now> 1;
Matrix temp = solve (a, mid );
If (now & 1)
Return temp + (m_pow (a, mid) * (temp + m_pow (a, mid + 1 )));
Else
Return temp + (m_pow (a, mid) * temp );
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
// Freopen ("out.txt," w ", stdout );
Cin> n> k> m;
For (int I = 0; I <n; I ++)
For (int j = 0; j <n; j ++)
Scanf ("% d", & first. ma [I] [j]);
Result = solve (first, k );
For (int I = 0; I <n; I ++)
{
For (int j = 0; j <n; j ++)
Cout <result. ma [I] [j] <"";
Cout <endl;
}
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.