ADDED SRM/578-U/1A.cpp Index: SRM/578-U/1A.cpp ================================================================== --- SRM/578-U/1A.cpp +++ SRM/578-U/1A.cpp @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +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_[n]; } + +// nCk :: O(1) time, O(n^2) space. +vector< vector > CP_; +mint C(LL n, LL k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector(nn+1,1)); + for(int kk=1; kk uf, sz; + int nc; + + UnionFind(int N): uf(N), sz(N,1), nc(N) + { for(int i=0; i= sz[b] ) swap(a, b); + uf[a] = b; + sz[b] += sz[a]; + --nc; + } + return (a!=b); + } +}; + +class GooseInZooDivOne { public: + int count(vector field, int dist) + { + int H = field.size(); + int W = field[0].size(); + vector > b; + for(int y=0; y +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(_, GooseInZooDivOne().count(field, dist));} +int main(){ + +CASE(0) + string field_[] = {"vvv"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = 0; + int _ = 3; +END +CASE(1) + string field_[] = {"."}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = 100; + int _ = 0; +END +CASE(2) + string field_[] = {"vvv"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = 1; + int _ = 0; +END +CASE(3) + string field_[] = {"v.v..................v............................" +,".v......v..................v.....................v" +,"..v.....v....v.........v...............v......v..." +,".........vvv...vv.v.........v.v..................v" +,".....v..........v......v..v...v.......v..........." +,"...................vv...............v.v..v.v..v..." +,".v.vv.................v..............v............" +,"..vv.......v...vv.v............vv.....v.....v....." +,"....v..........v....v........v.......v.v.v........" +,".v.......v.............v.v..........vv......v....." +,"....v.v.......v........v.....v.................v.." +,"....v..v..v.v..............v.v.v....v..........v.." +,"..........v...v...................v..............v" +,"..v........v..........................v....v..v..." +,"....................v..v.........vv........v......" +,"..v......v...............................v.v......" +,"..v.v..............v........v...............vv.vv." +,"...vv......v...............v.v..............v....." +,"............................v..v.................v" +,".v.............v.......v.........................." +,"......v...v........................v.............." +,".........v.....v..............vv.................." +,"................v..v..v.........v....v.......v...." +,"........v.....v.............v......v.v............" +,"...........v....................v.v....v.v.v...v.." +,"...........v......................v...v..........." +,"..........vv...........v.v.....................v.." +,".....................v......v............v...v...." +,".....vv..........................vv.v.....v.v....." +,".vv.......v...............v.......v..v.....v......" +,"............v................v..........v....v...." +,"................vv...v............................" +,"................v...........v........v...v....v..." +,"..v...v...v.............v...v........v....v..v...." +,"......v..v.......v........v..v....vv.............." +,"...........v..........v........v.v................" +,"v.v......v................v....................v.." +,".v........v................................v......" +,"............................v...v.......v........." +,"........................vv.v..............v...vv.." +,".......................vv........v.............v.." +,"...v.............v.........................v......" +,"....v......vv...........................v........." +,"....vv....v................v...vv..............v.." +,".................................................." +,"vv........v...v..v.....v..v..................v...." +,".........v..............v.vv.v.............v......" +,".......v.....v......v...............v............." +,"..v..................v................v....v......" +,".....v.....v.....................v.v......v......."}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = 3; + int _ = 898961330; +END +/* +CASE(4) + string field_[] = ; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = ; + int _ = ; +END +CASE(5) + string field_[] = ; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int dist = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/578-U/1B-U.cpp Index: SRM/578-U/1B-U.cpp ================================================================== --- SRM/578-U/1B-U.cpp +++ SRM/578-U/1B-U.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +static const unsigned MODVAL = 1000000007; + +class WolfInZooDivOne { public: + int count(int N, vector L_, vector R_) + { + vector L, R; + { + stringstream sin(accumulate(L_.begin(), L_.end(), string())); + for(int s; sin>>s;) L.push_back(s); + } + { + stringstream sin(accumulate(R_.begin(), R_.end(), string())); + for(int s; sin>>s;) R.push_back(s+1); + } + + set< pair > LR; + for(int i=0; i > LR_imp; + for(set< pair >::iterator it=LR.begin(); it!=LR.end(); ++it) { + bool important = true; + for(set< pair >::iterator kt=LR.begin(); kt!=LR.end(); ++kt) + if(it != kt && kt->first<=it->first && it->second<=kt->second) + important = false; + if(important) + LR_imp.push_back(*it); + } + + return solve(N, LR_imp); + } + + int solve(int N, const vector< pair >& LR) + { + memo = vector > >(N, + vector >(LR.size()+1, vector(LR.size()+1, -1))); + return rec(0, N, LR, 0, 0, 0, 0); + } + + vector > > memo; + int rec(int i, const int N, const vector< pair >& LR, int end_of_past, int end_of_two, int end_of_one, + int end_of_zero) + { + if(i == N) + return 1; + int& me = memo[i][end_of_two][end_of_one]; + if(me != -1) + return me; + + int ez = end_of_zero; + while(ez= end_of_two) { + int e1 = max(ep, end_of_one); + int e2 = max(ep, end_of_two); + while(e2 +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(_, WolfInZooDivOne().count(N, L, R));} +int main(){ + +CASE(0) + int N = 5; + string L_[] = {"0"}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = {"4"}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 16; +END +CASE(1) + int N = 5; + string L_[] = {"0 1"}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = {"2 4"}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 21; +END +CASE(2) + int N = 10; + string L_[] = {"0 4 2 7"}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = {"3 9 5 9"}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 225; +END +CASE(3) + int N = 100; + string L_[] = {"0 2 2 7 10 1","3 16 22 30 33 38"," 42 44 49 51 57 60 62"," 65 69 72 74 77 7","8 81 84 88 91 93 96"}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = {"41 5 13 22 12 13 ","33 41 80 47 40 ","4","8 96 57 66 ","80 60 71 79"," 70 77 ","99"," 83 85 93 88 89 97 97 98"}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 6419882; +END +/* +CASE(4) + int N = ; + string L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +CASE(5) + int N = ; + string L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + string R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +*/ +} +// END CUT HERE