結果

問題 No.14 最小公倍数ソート
ユーザー nebukuro09nebukuro09
提出日時 2018-09-29 02:03:57
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,098 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 161,800 KB
実行使用メモリ 7,084 KB
最終ジャッジ日時 2023-09-03 21:01:34
合計ジャッジ時間 2,988 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!uint.gcdImpl`

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable int MAX = 100001;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    int[][int] P;
    RedBlackTree!(int, "a < b", true)[int] B;

    auto rbt = new RedBlackTree!(int, "a < b", true)();
    foreach (i; 1..N) rbt.insert(A[i]);

    foreach (i; 0..N) {
        if (A[i] in P) continue;
        int x = A[i];
        for (int j = 2; j * j <= A[i]; ++j) {
            if (x % j == 0) P[A[i]] ~= j;
            while (x % j == 0) x /= j;
        }
        if (x > 1) P[A[i]] ~= x;
    }

    foreach (i; 1..N) {
        if (A[i] == 1) continue;
        foreach (p; P[A[i]]) {
            if (p !in B)
                B[p] = new RedBlackTree!(int, "a < b", true)();
            B[p].insert(A[i]);
        }
    }

    int[] ans = [A[0]];
    int cur = A[0];

    foreach (i; 0..N-1) {
        if ((1 in rbt) || cur == 1) {
            int x = rbt.front;
            rbt.removeKey(x);
            if (x in P) foreach (p; P[x]) B[p].removeKey(x);
            ans ~= x;
            cur = x;
            continue;
        }
        int min_lcm = 1 << 29;
        int min_num = -1;
        foreach (p; P[cur]) {
            if (B[p].empty) continue;
            int x = B[p].front;
            int lcm = x * cur / gcd(x, cur);
            if (lcm < min_lcm) {
                min_lcm = lcm;
                min_num = x;
            } else if (lcm == min_lcm && x < min_num) {
                min_num = x;
            }
        }
        int x = rbt.front;
        int lcm = x * cur / gcd(x, cur);
        if (lcm < min_lcm) {
            min_lcm = lcm;
            min_num = x;
        } else if (lcm == min_lcm && x < min_num) {
            min_num = x;
        }

        x = min_num;
        ans ~= x;
        if (x in P) foreach (p; P[x]) B[p].removeKey(x);
        rbt.removeKey(x);
        cur = x;
    }

    ans.map!(to!string).join(" ").writeln;
}
0