Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-04

LCMRange

| 18:43

問題文

最小公倍数の問題。

class LCMRange {
    public:
        int lcm(int first, int last) {
            int result = first;
            for (int i = first+1; i <= last; i++) {
                result = lcd(result, i);
            }
            return result;
        }
    private:
        int gcd(int c, int d) {
            int v;
            v = c;
            while (v > 0) {
                v = c % d;
                c = d;
                d = v;
            }
            return c;
        }
        int lcd(int c, int d) {
            return c*d / gcd(c,d);
        }
};