SRM 605 div 2 T3
#include <bits/stdc++.h> #define Mo 1000000007 #define MAXN 50 #define MAXK 10 using namespace std; int dp[2*MAXN+2][1<<MAXK]; class AlienAndSetDiv2 { public: int N, K; int calc(int n, int unmatched) { int res = 0; if (-1 != dp[n][unmatched]) { return dp[n][unmatched]; } if (n == 2 * N + 1) { if (0 == unmatched) { res = 1; } } else { if (0 == unmatched) { res += ( 2 * calc(n + 1, 1) ) % Mo; } else { int newset = unmatched; int i = 0; for (i = 0; (newset & 0x80000000 ) == 0; newset = ( newset << 1 ), ++i) { } int mx = 31 - i; res += calc(n + 1, (unmatched - (1 << mx)) << 1 ); res %= Mo; if (mx != K - 1) { newset = unmatched; newset = ( (newset << 1) | 1 ); res += calc(n + 1, newset); res %= Mo; } } } dp[n][unmatched] = res; return res; }