51nod 1033 Domino Cover V2

Source: Internet
Author: User
Tags mul pow

Matrix multiplication problem

It's just a matter of pressing the state of a column and then preprocessing the number of transitions for a given two states.

Then construct a matrix A[i][j] representing the number of States of I to the state of J

And then the direct matrix multiplication is possible.

#include <algorithm> #include <iostream> #include <cmath> #include <vector> using namespace std;
const int mod=1000000007;
typedef vector<long Long> VEC;
typedef vector<vec> MAT;
    A*b Mat Mul (mat& A, mat& B) {Mat C (A.size (), VEC (B[0].size ())); for (int i=0;i< (int) a.size (), ++i) {for (int j=0;j< (int) b[0].size (), ++j) {for (int k=0;k< (int) b.size (); ++k
			) {c[i][j]= (c[i][j]+a[i][k]*b[k][j])%mod;
}}} return C;
    }//A^n Mat POW (Mat a,int N) {Mat B (A.size (), VEC (A.size ()));
	for (int i=0;i< (int) a.size (); ++i) {b[i][i]=1;
		} while (n) {if (n&1) {B=mul (b,a); } n>>=1;
    A=mul (A,a);
} return B;
} int n,m;
Long Long dp[1<<5][1<<5];
	void Dfs (int c,int pre,int now) {if (c>n) {return;
        } if (c==n) {dp[pre][now]++;
    Return
    } dfs (C+1,PRE&LT;&LT;1,NOW&LT;&LT;1|1);
    DFS (C+1,PRE&LT;&LT;1|1,NOW&LT;&LT;1); DFS (c+2,pRE&LT;&LT;2,NOW&LT;&LT;2);
    } int main () {cin>>m>>n;
    Mat A (1<<n,vec (1<<n));
    DFS (0,0,0);
        for (int i=0;i< (1<<n), i++) {for (int j=0;j< (1<<n); j + +) {A[i][j]=dp[i][j];
    }} a=pow (a,m+1);
    printf ("%lld\n", a[0][(1<<n)-1]);
return 0; }/* In:2 3 Out:3 */


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.