結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー izuru_matsuura
提出日時 2016-12-14 22:19:36
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,075 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 146,912 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-12 05:32:58
合計ジャッジ時間 2,660 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!uint.gcdImpl`

ソースコード

diff #

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }

void main() {
    auto N = readln.chomp.to!int;
    auto K = readln.chomp.to!int;
    auto X = iota(0, N, 1).array;
    foreach (i; 0 .. K) {
        int x, y; readf("%s %s\n", &x, &y);
        x--; y--;
        swap(X[x], X[y]);
    }

    auto Y = iota(0, N, 1).array;
    void step() {
        auto nY = new int[N];
        foreach (i; 0 .. N) {
            nY[i] = Y[ X[i] ];
        }
        Y = nY;
    }

    int lcm(int a, int b) {
        return a / gcd(a, b) * b;
    }

    int ans = 1;
    auto used = new bool[N];
    for (int t = 1; t <= N; t++) {
        step();
        foreach (i; 0 .. N) {
            if (!used[i] && Y[i] == i) {
                used[i] = true;
                ans = lcm(ans, t);
            }
        }
    }
    writeln(ans);
}
0