Hihocoder 1151 Domino cover problem two (Matrix fast power)

Source: Internet
Author: User

Ideas see Hihocoder, with the Kuangbin matrix fast Power, a ac,6 of a stroke.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < string> #include <stack> #include <cmath> #include <queue> #include <set> #include <map > #define for (i,s,t) for (int i = (s); I <= (t); ++i) #define Lchild o<<1#define rchild o<<1|1typedef Lo ng Long ll;typedef unsigned long long ull;using namespace Std;const int Inf=0x3f3f3f3f;const ll Inf=0x3f3f3f3f3f3f3f3f;con St int Maxn=1e6+5;int n;ll dp[2][8];struct matrix{long Long mat[8][8];};    Matrix Mul (Matrix A,matrix b) {matrix ret;            for (int i=0;i<8;i++) for (int j=0;j<8;j++) {ret.mat[i][j]=0;                for (int k=0;k<8;k++) {ret.mat[i][j]+=a.mat[i][k]*b.mat[k][j];            ret.mat[i][j]%= (12357); }} return ret;}    Matrix Pow_m (Matrix A,int N) {matrix ret;    memset (ret.mat,0,sizeof (Ret.mat));    for (int i=0;i<8;++i) ret.mat[i][i]=1; Matrix TEmp=a;        while (n) {if (n&1) Ret=mul (ret,temp);        Temp=mul (temp,temp);    n>>=1; } return ret;}    int main () {//freopen ("In.txt", "R", stdin);    Matrix A;    memset (a.mat,0,sizeof A.mat);    for (int i=0;i<8;++i) a.mat[i][7-i]=1;    A.mat[3][7]=1;    A.mat[6][7]=1;    A.mat[7][3]=1;    A.mat[7][6]=1;    ll b[8]={0,0,0,0,0,0,0,1};        while (cin>>n) {Matrix tmp = pow_m (a,n);        ll ans = 0;        for (int i=0;i<8;++i) {ans + = b[i]*tmp.mat[i][7];    } cout<<ans<<endl; } return 0;}

  

Hihocoder 1151 Domino cover problem two (Matrix fast 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.