Artifact Content
Not logged in

Artifact 71a7603b4167a67f4627be476312e0f9bd641f74


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
	mint(LL       x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<28)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

class BearCries { public:
	int count(string message)
	{
		const int N = message.size();
		DP3<int> dp(N, N, N, -1);
		function<int(int,int,int)> rec;
		rec = [&](int i, int s, int su) {
			if(i == N)
				return (s==0 && su==0 ? 1 : 0);
			if(dp(i,s,su) != -1)
				return dp(i,s,su);
			mint total = 0;
			if(message[i]==';') {
				total += rec(i+1, s+1, su);
				if(su) total += mint(rec(i+1, s, su-1)) * su;
			} else {
				if(s) total += mint(rec(i+1, s-1, su+1)) * s;
				if(su) total += mint(rec(i+1, s, su)) * su;
			}
			return dp(i,s,su) = total.val;
		};
		return rec(0, 0, 0);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BearCries().count(message));}
int main(){

CASE(0)
	string message = ";_;;_____;"; 
	int _ = 2; 
END
CASE(1)
	string message = ";;;___;;;"; 
	int _ = 36; 
END
CASE(2)
	string message = "_;__;"; 
	int _ = 0; 
END
CASE(3)
	string message = ";_____________________________________;"; 
	int _ = 1; 
END
CASE(4)
	string message = ";__;____;"; 
	int _ = 0; 
END
CASE(5)
	string message = ";_;;__;_;;"; 
	int _ = 52; 
END
/*
CASE(6)
	string message = ; 
	int _ = ; 
END
CASE(7)
	string message = ; 
	int _ = ; 
END
*/
}
// END CUT HERE