状态压缩
hiho1049 入门题
#include <set> #include <map> #include <stack> #include <queue> #include <cmath> #include <vector> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b)) #define MIN(a,b) ((a)<=(b)?(a):(b)) #define OO 0x0fffffff using namespace std; typedef long long LL; const int N = 1024; const LL MOD = 1000000007; LL dp[N][64]; int status[64],scnt; int m,n; bool judge(int data){ bool want0 = false; for(int i=0;i<m;i++){ if(!want0) want0 = !(data&1); else{ if(data&1) return false; want0 = false; } data>>=1; } return (!want0); } int judge(int a,int b){ int ret = 0; for(int i=0;i<m;i++){ if((a&1)) { if(!(b&1)) return -1; } else if(b&1){ ret += (1<<i); } a>>=1; b>>=1; } return ret; } int main(){ cin>>n>>m; memset(dp,0,sizeof(dp)); scnt = 0; for(int i=0;i<(1<<m);i++) { if(judge(i)) { dp[1][i] = 1; status[scnt++] = i; } } for(int i=1;i<n;i++) for(int j=0;j<(1<<m);j++){ if(dp[i][j]){ for(int s=0;s<scnt;s++){ int news = judge(j,status[s]); if(news>=0) { dp[i+1][news] += dp[i][j]; dp[i+1][news] %= MOD; } } } } printf("%lld\n",dp[n][0]); return 0; }