Similar to D2 L3, the only difference is that D2L3 is that the difference between the two numbers is mostly K. This question is at least K, which makes it more difficult and hard to figure out the status of DP. Similar to D2L3, only unmatched numbers are considered. However, unmatched numbers are divided into two types: Good integers, g> = n + K, and Good integers, s> n & s <n + K. The number of 2N is considered in the order from large to small.
The Code is as follows:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; /************** Program Begin ********************** /const int MOD = 1000000007; const int MAX_N = 50; const int MAX_K = 10; int dp [2 * MAX_N + 1] [2 * MAX_N + 1] [1 <MAX_K]; class AlienAndSetDiv1 {public: int K; int calc (int s, int g, int n) {int res = 0; if (-1! = Dp [n] [g] [s]) {return dp [n] [g] [s];} if (n = 0) {if (0 = s & 0 = g) {// base caseres = 1 ;}} else {if (0 = s & 0 = g) {if (1 = K) {res + = 2 * calc (0, 1, n-1); // add} else {res + = 2 * calc (1, 0, n-1); // add} res % = MOD;} else if (0 = s & g! = 0) {res + = calc (0, g-1, n-1); // matchif (1 = K) {res + = calc (0, g + 1, n-1 ); // add} else {res + = calc (1, g, n-1); // add} res % = MOD;} else if (s! = 0 & 0 = g) {// K! = 1, only addint newset = s; int I = 0; for (I = 0; (newset & 0x80000000) = 0; newset = (newset <1 ), + + I) {// note the priority of bit operations. Brackets must be added.} int mx = 31-I; // The highest bitwise if (n + mx + 1-(n-1)> = K) Where mx is not 0) {// g-> 1int t = (s-(1 <mx); // clear mx bit res + = calc (t <1) | 1, 1, n-1); // addres % = MOD;} else {res + = calc (s <1) | 1, 0, n-1 ); // addres % = MOD ;}} else {// s! = 0 & 0! = G // K! = 1int newset = s; int I = 0; for (I = 0; (newset & 0x80000000) = 0; newset = (newset <1 ), + + I) {} int mx = 31-I; // The highest bit if (n + mx + 1-(n-1)> = K) Where mx is not 0) {// determine whether to change g int t = (s-(1 <mx); res + = calc (t <1, g, n-1 ); // matchres + = calc (t <1) | 1, g + 1, n-1 ); // add} else {res + = calc (s <1, g-1, n-1); // matchres + = calc (s <1) | 1, g, n-1); // add} res % = MOD;} dp [n] [g] [s] = res; return res;} int getNumber (int N, int K) {int res = 0; this-> K = K; memset (dp,-1, sizeof (dp); res = calc (0, 0, 2 * N); return res ;}}; /************** Program End *********************** */