ADDED SRM/652-U/1A.cpp Index: SRM/652-U/1A.cpp ================================================================== --- SRM/652-U/1A.cpp +++ SRM/652-U/1A.cpp @@ -0,0 +1,102 @@ +#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 CountryGroupHard { public: + string solve(vector a) + { + return rec(0, a)==1 ? "Sufficient" : "Insufficient"; + } + + map memo; + int rec(int k, const vector& a) + { + if(k == a.size()) + return 1; + if(memo.count(k)) + return memo[k]; + + int cnt = 0; + for(int v=1; k+v<=int(a.size()); ++v) { + set s(a.begin()+k, a.begin()+k+v); + s.erase(0); + s.erase(v); + if(s.empty()) + cnt += rec(k+v, a); + } + return memo[k] = (cnt>=2 ? 2 : cnt); + } +}; + +// 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 string& Expected, const string& 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(_, CountryGroupHard().solve(a));} +int main(){ + +CASE(0) + int a_[] = {0,2,3,0,0}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = "Sufficient"; +END +CASE(1) + int a_[] = {0,2,0}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = "Insufficient"; +END +CASE(2) + int a_[] = {0,3,0,0,3,0}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = "Sufficient"; +END +CASE(3) + int a_[] = {0,0,3,3,0,0}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = "Insufficient"; +END +CASE(4) + int a_[] = {2,2,0,2,2}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = "Sufficient"; +END +/* +CASE(5) + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = ; +END +CASE(6) + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/652-U/1B.cpp Index: SRM/652-U/1B.cpp ================================================================== --- SRM/652-U/1B.cpp +++ SRM/652-U/1B.cpp @@ -0,0 +1,215 @@ +#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; + +template +class IdGen +{ + map v2id_; + vector id2v_; +public: + int v2id(const T& v) { + if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } + return v2id_[v]; + } + const T& id2v(int i) const { return id2v_[i]; } + int size() const { return id2v_.size(); } +}; + +template, typename Flow=LL, int NV=2048> +class MaxFlow +{ + IdGen idgen; + vector G[NV]; + Flow F[NV][NV]; + +public: + void addEdge( Vert s_, Vert t_, Flow f ) + { + const int s = idgen.v2id(s_), t = idgen.v2id(t_); + G[s].push_back(t); + G[t].push_back(s); + F[s][t] = f; + F[t][s] = 0; + } + + Flow calc( Vert s_, Vert t_ ) + { + const int S = idgen.v2id(s_), D = idgen.v2id(t_); + for( Flow total=0 ;; ) { + // Do BFS and compute the level for each node. + int LV[NV] = {0}; + vector Q(1, S); + for(int lv=1; !Q.empty(); ++lv) { + vector Q2; + for(size_t i=0; i!=Q.size(); ++i) { + const vector& ne = G[Q[i]]; + for(size_t j=0; j!=ne.size(); ++j) + if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) + LV[ne[j]]=lv, Q2.push_back(ne[j]); + } + Q.swap(Q2); + } + + // Destination is now unreachable. Done. + if( !LV[D] ) + return total; + + // Iterating DFS. + bool blocked[NV] = {}; + total += dinic_dfs( S, D, LV, 0x7fffffff, blocked ); + } + } + +private: + Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] ) + { + Flow flow_out = 0; + for(size_t i=0; i!=G[v].size(); ++i) { + int u = G[v][i]; + if( LV[v]+1==LV[u] && F[v][u] ) { + Flow f = min(flow_in-flow_out, F[v][u]); + if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) { + F[v][u] -= f; + F[u][v] += f; + flow_out += f; + if( flow_in == flow_out ) return flow_out; + } + } + } + blocked[v] = (flow_out==0); + return flow_out; + } +}; + +class Singing { public: + int solve(int N, int low, int high, vector pitch) + { + map, int> edge; + for(int i=0; i+1* mf = new MaxFlow<>; + const LL INF = 0x3fffffff; + + enum{BOB, LEFT, RIGHT, ALICE}; + pair Bob(BOB,0); + pair Alice(ALICE,0); + for(int v=1; v<=N; ++v) { + if(v>high) + mf->addEdge(Bob, make_pair(LEFT,v), INF); + mf->addEdge(make_pair(LEFT,v), make_pair(RIGHT,v), INF); + if(vaddEdge(make_pair(RIGHT,v), Alice, INF); + } + for(auto e: edge) { + int v = e.first.first; + int u = e.first.second; + int c = e.second; + mf->addEdge(make_pair(RIGHT,v), make_pair(LEFT,u), c); + mf->addEdge(make_pair(RIGHT,u), make_pair(LEFT,v), c); + } + + LL ans = mf->calc(Bob, Alice); + delete mf; + return int(ans); + } +}; + +// 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(_, Singing().solve(N, low, high, pitch));} +int main(){ + +CASE(0) + int N = 3; + int low = 2; + int high = 2; + int pitch_[] = {1,2,3,2,1,2}; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = 2; +END +CASE(1) + int N = 10; + int low = 3; + int high = 7; + int pitch_[] = {4,4,5,5,6,5,3,6}; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = 0; +END +CASE(2) + int N = 6; + int low = 2; + int high = 5; + int pitch_[] = {5,3,1,6,4,2}; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = 1; +END +CASE(3) + int N = 10; + int low = 4; + int high = 5; + int pitch_[] = {1,4,3,5,2,5,7,5,9}; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = 3; +END +CASE(4) + int N = 100; + int low = 20; + int high = 80; + int pitch_[] = {2,27,3,53,53,52,52,60,85,89,100,53,60,2,3,53,100,89,40,42,2,53,2,85}; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = 5; +END +/* +CASE(5) + int N = ; + int low = ; + int high = ; + int pitch_[] = ; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = ; +END +CASE(6) + int N = ; + int low = ; + int high = ; + int pitch_[] = ; + vector pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/652-U/1C-U.cpp Index: SRM/652-U/1C-U.cpp ================================================================== --- SRM/652-U/1C-U.cpp +++ SRM/652-U/1C-U.cpp @@ -0,0 +1,174 @@ +#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; } + +// nCk :: O(1) time, O(n^2) space. +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 Vec; +inline int operator*(Vec v, Vec u) { + return get<0>(v)*get<0>(u) + get<1>(v)*get<1>(u) + get<2>(v)*get<2>(u); +} + +class RockPaperScissorsMagic { public: + int count(int W, int L, int T, vector card) + { + const int N0 = std::count(card.begin(), card.end(), 0); + const int N1 = std::count(card.begin(), card.end(), 1); + const int N2 = std::count(card.begin(), card.end(), 2); + + mint ans = 0; + for(int x1=0; x1<=N0; ++x1) + for(int x2=0; x1+x2<=N0; ++x2) + for(int y1=0; y1<=N1; ++y1) + for(int y2=0; y1+y2<=N1; ++y2) + for(int z1=0; z1<=N2; ++z1) + for(int z2=0; z1+z2<=N2; ++z2) { + int x3,y3,z3; + Vec x(x1, x2, x3=N0-x1-x2); + Vec y(y1, y2, y3=N1-y1-y2); + Vec z(z1, z2, z3=N2-z1-z2); + Vec TLW(T,L,W); + Vec WTL(W,T,L); + Vec LTW(L,T,W); + if(TLW*x+WTL*y != TLW*y+WTL*x) continue; + if(T!=L && x1-x2!=y1-y2) continue; + if(W!=L && x1-x3!=y1-y3) continue; + if(TLW*x+WTL*z != TLW*z+WTL*x) continue; + if(T!=L && x1-x2!=z1-z2) continue; + if(W!=L && x1-x3!=z1-z3) continue; + + ans += C(N0, x1) * C(N0-x1, x2) + * C(N1, y1) * C(N1-y1, y2) + * C(N2, z1) * C(N2-z1, z2); + } + return ans.val; + } +}; + +// 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(_, RockPaperScissorsMagic().count(win, lose, tie, card));} +int main(){ + +CASE(0) + int win = 2; + int lose = 0; + int tie = 1; + int card_[] = {0,1,2}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 3; +END +CASE(1) + int win = 2; + int lose = 0; + int tie = 1; + int card_[] = {0,0,0}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 6; +END +CASE(2) + int win = 0; + int lose = 0; + int tie = 0; + int card_[] = {1,0,2,2,2,0}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 729; +END +CASE(3) + int win = 514; + int lose = 451; + int tie = 145; + int card_[] = {0,0,0,0,0,1,1,1,1,1,1,2,2,2}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 0; +END +CASE(4) + int win = 3; + int lose = 6; + int tie = 9; + int card_[] = {0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 2336040; +END +CASE(5) + int win = 514; + int lose = 451; + int tie = 145; + int card_[] = {0,0,0,1,1,1,1,1,1,2,2,2,2,2,2}; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = 116100; +END +/* +CASE(6) + int win = ; + int lose = ; + int tie = ; + int card_[] = ; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = ; +END +CASE(7) + int win = ; + int lose = ; + int tie = ; + int card_[] = ; + vector card(card_, card_+sizeof(card_)/sizeof(*card_)); + int _ = ; +END +*/ +} +// END CUT HERE