ADDED SRM/686-U/1A.cpp Index: SRM/686-U/1A.cpp ================================================================== --- SRM/686-U/1A.cpp +++ SRM/686-U/1A.cpp @@ -0,0 +1,109 @@ +#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; + +class BracketSequenceDiv1 { public: + long long count(string s) + { + return count_multi(s, 0, s.size()) - 1; + } + + map, LL> memo1; + LL count_multi(const string& str, int s, int e) { + if(s == e) + return 1; + pair key(s,e); + if(memo1.count(key)) + return memo1[key]; + + LL total = 1; + for(int m=s+2; m<=e; ++m) + total += count_oneblock(str, s, m) * count_multi(str, m, e); + return memo1[key] = total; + } + + map, LL> memo2; + LL count_oneblock(const string& str, int s, int e) { + if(s == e) + return 0; + if(str[e-1]=='(' || str[e-1]=='[') + return 0; + const char op = (str[e-1]==')' ? '(' : '['); + + pair key(s,e); + if(memo2.count(key)) + return memo2[key]; + + LL total = 0; + for(int k=s; k +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 long long& Expected, const long long& 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(_, BracketSequenceDiv1().count(s));} +int main(){ + +CASE(0) + string s = "()[]"; + long long _ = 3LL; +END +CASE(1) + string s = "())"; + long long _ = 2LL; +END +CASE(2) + string s = "()()"; + long long _ = 4LL; +END +CASE(3) + string s = "([)]"; + long long _ = 2LL; +END +CASE(4) + string s = "())[]][]([]()]]()]]]"; + long long _ = 3854LL; +END +/* +CASE(5) + string s = ; + long long _ = LL; +END +CASE(6) + string s = ; + long long _ = LL; +END +*/ +} +// END CUT HERE ADDED SRM/686-U/1B-U.cpp Index: SRM/686-U/1B-U.cpp ================================================================== --- SRM/686-U/1B-U.cpp +++ SRM/686-U/1B-U.cpp @@ -0,0 +1,142 @@ +#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; } +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } + +class CyclesNumber { public: + vector getExpectation(vector n, vector m) + { + map> q; + for(int i=0; i, int> ans; + T(*max_element(n.begin(), n.end()), q, ans); + + vector result; + for(int i=0; i>& q, map,int>& ans) + { + vector Si; + Si.push_back(0); + Si.push_back(1); + for(int i=1; i<=n; ++i) + { + for(int m: q[i]) { + mint total = 0; + for(int f=0; f=1; --k) + Si[k] = Si[k]*i + Si[k-1]; + } + } +}; + +// 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 vector & Expected, const vector & 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(_, CyclesNumber().getExpectation(n, m));} +int main(){ +CASE(0) + int n_[] = {2}; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = {2}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = {5 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int n_[] = {3}; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = {0}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = {6 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int n_[] = {1, 2, 3}; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = {1, 3, 3}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = {1, 9, 53 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int n_[] = {10, 20, 30}; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = {10, 20, 30}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = {586836447, 544389755, 327675273 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int n_[] = {100000}; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = {300}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = {-1}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + int n_[] = ; + vector n(n_, n_+sizeof(n_)/sizeof(*n_)); + int m_[] = ; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE