C♯の勉強

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

TopCoder SRM620: PairGameEasy

\((x, y)\) から \((x + y, y)\) または \((x, x + y)\) に変換できる。
ここで \(x + y\) は \(x\) や \(y\) よりも大きいため、要素の大きいほうが、最後に加算された箇所だと分かる。
よって変換後から変換前への状態は一意で決まるため、\((c, d)\) から逆変換していって \((a, b)\) に到達できるかをチェックすればよい。

public class PairGameEasy {
    public string able(int a, int b, int c, int d) {
        if (a == c && b == d)
            return "Able to generate";
        if (a > c || b > d)
            return "Not able to generate";
        if (c < d)
            return able(a, b, c, d - c);
        else
            return able(a, b, c - d, d);
    }
}