ADDED SRM/654-U/1A.cpp Index: SRM/654-U/1A.cpp ================================================================== --- SRM/654-U/1A.cpp +++ SRM/654-U/1A.cpp @@ -0,0 +1,116 @@ +#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 SquareScores { public: + double calcexpectation(vector p, string s) + { + double ans = 0.0; + for(int k=0; 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 double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, SquareScores().calcexpectation(p, s));} +int main(){ + +CASE(0) + int p_[] = {1, 99}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "aaaba"; + double _ = 8.0; +END +CASE(1) + int p_[] = {10, 20, 70}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "aa?bbbb"; + double _ = 15.0; +END +CASE(2) + int p_[] = {10, 20, 30, 40}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "a??c?dc?b"; + double _ = 11.117; +END +CASE(3) + int p_[] = {25, 25, 21, 2, 2, 25}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "a??b???????ff??e"; + double _ = 21.68512690712425; +END +CASE(4) + int p_[] = {4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "??????????????????????????????"; + double _ = 31.16931963781721; +END +CASE(5) + int p_[] = {4, 3, 4, 3, 8, 2, 4, 3, 4, 4, 3, 2, 4, 4, 3, 4, 2, 4, 7, 6, 4, 4, 3, 4, 4, 3}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = "makigotapresentfromniko"; + double _ = 23.0; +END +/* +CASE(6) + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = ; + double _ = ; +END +CASE(7) + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + string s = ; + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/654-U/1B.cpp Index: SRM/654-U/1B.cpp ================================================================== --- SRM/654-U/1B.cpp +++ SRM/654-U/1B.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +template +struct DP2 +{ + const int N1, N2; + vector data; + DP2(int N1, int N2, const T& t = T()) + : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<28)); } + T& operator()(int i1, int i2) + { return data[ (i1*N2)+i2 ]; } + void swap(DP2& rhs) + { data.swap(rhs.data); } +}; + +class SuccessiveSubtraction2 { public: + vector calc(vector a, vector p, vector v) + { + vector answer; + for(int i=0; i& a) + { + DP2 dp(a.size(), 5, -0x1fffffff); + dp(1, 0) = 0; + + for(int i=2; i +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(_, SuccessiveSubtraction2().calc(a, p, v));} +int main(){ + +CASE(0) + int a_[] = {1, 2, 3, 4, 5}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {1, 2, 0, 4, 3}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {3, 9, -10, 5, 1}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {10, 16, 5, 5, 2 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int a_[] = {-100, -100, -100, -100, -100}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {0, 1, 2, 3, 4}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {0, 0, 0, 0, 0}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {400, 300, 200, 100, 0 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int a_[] = {83, 0, 25, 21}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {0, 3, 2, 1, 3, 1, 2}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {10, -90, 33, 52, -100, 0, 45}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {56, 125, 133, 81, 91, 143, 155 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int a_[] = {1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {0, 0, 0, 0}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {10, -10, 100, -100}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {10, -10, 100, -100 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int a_[] = {-11, -4, 28, 38, 21, -29, -45, 11, -58, -39, 92, 35, -56, -6, 29, -2, 61, 10, -29, -63}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {19, 5, 3, 10, 4, 18, 5, 2, 0, 15}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {-19, 21, 7, -66, 38, -39, -22, 24, -32, 13}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {451, 443, 412, 440, 457, 467, 468, 464, 443, 458 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(5) +int a_[] = {10, 20}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {0, 1}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = {2, 3}; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = {-18, -1}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(6) + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + int v_[] = ; + vector v(v_, v_+sizeof(v_)/sizeof(*v_)); + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/654-U/1C-U.cpp Index: SRM/654-U/1C-U.cpp ================================================================== --- SRM/654-U/1C-U.cpp +++ SRM/654-U/1C-U.cpp @@ -0,0 +1,180 @@ +#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; } +mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } +mint operator/(mint x, mint y) { return x/=y; } + +vector FAC_(1,1); +mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[size_t(n)]; } + +vector< vector > CP_; +mint C(int n, int k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector(nn+1,1)); + for(int kk=1; kk a, vector b, int s1, int s2) + { + const int N = a.size() + 1; + + // Graph + vector> g(N); + for(int i=0; i> children(N); + vector parent(N, -1); + vector tree_size(N); + function rec; rec = [&](int pre, int v) { + int sub_size = 1; + for(auto u: g[v]) if(u != pre) { + children[v].push_back(u); + parent[u] = v; + sub_size += rec(v, u); + } + tree_size[v] = sub_size; + }; + rec(-1, s1); + + // Mark s1--s2 + vector s2_path(N); + for(int v=s2; v!=-1; v=parent[v]) + s2_path[v] = true; + + // Parts except s1--s2 path is easy. + // It's dp-on-tree[s1 \setminus s2] * dp-on-tree[s1] * C(N, |s1|) * C(N-|s1|, |s2|). + // CAUTION: ENSURE s1 and s2 NOT be included on dp-on-tree and |s|. + // otherwise C(N, |s1|) will fix the timing s1 is filled, which reduces the freedom in + // path mode. + + // What about the rest N-|s1|-|s2|? + } +}; + +// 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(_, TwoEntrances().count(a, b, s1, s2));} +int main(){ + +CASE(0) + int a_[] = {0, 1, 2}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1, 2, 3}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = 0; + int s2 = 1; + int _ = 4; +END +CASE(1) + int a_[] = {0, 1, 2}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1, 2, 3}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = 0; + int s2 = 2; + int _ = 9; +END +CASE(2) + int a_[] = {0, 1, 1, 3, 3, 3, 6, 7, 6}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = 1; + int s2 = 9; + int _ = 16000; +END +CASE(3) + int a_[] = {0, 0, 1, 2, 3, 1, 2, 0, 6, 5, 10, 10}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10, 11, 12}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = 3; + int s2 = 6; + int _ = 310464; +END +CASE(4) + int a_[] = {0}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = 1; + int s2 = 0; + int _ = 2; +END +/* +CASE(5) + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = ; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = ; + int s2 = ; + int _ = ; +END +CASE(6) + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = ; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + int s1 = ; + int s2 = ; + int _ = ; +END +*/ +} +// END CUT HERE