結果
| 問題 | No.101 ぐるぐる!あみだくじ! |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-13 18:11:38 |
| 言語 | JavaScript (node v23.5.0) |
| 結果 |
AC
|
| 実行時間 | 73 ms / 5,000 ms |
| コード長 | 2,140 bytes |
| コンパイル時間 | 38 ms |
| コンパイル使用メモリ | 5,120 KB |
| 実行使用メモリ | 43,264 KB |
| 最終ジャッジ日時 | 2024-10-13 00:04:58 |
| 合計ジャッジ時間 | 4,059 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
/// <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();