ADDED SRM/567-U/1A-U.cpp Index: SRM/567-U/1A-U.cpp ================================================================== --- SRM/567-U/1A-U.cpp +++ SRM/567-U/1A-U.cpp @@ -0,0 +1,117 @@ +#include <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex<LD> CMP; + +class TheSquareRootDilemma { public: + int countPairs(int N, int M) + { + static const int X = 279; + vector<bool> isp(X+1, true); + vector<int> ps; + for(int p=2; p<=X; ++p) + if( isp[p] ) { + ps.push_back(p); + for(int q=p+p; q<=X; q+=p) + isp[q] = false; + } + + map<vector<int>, int> AA; + for(int A=1; A<=N; ++A) + { + int x = A; + vector<int> v; + for(int i=0; i<ps.size(); ++i) { + int p = ps[i]; + int k = 0; + while(x%p==0) {k++; x/=p;} + v.push_back(k&1); + } + if(x==1) + AA[v]++; + } + + int result = 0; + for(int B=1; B<=M; ++B) + { + int x = B; + vector<int> v; + for(int i=0; i<ps.size(); ++i) { + int p = ps[i]; + int k = 0; + while(x%p==0) {k++; x/=p;} + v.push_back(k&1); + } + if(x==1) + result += AA[v]; + } + return result; + } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::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(_, TheSquareRootDilemma().countPairs(N, M));} +int main(){ + +CASE(0) + int N = 2; + int M = 2; + int _ = 2; +END +CASE(1) + int N = 10; + int M = 1; + int _ = 3; +END +CASE(2) + int N = 3; + int M = 8; + int _ = 5; +END +CASE(3) + int N = 100; + int M = 100; + int _ = 310; +END +CASE(4) + int N = 77777; + int M = 77777; + int _ = -1; +END +/* +CASE(5) + int N = ; + int M = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/567-U/1B.cpp Index: SRM/567-U/1B.cpp ================================================================== --- SRM/567-U/1B.cpp +++ SRM/567-U/1B.cpp @@ -0,0 +1,132 @@ +#include <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex<LD> CMP; + +class StringGame { public: + vector <int> getWinningStrings(vector <string> S) + { + vector< vector<int> > cb; + for(int i=0; i<S.size(); ++i) + cb.push_back(as_cb(S[i])); + + vector<int> result; + for(int i=0; i<cb.size(); ++i) + if( can_win(cb, i) ) + result.push_back(i); + return result; + } + + vector<int> as_cb(const string& s) + { + vector<int> cb(26); + for(int i=0; i<s.size(); ++i) + cb[s[i]-'a']++; + return cb; + } + + bool can_win(const vector< vector<int> >& cb, int I) + { + vector<int> enemy; + for(int i=0; i<cb.size(); ++i) + if(i != I) + enemy.push_back(i); + + while(!enemy.empty()) + { + bool update = false; + for(int c=0; c<26; ++c) + { + vector<int> win, draw; + for(int i=0; i<enemy.size(); ++i) + { + int e = enemy[i]; + if(cb[e][c] < cb[I][c]) + win.push_back(e); + else if(cb[e][c] == cb[I][c]) + draw.push_back(e); + } + if(!win.empty() && win.size() + draw.size() == enemy.size()) { + enemy = draw; + update = true; + } + } + if(!update) + return false; + } + + return true; + } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector <int>& Expected, const vector <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(_, StringGame().getWinningStrings(S));} +int main(){ + +CASE(0) + string S_[] = {"a", "b", "c", "d"}; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + int __[] = {0, 1, 2, 3 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + string S_[] = {"aabbcc", "aaabbb", "aaaccc"}; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + int __[] = {1, 2 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + string S_[] = {"ab", "ba"}; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + vector <int> _; +END +CASE(3) + string S_[] = {"xaocxsss", "oooxsoas", "xaooosss", "xaocssss", "coxaosxx"}; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + int __[] = {1, 3, 4 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) +string S_[] = {"aaabbc", "aabbcc", "aaaabc"}; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + int __[] = {0, 1, 2}; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + string S_[] = ; + vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); + int __[] = ; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/567-U/1C.cpp Index: SRM/567-U/1C.cpp ================================================================== --- SRM/567-U/1C.cpp +++ SRM/567-U/1C.cpp @@ -0,0 +1,176 @@ +#include <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex<LD> CMP; + +static const unsigned MODVAL = 1000000009; + +class Mountains { public: + int countPlacements(vector <int> heights, vector <string> visibility) + { + int W = visibility[0].size(); + int N = visibility.size(); + memo.resize(N); + + vector<int> view(W, 0); + return rec(W, view, heights.size()-1, heights, visibility); + } + + vector< map<vector<int>,int> > memo; + int rec(int W, const vector<int>& view, int i, vector<int>& H, const vector<string>& V) + { + if(i < 0) + return 1; + + if(memo[i].count(view)) + return memo[i][view]; + + int mustL=0, mustR=W-1; + int mustNotL=W, mustNotR=-1; + for(int x=0; x<W; ++x) + { + int LLL = x - (H[i]-view[x]-1); + int RRR = x + (H[i]-view[x]-1); + if(V[i][x] == 'X') { + if( H[i] <= view[x] ) + return 0; + mustL = max(mustL, LLL); + mustR = min(mustR, RRR); + } else { +// if( H[i] > view[x] ) { +// mustNotL = min(mustNotL, LLL); +// mustNotR = max(mustNotR, RRR); +// } + } + } + + LL result = 0; + for(int c=mustL; c<=mustR; ++c) if(c<mustNotL || mustNotR<c) + { + bool good = true; + for(int x=0; x<W && good; ++x) { + int my_h = H[i] - abs(x-c); + if((V[i][x]=='X') != (view[x] < my_h)) + good = false; + } + + if( good ) { + vector<int> view2(W); + for(int x=0; x<W; ++x) { + int my_h = H[i] - abs(x-c); + view2[x] = max(my_h, view[x]); + } + result += rec(W, view2, i-1, H, V); + } + } + return memo[i][view] = int(result % MODVAL); + } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::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(_, Mountains().countPlacements(heights, visibility));} +int main(){ + +CASE(0) + int heights_[] = {2, 3, 2}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"------", + "XXXX--", + "---XXX"}; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = 4; +END +CASE(1) + int heights_[] = {4, 3, 4}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"XXXXX--------", + "----------XXX", + "----XXXXXXX--"}; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = 4; +END +CASE(2) + int heights_[] = {13, 2, 3, 2}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"XXXXXXXXX", + "-XXX-----", + "----XXXXX", + "-----XXX-"}; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = 9; +END +CASE(3) + int heights_[] = {8, 2, 9, 3, 10}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"X------", + "-------", + "------X", + "-------", + "XXXXXXX"}; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = 98; +END +CASE(4) + int heights_[] = {20, 20, 20, 20, 20, 20, 45, 50, 49, 50}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"-------------------", + "-------------------", + "-------------------", + "-------------------", + "-------------------", + "-------------------", + "-------------------", + "------------XXXXXXX", + "XXXXXXX------------", + "XXXXXXXXXXXXXXXXXXX"} +; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = 973726691; +END +CASE(5) + int heights_[] = {20, 20, 20, 20, 20, 20, 45, 50, 49, 50}; + vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + string visibility_[] = {"-------------------------------------------------", + "-------------------------------------------------", + "-------------------------------------------------", + "-------------------------------------------------", + "-------------------------------------------------", + "-------------------------------------------------", + "-------------------------------------------------", + "------------XXXXXXX------------------------------", + "XXXXXXX------------------------------------------", + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"} +; + vector <string> visibility(visibility_, visibility_+sizeof(visibility_)/sizeof(*visibility_)); + int _ = -1; +END +} +// END CUT HERE