[Practice of matrix multiplication] HDU 2604 -- queuing

Source: Internet
Author: User

Question: Click to open the link

First, this is a recursive problem. The end of mm can only be pushed by the end of FM or mm. Those ending with MF can only be pushed by mm, those ending with FM can only be pushed by MF or ff, and those ending with FF can only be pushed by MF.

F (n) = f (n-1) + f (n-3) + f (n-4 ). But if it is normal to use a large number, it will be TLE. Later I checked discuss and learned that matrix multiplication should be used.

1-4 is known.

Here, this a matrix is to be constructed. It is generally achieved through a 0-1 array, and the matrix construction method is not unique.

After the array is created, the target value is-4, then the binary power is optimized, and then the first four are manually added, which is a little dizzy. After all, the mathematics is slightly inferior ..

Two clear links are provided:

Matrix multiplication method DP Method

# Include <iostream> # include <cmath> # include <cstring> using namespace STD; Class mat {public: _ int64 mat [10] [10];}; int N = 4; // dimension, that is, the number of rows in matrix A int MOD; // The number mat E after the remainder is required for many problems; int f [5] = {0, 2, 4,6, 9}; void inite () {for (INT I = 0; I <10; I ++) E. mat [I] [I] = 1;} mat operator * (MAT a, mat B) {int I, j, k; mat c; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {C. mat [I] [J] = 0; For (k = 0; k <n; k ++) C. mat [I] [J] + = (. mat [I] [k] * B. mat [k] [J]) % MOD; C. mat [I] [J] % = MOD;} return C;} mat operator + (MAT a, mat B) {mat C; int I, J; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) C. mat [I] [J] = (. mat [I] [J] + B. mat [I] [J]) % MOD;} return C;} mat operator ^ (MAT a, int X) {mat P = E, q =; while (x> = 1) {If (X % 2 = 1) P = p * q; X/= 2; q = Q * q;} return P ;} mat solve (MAT a, int p) {If (P = 1) return a; else if (P & 1) Return (a ^ p) + solve (, p-1); else return (a ^ (P> 1) + E) * solve (A, P> 1);} int main () {int length, MOD; while (CIN> length> mod) {inite (); MAT a; memset (. mat, 0, sizeof (. mat);. mat [0] [0] =. mat [0] [2] =. mat [0] [3] =. mat [1] [0] =. mat [2] [1] =. mat [3] [2] = 1; if (length <= 4) {cout <F [length] % mod <Endl; continue;} mat X; X = a ^ (length-4); int ans = 0; ans = (X. mat [0] [0] * 9 + X. mat [0] [1] * 6 + X. mat [0] [2] * 4 + X. mat [0] [3] * 2); cout <ans % mod <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.