da6b080c3a 2015-10-20 kinaba: #include <iostream> da6b080c3a 2015-10-20 kinaba: #include <sstream> da6b080c3a 2015-10-20 kinaba: #include <iomanip> da6b080c3a 2015-10-20 kinaba: #include <vector> da6b080c3a 2015-10-20 kinaba: #include <string> da6b080c3a 2015-10-20 kinaba: #include <map> da6b080c3a 2015-10-20 kinaba: #include <set> da6b080c3a 2015-10-20 kinaba: #include <algorithm> da6b080c3a 2015-10-20 kinaba: #include <numeric> da6b080c3a 2015-10-20 kinaba: #include <iterator> da6b080c3a 2015-10-20 kinaba: #include <functional> da6b080c3a 2015-10-20 kinaba: #include <complex> da6b080c3a 2015-10-20 kinaba: #include <queue> da6b080c3a 2015-10-20 kinaba: #include <stack> da6b080c3a 2015-10-20 kinaba: #include <cmath> da6b080c3a 2015-10-20 kinaba: #include <cassert> da6b080c3a 2015-10-20 kinaba: #include <tuple> da6b080c3a 2015-10-20 kinaba: using namespace std; da6b080c3a 2015-10-20 kinaba: typedef long long LL; da6b080c3a 2015-10-20 kinaba: typedef complex<double> CMP; da6b080c3a 2015-10-20 kinaba: da6b080c3a 2015-10-20 kinaba: static const unsigned MODVAL = 1000000007; da6b080c3a 2015-10-20 kinaba: struct mint da6b080c3a 2015-10-20 kinaba: { da6b080c3a 2015-10-20 kinaba: unsigned val; da6b080c3a 2015-10-20 kinaba: mint():val(0){} da6b080c3a 2015-10-20 kinaba: mint(int x):val(x%MODVAL) {} da6b080c3a 2015-10-20 kinaba: mint(unsigned x):val(x%MODVAL) {} da6b080c3a 2015-10-20 kinaba: mint(LL x):val(x%MODVAL) {} da6b080c3a 2015-10-20 kinaba: }; da6b080c3a 2015-10-20 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } da6b080c3a 2015-10-20 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } da6b080c3a 2015-10-20 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } da6b080c3a 2015-10-20 kinaba: mint operator+(mint x, mint y) { return x+=y; } da6b080c3a 2015-10-20 kinaba: mint operator-(mint x, mint y) { return x-=y; } da6b080c3a 2015-10-20 kinaba: mint operator*(mint x, mint y) { return x*=y; } da6b080c3a 2015-10-20 kinaba: da6b080c3a 2015-10-20 kinaba: template<typename T> da6b080c3a 2015-10-20 kinaba: struct DP3 da6b080c3a 2015-10-20 kinaba: { da6b080c3a 2015-10-20 kinaba: int N1, N2, N3; da6b080c3a 2015-10-20 kinaba: vector<T> data; da6b080c3a 2015-10-20 kinaba: DP3(int N1, int N2, int N3, const T& t = T()) da6b080c3a 2015-10-20 kinaba: : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<28)); } da6b080c3a 2015-10-20 kinaba: T& operator()(int i1, int i2, int i3) da6b080c3a 2015-10-20 kinaba: { return data[ ((i1*N2)+i2)*N3+i3 ]; } da6b080c3a 2015-10-20 kinaba: void swap(DP3& rhs) da6b080c3a 2015-10-20 kinaba: { data.swap(rhs.data); } da6b080c3a 2015-10-20 kinaba: }; da6b080c3a 2015-10-20 kinaba: da6b080c3a 2015-10-20 kinaba: class BearCries { public: da6b080c3a 2015-10-20 kinaba: int count(string message) da6b080c3a 2015-10-20 kinaba: { da6b080c3a 2015-10-20 kinaba: const int N = message.size(); da6b080c3a 2015-10-20 kinaba: DP3<int> dp(N, N, N, -1); da6b080c3a 2015-10-20 kinaba: function<int(int,int,int)> rec; da6b080c3a 2015-10-20 kinaba: rec = [&](int i, int s, int su) { da6b080c3a 2015-10-20 kinaba: if(i == N) da6b080c3a 2015-10-20 kinaba: return (s==0 && su==0 ? 1 : 0); da6b080c3a 2015-10-20 kinaba: if(dp(i,s,su) != -1) da6b080c3a 2015-10-20 kinaba: return dp(i,s,su); da6b080c3a 2015-10-20 kinaba: mint total = 0; da6b080c3a 2015-10-20 kinaba: if(message[i]==';') { da6b080c3a 2015-10-20 kinaba: total += rec(i+1, s+1, su); da6b080c3a 2015-10-20 kinaba: if(su) total += mint(rec(i+1, s, su-1)) * su; da6b080c3a 2015-10-20 kinaba: } else { da6b080c3a 2015-10-20 kinaba: if(s) total += mint(rec(i+1, s-1, su+1)) * s; da6b080c3a 2015-10-20 kinaba: if(su) total += mint(rec(i+1, s, su)) * su; da6b080c3a 2015-10-20 kinaba: } da6b080c3a 2015-10-20 kinaba: return dp(i,s,su) = total.val; da6b080c3a 2015-10-20 kinaba: }; da6b080c3a 2015-10-20 kinaba: return rec(0, 0, 0); da6b080c3a 2015-10-20 kinaba: } da6b080c3a 2015-10-20 kinaba: }; da6b080c3a 2015-10-20 kinaba: da6b080c3a 2015-10-20 kinaba: // BEGIN CUT HERE da6b080c3a 2015-10-20 kinaba: #include <ctime> da6b080c3a 2015-10-20 kinaba: double start_time; string timer() da6b080c3a 2015-10-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } da6b080c3a 2015-10-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) da6b080c3a 2015-10-20 kinaba: { os << "{ "; da6b080c3a 2015-10-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) da6b080c3a 2015-10-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } da6b080c3a 2015-10-20 kinaba: void verify_case(const int& Expected, const int& Received) { da6b080c3a 2015-10-20 kinaba: bool ok = (Expected == Received); da6b080c3a 2015-10-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; da6b080c3a 2015-10-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } da6b080c3a 2015-10-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); da6b080c3a 2015-10-20 kinaba: #define END verify_case(_, BearCries().count(message));} da6b080c3a 2015-10-20 kinaba: int main(){ da6b080c3a 2015-10-20 kinaba: da6b080c3a 2015-10-20 kinaba: CASE(0) da6b080c3a 2015-10-20 kinaba: string message = ";_;;_____;"; da6b080c3a 2015-10-20 kinaba: int _ = 2; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(1) da6b080c3a 2015-10-20 kinaba: string message = ";;;___;;;"; da6b080c3a 2015-10-20 kinaba: int _ = 36; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(2) da6b080c3a 2015-10-20 kinaba: string message = "_;__;"; da6b080c3a 2015-10-20 kinaba: int _ = 0; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(3) da6b080c3a 2015-10-20 kinaba: string message = ";_____________________________________;"; da6b080c3a 2015-10-20 kinaba: int _ = 1; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(4) da6b080c3a 2015-10-20 kinaba: string message = ";__;____;"; da6b080c3a 2015-10-20 kinaba: int _ = 0; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(5) da6b080c3a 2015-10-20 kinaba: string message = ";_;;__;_;;"; da6b080c3a 2015-10-20 kinaba: int _ = 52; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: /* da6b080c3a 2015-10-20 kinaba: CASE(6) da6b080c3a 2015-10-20 kinaba: string message = ; da6b080c3a 2015-10-20 kinaba: int _ = ; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: CASE(7) da6b080c3a 2015-10-20 kinaba: string message = ; da6b080c3a 2015-10-20 kinaba: int _ = ; da6b080c3a 2015-10-20 kinaba: END da6b080c3a 2015-10-20 kinaba: */ da6b080c3a 2015-10-20 kinaba: } da6b080c3a 2015-10-20 kinaba: // END CUT HERE