ADDED SRM/604-U/1A.cpp Index: SRM/604-U/1A.cpp ================================================================== --- SRM/604-U/1A.cpp +++ SRM/604-U/1A.cpp @@ -0,0 +1,111 @@ +#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 PowerOfThree { public: + string ableToGet(int x_, int y_) + { + LL x=x_, y=y_; + for(LL tk=1;; tk*=3) + { + if(x==0 && y==0) + break; + LL xr = ((x/tk)%3+3)%3; + LL yr = ((y/tk)%3+3)%3; + if(xr==1 && yr==0) x-=tk; + else if(xr==2 && yr==0) x+=tk; + else if(xr==0 && yr==1) y-=tk; + else if(xr==0 && yr==2) y+=tk; + else return "Impossible"; + } + return "Possible"; + } +}; + +// 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(_, PowerOfThree().ableToGet(x, y));} +int main(){ + +CASE(0) + int x = 1; + int y = 3; + string _ = "Possible"; +END +CASE(1) + int x = 0; + int y = 2; + string _ = "Possible"; +END +CASE(2) + int x = 1; + int y = 9; + string _ = "Impossible"; +END +CASE(3) + int x = 3; + int y = 0; + string _ = "Impossible"; +END +CASE(4) + int x = 1; + int y = 1; + string _ = "Impossible"; +END +CASE(5) + int x = -6890; + int y = 18252; + string _ = "Possible"; +END +CASE(6) + int x = 1000000000; + int y = -1000000000; + string _ = "Impossible"; +END +CASE(7) + int x = 0; + int y = 0; + string _ = "Possible"; +END +CASE(8) + int x = 8; + int y = 3; + string _ = "P"; +END +CASE(9) + // -3^0 - 3^1 - ... - 3^18 + 3^19 + // NOTE: 3^19 = 1162261467 > 10^9 + int x = 581130734; + int y = 0; + string _ = "P"; +END +} +// END CUT HERE ADDED SRM/604-U/1B-U.cpp Index: SRM/604-U/1B-U.cpp ================================================================== --- SRM/604-U/1B-U.cpp +++ SRM/604-U/1B-U.cpp @@ -0,0 +1,200 @@ +#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 +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); } +}; + +static const int INF = 0x3fffffff; +class FoxConnection { public: + int minimalDistance(vector A, vector B, string haveFox) + { + int N = haveFox.size(); + vector> G(N); + for(int i=0; i>(N)); + ts_memo.resize(N+1, vector(N, INF)); + for(int root=0; root& L = rec(-1, root, G, haveFox); + best = min(best, L[0]); + } + return best==INF ? 0 : best; + } + + vector> ts_memo; + int treeSize(int prev, int v, const vector>& G, const string& haveFox) + { + if(ts_memo[prev+1][v] != INF) + return ts_memo[prev+1][v]; + + int ts = (haveFox[v]=='Y'); + for(int u : G[v]) if(u != prev) + ts += treeSize(v, u, G, haveFox); + return ts_memo[prev+1][v] = ts; + } + + vector>> memo; + const vector& rec(int prev, int v, const vector>& G, const string& haveFox) + { + if(!memo[prev+1][v].empty()) + return memo[prev+1][v]; + int N = G.size(); + + vector*> children; + for(int u : G[v]) if(u != prev) + children.push_back(&rec(v,u,G,haveFox)); + int Z = children.size(); + + DP2 dp(Z+1, N, INF); + dp(0,0) = 0; + for(int i=0; i& ch = *children[i]; + for(int t=0; t& L = memo[prev+1][v]; + L.resize(N, INF); + + const int TS = treeSize(prev, v, G, haveFox); + for(int i=0; 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 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(_, FoxConnection().minimalDistance(A, B, haveFox));} +int main(){ + +CASE(0) + int A_[] = {1,2,3}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2,3,4}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "YNNY"; + int _ = 2; +END +CASE(1) + int A_[] = {1,1,1,1}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2,3,4,5}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "NYYYY"; + int _ = 1; +END +CASE(2) + int A_[] = {1,3,4,5,4}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2,2,2,4,6}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "YNYNYY"; + int _ = 2; +END +CASE(3) + int A_[] = {1,2,3,4,5,6,7,8,9}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2,3,4,5,6,7,8,9,10}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "YNNNYNYNNY"; + int _ = 7; +END +CASE(4) + int A_[] = {1,2,3,4,3,6,8,7}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2,3,4,5,6,8,9,6}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "YNNYYNYYY"; + int _ = 3; +END +CASE(5) + int A_[] = {1}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "NY"; + int _ = 0; +END +CASE(6) + int A_[] = {1}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = "NN"; + int _ = 0; +END +/* +CASE(7) + int A_[] = ; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = ; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = ; + int _ = ; +END +CASE(8) + int A_[] = ; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = ; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string haveFox = ; + int _ = ; +END +*/ +} +// END CUT HERE