ADDED SRM/621-U/1A.cpp Index: SRM/621-U/1A.cpp ================================================================== --- SRM/621-U/1A.cpp +++ SRM/621-U/1A.cpp @@ -0,0 +1,182 @@ +#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 RadioRange { public: + double RadiusProbability(vector X, vector Y, vector R, int Z) + { + vector> ev; + + const int N = X.size(); + 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 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(_, RadioRange().RadiusProbability(X, Y, R, Z));} +int main(){ + +CASE(0) + int X_[] = {0}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {0}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {5}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 10; + double _ = 0.5; +END +CASE(1) + int X_[] = {0}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {0}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {10}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 10; + double _ = 0.0; +END +CASE(2) + int X_[] = {10}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {10}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {10}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 10; + double _ = 0.4142135623730951; +END +CASE(3) + int X_[] = {11, -11, 0, 0}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {0, 0, 11, -11}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {10, 10, 10, 10}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 31; + double _ = 0.3548387096774194; +END +CASE(4) + int X_[] = {100}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {100}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {1}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 10; + double _ = 1.0; +END +CASE(5) + int X_[] = {1000000000}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {1000000000}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {1000000000}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 1000000000; + double _ = 0.41421356237309503; +END +CASE(6) + int X_[] = {20, -20, 0, 0}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {0, 0, 20, -20}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {50, 50, 50, 50}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 100; + double _ = 0.3; +END +CASE(7) + int X_[] = {0, -60, -62, -60, 63, -97}; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = {-72, 67, 61, -8, -32, 89}; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = {6, 7, 8, 7, 5, 6}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = 918; + double _ = 0.9407071068962471; +END +/* +CASE(8) + int X_[] = ; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = ; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = ; + double _ = ; +END +CASE(9) + int X_[] = ; + vector X(X_, X_+sizeof(X_)/sizeof(*X_)); + int Y_[] = ; + vector Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int Z = ; + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/621-U/1B-U.cpp Index: SRM/621-U/1B-U.cpp ================================================================== --- SRM/621-U/1B-U.cpp +++ SRM/621-U/1B-U.cpp @@ -0,0 +1,176 @@ +#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 TreesAnalysis { public: + typedef int EID; + int N; + long long treeSimilarity(vector tree1, vector tree2) + { + N = tree1.size() + 1; + vector>> T1(N); + vector> T2(N); + for(int v=0; v rec; + vector sub_vu(N), sub_uv(N, true); + rec = [&](int a, int b) { + sub_vu[b] = true; + sub_uv[b] = false; + for(int c: T2[b]) if(c!=a) + rec(b,c); + }; + rec(v2, u2); + + vector memo_vu(2*N, -1), memo_uv(2*N, -1); + for(int v1=0; v1>>& T1, int E, int a1, int b1, vector& sub, vector& memo) + { + if(memo[E]>=0) + return memo[E]; + + int total = sub[b1]; + for(pair ic: T1[b1]) {EID id=ic.first; int c1=ic.second; if(c1 != a1) { + total += sim(T1, id, b1, c1, sub, memo); + }} + return memo[E] = total; + } +}; + +// 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 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(_, TreesAnalysis().treeSimilarity(tree1, tree2));} +int main(){ + +CASE(0) + int tree1_[] = {1}; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = {1}; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = 1LL; +END +CASE(1) + int tree1_[] = {2, 4, 1, 0}; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = {1, 4, 4, 4}; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = 111LL; +END +CASE(2) + int tree1_[] = {1, 2, 3, 4}; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = {1, 2, 3, 4} +; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = 128LL; +END +CASE(3) + int tree1_[] = {2, 3, 4, 4, 5, 8, 5, 6, 10, 8}; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = {9, 0, 1, 0, 3, 0, 5, 0, 7, 10}; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = 6306LL; +END +CASE(4) + int tree1_[] = {222, 261, 167, 133, 174, 150, 165, 311, 208, 268, 111, 222, 154, 277, 282, 201, 46, 124, 194, 331, 4, 216, 111, +275, 72, 322, 137, 216, 241, 48, 72, 101, 232, 165, 151, 263, 139, 16, 122, 140, 84, 135, 314, 106, 309, 126, 102, +151, 208, 27, 242, 93, 83, 314, 136, 77, 82, 215, 16, 232, 286, 156, 294, 38, 67, 204, 206, 137, 174, 282, 188, +143, 84, 279, 236, 136, 158, 10, 65, 332, 122, 44, 329, 62, 174, 67, 102, 216, 245, 296, 287, 307, 93, 197, 169, +268, 266, 294, 157, 277, 95, 68, 214, 135, 211, 127, 82, 108, 212, 161, 243, 212, 207, 119, 119, 158, 97, 290, 21, +217, 230, 85, 171, 13, 138, 294, 304, 204, 318, 115, 70, 210, 195, 223, 37, 164, 149, 3, 164, 328, 316, 108, 330, +48, 38, 324, 222, 193, 50, 41, 184, 93, 148, 41, 151, 139, 106, 301, 264, 80, 249, 110, 244, 109, 212, 223, 279, +330, 67, 27, 301, 165, 236, 194, 3, 157, 1, 217, 311, 87, 105, 4, 286, 37, 6, 31, 111, 66, 230, 227, 244, 322, +196, 65, 69, 305, 112, 133, 231, 68, 153, 206, 309, 248, 329, 58, 69, 69, 328, 0, 29, 233, 243, 305, 167, 80, 65, +194, 190, 179, 142, 196, 324, 206, 134, 50, 272, 261, 10, 147, 329, 322, 14, 142, 169, 21, 296, 284, 241, 55, 304, +150, 166, 230, 167, 304, 87, 156, 156, 97, 274, 324, 196, 101, 82, 106, 260, 242, 233, 207, 305, 10, 166, 53, 18, +154, 233, 217, 296, 263, 168, 138, 30, 115, 135, 188, 98, 309, 292, 204, 150, 210, 332, 330, 166, 96, 70, 24, 229, +215, 201, 285, 40, 287, 142, 68, 133, 208, 268, 161, 310, 41, 203, 73, 275, 184, 163, 227, 89, 110, 328, 108, 112, +125, 164, 127, 179, 267, 221, 49, 139, 1, 84, 136, 38, 6, 70, 246, 243, 3, 188, 297}; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = {174, 262, 195, 288, 157, 278, 36, 133, 230, 273, 222, 138, 97, 23, 189, 141, 296, 55, 45, 301, 81, 199, 188, 289, +187, 164, 113, 58, 138, 300, 289, 282, 329, 91, 130, 178, 92, 143, 48, 81, 311, 133, 151, 286, 171, 196, 199, 80, +83, 121, 65, 151, 277, 136, 49, 111, 58, 36, 259, 14, 31, 9, 136, 181, 122, 324, 249, 114, 9, 37, 259, 242, 165, +174, 34, 36, 298, 92, 301, 237, 178, 82, 65, 295, 110, 311, 274, 235, 68, 56, 259, 180, 195, 52, 110, 68, 140, 71, +52, 296, 290, 115, 213, 82, 209, 209, 74, 178, 302, 131, 99, 205, 296, 309, 288, 180, 329, 71, 143, 58, 152, 273, +196, 7, 169, 88, 231, 331, 213, 181, 80, 249, 170, 246, 16, 127, 75, 276, 332, 174, 21, 180, 163, 78, 242, 312, +295, 199, 89, 142, 85, 195, 115, 119, 95, 94, 279, 290, 3, 33, 93, 284, 20, 47, 47, 78, 331, 271, 113, 179, 249, +331, 92, 324, 9, 71, 232, 46, 28, 289, 80, 28, 80, 134, 20, 280, 277, 48, 205, 107, 52, 320, 4, 191, 160, 182, +189, 227, 295, 115, 54, 195, 78, 292, 189, 273, 301, 69, 305, 36, 222, 167, 326, 106, 48, 45, 74, 61, 181, 311, +292, 270, 201, 34, 314, 218, 214, 92, 269, 18, 37, 151, 142, 209, 11, 227, 327, 198, 28, 272, 152, 22, 47, 143, +332, 253, 273, 35, 78, 130, 295, 223, 181, 329, 18, 238, 300, 186, 274, 99, 300, 322, 41, 185, 311, 288, 198, 2, +37, 83, 238, 133, 122, 178, 107, 106, 66, 238, 69, 90, 38, 109, 246, 278, 288, 250, 321, 269, 130, 28, 115, 122, +33, 185, 275, 99, 130, 99, 152, 268, 133, 249, 180, 30, 210, 201, 324, 29, 290, 143, 3, 269, 68, 106, 230, 1, +269, 29, 120, 259, 324, 328, 23, 243, 9, 61, 14, 118, 199, 146, 237, 14}; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = 11478648052LL; +END +/* +CASE(5) + int tree1_[] = ; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = ; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = LL; +END +CASE(6) + int tree1_[] = ; + vector tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); + int tree2_[] = ; + vector tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); + long long _ = LL; +END +*/ +} +// END CUT HERE ADDED SRM/621-U/1C-U.cpp Index: SRM/621-U/1C-U.cpp ================================================================== --- SRM/621-U/1C-U.cpp +++ SRM/621-U/1C-U.cpp @@ -0,0 +1,192 @@ +#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; + +struct SaComp { + const int sp, *sr, srlen; + SaComp(int sp, const vector& sr) : sp(sp), sr(&sr[0]), srlen(sr.size()) {} + bool operator()(int a, int b) const + { return make_pair(sr[a], a+sp +vector compute_suffix_array(RanIt beg, RanIt end) +{ + const int N = end - beg; + + vector sa(N); + vector sort_rank(beg, end); + for(int i=0; i block_id(N); + for(int i=1; i inv_sa(const vector& sa) +{ + vector isa(sa.size()); + for(int i=0; i +vector longest_common_prefix(RanIte beg, RanIte end, const vector& sa) +{ + const int N = sa.size(); + vector lcp(N); + vector inv = inv_sa(sa); + + int len = 0; + for(int i=0; i sa = compute_suffix_array(S.begin(), S.end()); + vector lcp = longest_common_prefix(S.begin(), S.end(), sa); + + for(int i=0; i+1 +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(_, StringsNightmareAgain().UniqueSubstrings(a, b, c, d, n));} +int main(){ + +CASE(0) + int a = 0; + int b = 0; + int c = 0; + int d = 0; + int n = 4; + long long _ = 2LL; +END +CASE(1) + int a = 2; + int b = 3; + int c = 1; + int d = 1; + int n = 6; + long long _ = 3LL; +END +CASE(2) + int a = 4; + int b = 3; + int c = 1; + int d = 1; + int n = 6; + long long _ = 3LL; +END +CASE(3) + int a = 4; + int b = 3; + int c = 3; + int d = 3; + int n = 10; + long long _ = 5LL; +END +CASE(4) + int a = 5; + int b = 3; + int c = 2; + int d = 3; + int n = 11; + long long _ = 9LL; +END +CASE(5) + int a = 10; + int b = 1000000; + int c = 1000000; + int d = 1; + int n = 51; + long long _ = 63LL; +END +/* +CASE(6) + int a = ; + int b = ; + int c = ; + int d = ; + int n = ; + long long _ = LL; +END +CASE(7) + int a = ; + int b = ; + int c = ; + int d = ; + int n = ; + long long _ = LL; +END +*/ +} +// END CUT HERE