______________________________________________________________
和上一题差不多,注意初始化与判断即可
#include<bits/stdc++.h> using namespace std; long long int n,kk,tot,can[4096],dp[11][1096][121],cnt[4096]; long long ans; int main() { cin>>n>>kk; for(int i=0;i<=(1<<n)-1;i++) if((!(i&(i<<1)))){can[++tot]=i; int q=i; while(q){if(q%2==1){cnt[i]++;}q>>=1;} } for(int j=1;j<=tot;j++)dp[1][can[j]][cnt[can[j]]]=1; for(int i=2;i<=n;i++) for(int j=1;j<=tot;j++) for(int k=1;k<=tot;k++) {if((can[j]&can[k])||((can[j]<<1)&can[k])||((can[j]>>1)&can[k]))continue; for(int t=0;t<=kk;t++)dp[i][can[j]][t+cnt[can[j]]]+=dp[i-1][can[k]][t];} for(int i=1;i<=tot;i++)ans+=dp[n][can[i]][kk]; cout<<ans; }