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<<1,NOW<<1|1);
DFS (C+1,PRE<<1|1,NOW<<1); DFS (c+2,pRE<<2,NOW<<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 */