题目:http://community.topcoder.com/stat?c=problem_statement&pm=12929&rd=15820

参考:http://apps.topcoder.com/wiki/display/tc/SRM+602

需要仔细分析,要注意base case的判断,还要注意不要漏了repaint的情况。

代码:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>

#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair

/*************** Program Begin **********************/
const int MOD = 1e9 + 7;

long long dp[55][55];
long long C[55][55];

class BlackBoxDiv2 {
public:
	int w, h;
	void calc()
	{
		C[0][0] = 1;
		for (int i = 1; i <= 50; i++) {
			C[i][0] = C[i][i] = 1;
			for (int j = 1; j < i; j++) {
				C[i][j] = C[i-1][j] + C[i-1][j-1];
				C[i][j] %= MOD;
			}
		}
	}
	long long rec(int x, int y)
	{
		if (0 == x) {		// base case
			if (0 == y) {
				return 1;
			} else {
				return 0;
			}
		}

		long long & res = dp[x][y];
		if (res != -1) {
			return res;
		}

		res = 0;
		for (int s = 0; s <= y; s++) {
			for (int r = 0; r <= h - y; r++) {
				if (s + r < 1) {
					continue;
				}
				long long t = C[y][s] * C[h - y][r] % MOD;
				res += ( t * rec(x - 1, y - s) ) % MOD;
				res %= MOD;
			}
		}
		return res;
	}
	int count(string front, string side) {
		this->w = 0; this->h = 0;
		for (int i = 0; i < front.size(); i++) {
			w += ( front[i] == 'B' ? 1 : 0 );
		}
		for (int i = 0; i < side.size(); i++) {
			h += ( side[i] == 'B' ? 1 : 0 );
		}
		calc();
		memset(dp, -1, sizeof(dp));
		return rec(w, h);
	}

};

/************** Program End ************************/


Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐