ADDED SRM/TCO20-3B-U/1A.cpp Index: SRM/TCO20-3B-U/1A.cpp ================================================================== --- SRM/TCO20-3B-U/1A.cpp +++ SRM/TCO20-3B-U/1A.cpp @@ -0,0 +1,179 @@ +#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 VisitALot { public: + string travel(int R, int C, vector obsr, vector obsc) + { + if (R >= 6) { + string ans; + int r = 0, c = 0; + while (r < R) { + if (count(obsr.begin(), obsr.end(), r) == 0) { + if (c == 0) + while (c + 1 < C) { + ans += 'E'; + c++; + } + else + while (c - 1 >= 0) { + ans += 'W'; + c--; + } + } + + if (r + 1 < R) + ans += 'S'; + r++; + } + return ans; + } + + if (C >= 6) { + string ans; + int r = 0, c = 0; + while (c < C) { + if (count(obsc.begin(), obsc.end(), c) == 0) { + if (r == 0) + while (r + 1 < R) { + ans += 'S'; + r++; + } + else + while (r - 1 >= 0) { + ans += 'N'; + r--; + } + } + + if (c + 1 < C) + ans += 'E'; + c++; + } + return ans; + } + + // naive search + vector> visited(R, vector(C)); + for (int i = 0; i < obsr.size(); ++i) + visited[obsr[i]][obsc[i]] = true; + visited[0][0] = true; + string ans; + dfs(ans, R, C, 0, 0, visited); + return ans; + } + + bool dfs(string& s, int R, int C, int r, int c, vector>& visited) { + int dr[] = { -1,+1,0,0 }; + int dc[] = { 0,0,-1,+1 }; + char dd[] = { 'N', 'S', 'W', 'E' }; + for (int i = 0; i < 4; ++i) { + int rr = r + dr[i]; + int cc = c + dc[i]; + if (0 <= rr && rr < R && 0 <= cc && cc < C && !visited[rr][cc]) { + visited[rr][cc] = true; + s += dd[i]; + if (s.size()+1 >= (R*C+1)/2 || dfs(s, R, C, rr, cc, visited)) + return true; + s.resize(s.size() - 1); + visited[rr][cc] = false; + } + } + return false; + } +}; + +// 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(_, VisitALot().travel(R, C, obsr, obsc));} +int main(){ + +CASE(0) + int R = 2; + int C = 3; + vector obsr; + vector obsc; + string _ = "SENE"; +END +CASE(1) + int R = 6; + int C = 5; + int obsr_[] = {1, 1, 4}; + vector obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); + int obsc_[] = {1, 3, 1}; + vector obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); + string _ = "SSEESWWSSEENEE"; +END +CASE(2) + int R = 7; + int C = 8; + vector obsr; + vector obsc; + string _ = "EEEEEEESWWWWWWWSEEEEEEESWWWWWWWSEEEEEEESWWWWWWWSEEEEEEE"; +END +CASE(3) + int R = 7; + int C = 4; + int obsr_[] = { 5,2,1 }; + vector obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); + int obsc_[] = { 1,2,2 }; + vector obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); + string _ = "?"; +END +CASE(4) + int R = 50; + int C = 50; + int obsr_[] = { 1,10,15 }; + vector obsr(obsr_, obsr_+sizeof(obsr_)/sizeof(*obsr_)); + int obsc_[] = { 28, 17, 3 }; + vector obsc(obsc_, obsc_+sizeof(obsc_)/sizeof(*obsc_)); + string _ = "?"; +END +CASE(4) +int R = 5; +int C = 5; +int obsr_[] = { 1,2,3 }; +vector obsr(obsr_, obsr_ + sizeof(obsr_) / sizeof(*obsr_)); +int obsc_[] = { 1,2,3 }; +vector obsc(obsc_, obsc_ + sizeof(obsc_) / sizeof(*obsc_)); +string _ = "?"; +END +CASE(4) +int R = 4; +int C = 4; +int obsr_[] = { 1,2 }; +vector obsr(obsr_, obsr_ + sizeof(obsr_) / sizeof(*obsr_)); +int obsc_[] = { 1,2 }; +vector obsc(obsc_, obsc_ + sizeof(obsc_) / sizeof(*obsc_)); +string _ = "?"; +END +} +// END CUT HERE ADDED SRM/TCO20-3B-U/1B.cpp Index: SRM/TCO20-3B-U/1B.cpp ================================================================== --- SRM/TCO20-3B-U/1B.cpp +++ SRM/TCO20-3B-U/1B.cpp @@ -0,0 +1,252 @@ +#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; + +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; } + +class ShortBugPaths { public: + int count(int N, vector D, int J) + { + if (N <= 200) { + vector> c(N, vector(N, 1)); + vector> c2(N, vector(N, 1)); + for (int _ = 0; _ < J; ++_) { + for (int y = 0; y < N; ++y) + for (int x = 0; x < N; ++x) { + LL sum = 0; + for (int DD : D) { + int yy = y - DD, xx = x; + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy> c(201, vector(201, 1)); + vector> c2(201, vector(201, 1)); + for (int _ = 0; _ < J; ++_) { + for (int y = 0; y < 101; ++y) + for (int x = 0; x < 101; ++x) { + LL sum = 0; + for (int DD : D) { + int yy = y - DD, xx = x; + + if (y == 100 && x == 100) { + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][100]; + yy++, xx++; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][100]; + yy++, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][100]; + yy--, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][100]; + yy--, xx++; + } + } + else if (y == 100) { + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][min(100,xx)]; + yy++, xx++; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][min(100, xx)]; + yy++, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][min(100, xx)]; + yy--, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[100][min(100, xx)]; + yy--, xx++; + } + } + else if (x == 100) { + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][100]; + yy++, xx++; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][100]; + yy++, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][100]; + yy--, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][100]; + yy--, xx++; + } + } + else { + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][min(100, xx)]; + yy++, xx++; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][min(100, xx)]; + yy++, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][min(100, xx)]; + yy--, xx--; + } + for (int d = 0; d < DD; ++d) { + if (0 <= yy&&yy < N && 0 <= xx&&xx < N) sum += c[min(100, yy)][min(100, xx)]; + yy--, xx++; + } + } + + if (DD == 0) + sum += c[y][x]; + } + c2[y][x] = sum % MODVAL; + } + for (int y = 0; y < 201; ++y) + for (int x = 0; x < 201; ++x) + c2[y][x] = c2[y>100 ? 200-y : y][x>100 ? 200-x : x]; + c.swap(c2); + } + mint ans = 0; + for (int y = 0; y < 201; ++y) + for (int x = 0; x < 201; ++x) + if (y == 100 && x == 100) + ans += mint(c[y][x]) * (N - 200) * (N - 200); + else if (y == 100 || x == 100) + ans += mint(c[y][x]) * (N - 200); + else + ans += c[y][x]; + return ans.val; + } + } +}; + +// 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 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(_, ShortBugPaths().count(N, D, J));} +int main(){ + +CASE(0) + int N = 3; + int D_[] = {1}; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 1; + int _ = 24; +END +CASE(1) + int N = 123456789; + int D_[] = {0}; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 3; + int _ = 643499475; +END +CASE(2) + int N = 5; + int D_[] = {0, 10, 2}; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 4; + int _ = 38281; +END +CASE(3) + int N = 1000; + int D_[] = {0,1}; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 1; + int _ = 4996000; +END +CASE(4) + int N = 47; + int D_[] = {3}; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 6; + int _ = 195354165; +END +CASE(5) + int N = 200; + int D_[] = { 0,1,2,3,4,5,6,7,8,9,10 }; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 10; + int _ = 316860289; +END +CASE(6) + int N = 1000000000; + int D_[] = { 0,1,2,3,4,5,6,7,8,9,10 }; + vector D(D_, D_+sizeof(D_)/sizeof(*D_)); + int J = 10; + int _ = -100; +END +} +// END CUT HERE