Poj3233 (second division and second division of matrix)

Source: Internet
Author: User

The question is very simple:

Description

GivenN×NMatrixAAnd a positive integerK, Find the sumS=A+A2 +A3 +... +AK.

Output

S mod m

Range: N(N≤ 30 ),K(K≤ 109) andM(M<104 ).

Obviously, violence cannot solve the problem, which is very interesting and difficult to think about.

What I take is recursive binary solution:


If K is an even number, it can be converted to (a + A ^ 2 +... + A ^ (K/2) + A ^ (K/2) * (a + A ^ 2 +... + A ^ (K/2 ));

For example, K = 6 can be converted to (a + A ^ 2 + A ^ 3) + (a ^ 3) * (a + A ^ 2 + A ^ 3 ), the problem is converted to a + A ^ 2 + A ^ 3, and then a ^ 3 is quickly multiplied.

When K is an odd number: can be converted into a K-1 solution + (a ^ K)

This topic is very interesting, that is, a second division is used for sum, and a second division is used for power.

I wrote a little slowly. I reloaded a lot of symbols and had obsessive-compulsive disorder. In order to be nice ....

13577997 Dengyaolong 3233 Accepted 896 K 1672 Ms C ++ 2867b 17:10:35

/*************************************** * *******************> OS: linux 3.small-24-generic (MINT-17)> author: yaolong> mail: [email protected]> time: october 16: 05 minutes 45 seconds ******************************** * *************************/# include <iostream> # include <cstdio> # include <string >#include <cstring> using namespace STD; struct matrix {int A [31] [31]; int size; int MOD; matrix (int s, int m): size (s), MOD (m) {for (INT I = 1; I <= s; I ++) {A [I] [I] = 1; // matrix of units} Matrix Operator * (const Matrix & RHs) const // simple multiplication {matrix res (size, MoD); For (INT I = 1; I <= size; I ++) {for (Int J = 1; j <= size; j ++) {res. A [I] [J] = 0; For (int K = 1; k <= size; k ++) {res. A [I] [J] = (res. A [I] [J] + (A [I] [k] * RHS. A [k] [J]) % mod ;}}return res ;}matrix operator + (const Matrix & RHs) const // simpler addition {matrix res (size, MoD); For (INT I = 1; I <= size; I ++) {for (Int J = 1; j <= size; j ++) {res. A [I] [J] = (a [I] [J] + RHS. A [I] [J]) % mod ;}return res;} matrix operator ^ (INT p) // fast power {matrix R (size, MoD ); matrix base (* This); While (p) {If (P & 1) {r = r * base;} base = base * base; P >>= 1 ;} return R;} void print () // output {for (INT I = 1; I <= size; I ++) {for (Int J = 1; j <= size; j ++) {If (j> 1) {cout <"" ;}cout <A [I] [J];} cout <Endl ;}} void input () // input {for (INT I = 1; I <= size; I ++) {for (Int J = 1; j <= size; j ++) {CIN> A [I] [J]; A [I] [J] = A [I] [J] % mod ;}}}; matrix powerplus (matrix m, int K) {If (k = 1) // returns {return m;} If (K & 1) {Matrix T = powerplus (M, k> 1); Return (M ^ K) only once) + T + (M ^ (k> 1) * t;} else {Matrix T = powerplus (M, k> 1 ); return T + (M ^ (k> 1) * t ;}} int main () {int size, K, MOD; cin> size> K> MOD; matrix MTR (size, MoD); MTR. input (); (powerplus (MTR, k )). print (); Return 0 ;}




Poj3233 (second division and second division of matrix)

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.