TopCoder SRM614: TorusSailingEasy
解法については、 TopCoder SRM614 解説 を参考にしてください。
public class TorusSailingEasy { int reach(int N, int M, int x, int y) { for (int p = x; p < N * M; p += N) if (p % M == y) return p; return -1; } public double expectedTime(int N, int M, int goalX, int goalY) { var r1 = reach(N, M, goalX, goalY); var r2 = reach(N, M, (N - goalX) % N, (M - goalY) % M); if (r1 == -1 || r2 == -1) return -1; return r1 * r2; } }