結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー TatamoTatamo
提出日時 2016-12-13 18:11:38
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 2,140 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 5,392 KB
実行使用メモリ 43,448 KB
最終ジャッジ日時 2023-08-03 02:00:35
合計ジャッジ時間 5,501 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
42,232 KB
testcase_01 AC 78 ms
42,520 KB
testcase_02 AC 78 ms
42,364 KB
testcase_03 AC 81 ms
43,232 KB
testcase_04 AC 79 ms
42,296 KB
testcase_05 AC 87 ms
43,304 KB
testcase_06 AC 78 ms
42,232 KB
testcase_07 AC 82 ms
43,168 KB
testcase_08 AC 82 ms
43,236 KB
testcase_09 AC 82 ms
43,292 KB
testcase_10 AC 83 ms
43,336 KB
testcase_11 AC 82 ms
43,252 KB
testcase_12 AC 83 ms
43,372 KB
testcase_13 AC 84 ms
43,312 KB
testcase_14 AC 83 ms
43,412 KB
testcase_15 AC 83 ms
43,256 KB
testcase_16 AC 83 ms
43,292 KB
testcase_17 AC 84 ms
43,380 KB
testcase_18 AC 82 ms
43,220 KB
testcase_19 AC 83 ms
43,172 KB
testcase_20 AC 82 ms
43,404 KB
testcase_21 AC 83 ms
43,304 KB
testcase_22 AC 83 ms
43,208 KB
testcase_23 AC 85 ms
43,324 KB
testcase_24 AC 82 ms
43,216 KB
testcase_25 AC 85 ms
43,316 KB
testcase_26 AC 84 ms
43,440 KB
testcase_27 AC 84 ms
43,372 KB
testcase_28 AC 84 ms
43,320 KB
testcase_29 AC 86 ms
43,328 KB
testcase_30 AC 85 ms
43,372 KB
testcase_31 AC 84 ms
43,308 KB
testcase_32 AC 84 ms
43,212 KB
testcase_33 AC 85 ms
43,448 KB
testcase_34 AC 76 ms
42,120 KB
testcase_35 AC 76 ms
42,228 KB
testcase_36 AC 77 ms
42,228 KB
testcase_37 AC 80 ms
43,040 KB
testcase_38 AC 80 ms
43,040 KB
testcase_39 AC 80 ms
43,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/// <reference path="./typings/index.d.ts" />
var Solver = (function () {
    function Solver(n, k, t) {
        this.n = n;
        this.k = k;
        this.t = t;
        this.edge = new Array(n);
    }
    Solver.prototype.gcd = function (x, y, z) {
        if (typeof z !== "undefined") {
            return this.gcd(this.gcd(x, y), z);
        }
        var result = x;
        var k = -1;
        var n = y;
        while (k != 0) {
            k = result % n;
            result = n;
            n = k;
        }
        return result;
    };
    Solver.prototype.lcm = function (x, y, z) {
        if (typeof z !== "undefined") {
            return this.lcm(this.lcm(x, y), z);
        }
        return x * y / this.gcd(x, y);
    };
    Solver.prototype.constructGraph = function () {
        var _loop_1 = function (i_1) {
            var to = i_1;
            this_1.t.forEach(function (v) {
                var l = v[0] - 1;
                var r = v[1] - 1;
                if (to == l) {
                    to = r;
                }
                else if (to == r) {
                    to = l;
                }
            });
            this_1.edge[i_1] = to;
        };
        var this_1 = this;
        for (var i_1 = 0; i_1 < this.n; i_1++) {
            _loop_1(i_1);
        }
    };
    Solver.prototype.solve = function () {
        var _this = this;
        this.constructGraph();
        var cycle = new Array(n);
        for (var i_2 = 0; i_2 < this.n; i_2++) {
            var c = 1;
            var now = this.edge[i_2];
            while (now != i_2) {
                now = this.edge[now];
                c++;
            }
            cycle[i_2] = c;
        }
        var result = cycle.reduce(function (l, r) {
            return _this.lcm(l, r);
        });
        console.log(result);
    };
    return Solver;
}());
var input = require("fs").readFileSync("/dev/stdin", "utf8");
input = input.split("\n");
var n, k;
n = +input.shift();
k = +input.shift();
var t = [];
for (var i = 0; i < k; i++) {
    t.push(input[i].split(" ").map(function (v) { return +v; }));
}
(new Solver(n, k, t)).solve();
0