C♯の勉強

C♯4.0 で TopCoder の過去問を解きます。

TopCoder SRM594: FoxAndClassroom

実際にシミューレーションして判定をする。

nm が互いに素かどうかを判定してもよい。

public class FoxAndClassroom {
    public string ableTo(int n, int m) {
        HashSet<int> memo = new HashSet<int>();
        int r = 0, c = 0;
        while (true) {
            r = (r + 1) % n;
            c = (c + 1) % m;
            if (memo.Contains(100 * r + c)) break;
            memo.Add(100 * r + c);
        }

        return memo.Count() == n * m ? "Possible" : "Impossible";
    }
}