ADDED SRM/TCO13-2A-U/1A.cpp Index: SRM/TCO13-2A-U/1A.cpp ================================================================== --- SRM/TCO13-2A-U/1A.cpp +++ SRM/TCO13-2A-U/1A.cpp @@ -0,0 +1,106 @@ +#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; + +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<<26)); } + T& operator()(int i1, int i2) + { return data[ (i1*N2)+i2 ]; } + void swap(DP2& rhs) + { data.swap(rhs.data); } +}; + +class TheLargestString { public: + string find(string s, string t) + { + const int N = s.size(); + DP2< pair > best(N+1,N+1); + for(int i=N-1; i>=0; --i) + { + for(int len=1; len<=N-i; ++len) + { + pair cand = best(i+1, len); + pair cand2(s[i]+best(i+1, len-1).first, t[i]+best(i+1,len-1).second); + if(cand.first+cand.second < cand2.first+cand2.second) + cand = cand2; + best(i, len) = cand; + } + } + string ans; + for(int len=0; len<=N; ++len) + ans = max(ans, best(0,len).first + best(0,len).second); + return 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 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(_, TheLargestString().find(s, t));} +int main(){ +CASE(0) + string s = "ab"; + string t = "zy"; + string _ = "by"; +END +CASE(1) + string s = "abacaba"; + string t = "zzzaaaa"; + string _ = "cbaaaa"; +END +CASE(2) + string s = "x"; + string t = "x"; + string _ = "xx"; +END +CASE(3) + string s = "abbabbabbababaaaabbababab"; + string t = "bababbaabbbababbbbababaab"; + string _ = "bbbbbbbbbbbbbbbbbbaaab"; +END +/* +CASE(4) + string s = ; + string t = ; + string _ = ; +END +CASE(5) + string s = ; + string t = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/TCO13-2A-U/1C-U.cpp Index: SRM/TCO13-2A-U/1C-U.cpp ================================================================== --- SRM/TCO13-2A-U/1C-U.cpp +++ SRM/TCO13-2A-U/1C-U.cpp @@ -0,0 +1,136 @@ +#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; + +class ThePowers { public: + long long find(int A, int B) + { + LL total = 1; + + int prev_cnt = 1; + for(int K=31; K>=1; --K) + { + int ub = invpow(A,K); + int cnt = num_ppprime_under(ub); + LL x = solve2(K,B) * (cnt - prev_cnt); + total += x; + prev_cnt = cnt; + } + return total; + } + + // max x s.t. x^K <= A; + int invpow(int A, int K) + { + int v = 0; + int c = (int)pow(A, 1.0/K); + for(int x=max(1,c-3); x<=c+3; ++x) + if( pow_leq(x, K, A) ) + v = x; + return v; + } + + // number of non-square, cubic, ... numbers in [1,N] + int num_ppprime_under(int N) + { + int total = N; + vector num(32); + for(int p=num.size()-1; p>=2; --p) + { + num[p] = invpow(N, p) - 1; + for(int q=p+p; qN) + return false; + } + return true; + } + + // number of [1,K]*[1,B] + LL solve2(int K, int B) + { + set test; + for(LL x=1; x<=K; ++x) + for(LL y=1; y<=B; ++y) + test.insert(x*y); + return test.size(); + } +}; + +// 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(_, ThePowers().find(A, B));} +int main(){ + +CASE(0) + int A = 7; + int B = 4; + long long _ = 23LL; +END +CASE(1) + int A = 1; + int B = 1; + long long _ = 1LL; +END +CASE(2) + int A = 1000000000; + int B = 1000000000; + long long _ = 999983644283653287LL; +END +CASE(3) + int A = 999999999; + int B = 5; + long long _ = 4999934406LL; +END +/* +CASE(4) + int A = ; + int B = ; + long long _ = LL; +END +CASE(5) + int A = ; + int B = ; + long long _ = LL; +END +*/ +} +// END CUT HERE