ADDED SRM/671-U/1A.cpp Index: SRM/671-U/1A.cpp ================================================================== --- SRM/671-U/1A.cpp +++ SRM/671-U/1A.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex 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 +struct DP3 +{ + int N1, N2, N3; + vector 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 dp(N, N, N, -1); + function 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 +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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 ADDED SRM/671-U/1B.cpp Index: SRM/671-U/1B.cpp ================================================================== --- SRM/671-U/1B.cpp +++ SRM/671-U/1B.cpp cannot compute difference between binary files