七十分超时代码,具体思路可以仿照acwing蒙德里安的梦想
就是枚举第i层放 a ,第i-1层放b的合法方案其中b和a都是同一块;
#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
using namespace std;
typedef long long LL;
const int N = 5e5 + 4, mod = 1e9 + 7, M = 1<<7;LL n;
int m;
int dp[N][M];void dfs(int a, int b, int p)
{if(p == m) v[a][b]++, c[a][b]++;else{if(p > 0){if(!((a>>p&1)||(b>>p&1)||(b>>p-1&1)))dfs(a|1<<p, b|1<<p|1<<p-1, p+1);if(!((a>>p&1)||(b>>p&1)||(a>>p-1&1)))dfs(a|1<<p|1<<p-1, b|1<<p, p+1);}if(p+1 != m){if(!((a>>p&1)||(b>>p&1)||(b>>p+1&1)))dfs(a|1<<p, b|1<<p|1<<p+1, p+1);if(!((a>>p&1)||(b>>p&1)||(a>>p+1&1)))dfs(a|1<<p|1<<p+1, b|1<<p, p+1);}dfs(a, b, p+1);}
}int main()
{scanf("%lld%d", &n, &m);dfs(0, 0, 0);for(int i = 0;i < 1<<m;i ++)dp[1][i] = v[i][(1<<m)-1];for(int i = 2;i < n;i++)for(int j = 0;j < 1<<m;j ++)for(int k = 0;k < 1<<m;k ++)dp[i][j] = (dp[i][j] + (LL)dp[i-1][((1<<m)-1)^k]*v[j][k])%mod;cout<<dp[n-1][(1<<m)-1];return 0;
}
100 分矩阵快速幂
#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
using namespace std;
typedef long long LL;
const int N = 5e5 + 4, mod = 1e9 + 7, M = 1<<7;LL n;
int m, c[M][M], tem[M][M], res[M][M];
int dp[3][M];void multi(int a[][M], int b[][M], int n)
{memset(tem, 0, sizeof tem); for(int i = 0;i < n;i ++)for(int j = 0;j < n;j ++)for(int k = 0;k < n;k ++)tem[i][j] = (tem[i][j] + (LL)a[i][k]*b[k][j])%mod;for(int i = 0;i < n;i ++)for(int j = 0;j < n;j ++)a[i][j] = tem[i][j];
}void pow(int a[][M], LL n)
{memset(res, 0, sizeof res);for(int i = 0;i < 1<<m;i ++)res[i][i] = 1;while(n){if(n&1) multi(res, a, 1<<m);multi(a, a, 1<<m);n>>=1;}
}void dfs(int a, int b, int p)
{if(p == m) c[a][((1<<m)-1)^b]++; // 方便 矩阵快速幂 这题c[i][j]代表第二层是i 第一层是i的合法方案的其他空缺; else{if(p > 0){if(!((a>>p&1)||(b>>p&1)||(b>>p-1&1)))dfs(a|1<<p, b|1<<p|1<<p-1, p+1);if(!((a>>p&1)||(b>>p&1)||(a>>p-1&1)))dfs(a|1<<p|1<<p-1, b|1<<p, p+1);}if(p+1 != m){if(!((a>>p&1)||(b>>p&1)||(b>>p+1&1)))dfs(a|1<<p, b|1<<p|1<<p+1, p+1);if(!((a>>p&1)||(b>>p&1)||(a>>p+1&1)))dfs(a|1<<p|1<<p+1, b|1<<p, p+1);}dfs(a, b, p+1);}
}int main()
{scanf("%lld%d", &n, &m);dfs(0, 0, 0);for(int i = 0;i < 1<<m;i ++)dp[1][i] = c[i][0];if(n >= 2)pow(c, n-2); for(int i = 0;i < 1<<m;i ++)dp[2][(1<<m)-1] = (dp[2][(1<<m)-1] + (LL)res[(1<<m)-1][i]*dp[1][i])%mod;cout<<dp[2][(1<<m)-1];return 0;
}