結果

問題 No.14 最小公倍数ソート
ユーザー maimai
提出日時 2017-09-24 22:46:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 4,831 bytes
コンパイル時間 3,550 ms
コンパイル使用メモリ 226,632 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-27 02:29:51
合計ジャッジ時間 4,676 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,948 KB
testcase_03 AC 6 ms
6,944 KB
testcase_04 AC 54 ms
9,344 KB
testcase_05 AC 28 ms
7,168 KB
testcase_06 AC 31 ms
7,424 KB
testcase_07 AC 35 ms
7,936 KB
testcase_08 AC 42 ms
8,576 KB
testcase_09 AC 51 ms
9,088 KB
testcase_10 AC 49 ms
8,960 KB
testcase_11 AC 56 ms
9,216 KB
testcase_12 AC 51 ms
9,216 KB
testcase_13 AC 51 ms
9,216 KB
testcase_14 AC 51 ms
9,216 KB
testcase_15 AC 53 ms
9,344 KB
testcase_16 AC 30 ms
7,424 KB
testcase_17 AC 25 ms
6,944 KB
testcase_18 AC 16 ms
6,944 KB
testcase_19 AC 40 ms
8,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include "bits/stdc++.h" // define macro "/D__MAI"

using namespace std;
typedef long long int ll;

#define xprintf(fmt,...) fprintf(stderr,fmt,__VA_ARGS__)
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(auto cnt=0ll;cnt<(l);++cnt)
#define iterate(cnt,b,e) for(auto cnt=(b);cnt!=(e);++cnt)
#define MD 1000000007ll
#define PI 3.1415926535897932384626433832795
#define EPS 1e-12
template<typename T1, typename T2> ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << "(" << p.first << ":" << p.second << ")"; return o; }
template<typename iterator> inline size_t argmin(iterator begin, iterator end) { return distance(begin, min_element(begin, end)); }
template<typename iterator> inline size_t argmax(iterator begin, iterator end) { return distance(begin, max_element(begin, end)); }
template<typename T> T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> T& minset(T& to, const T& val) { return to = min(to, val); }

mt19937_64 randdev(8901016);
inline ll rand_range(ll l, ll h) {
    return uniform_int_distribution<ll>(l, h)(randdev);
}

#ifdef __MAI
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
#ifdef __VSCC
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) {
            var = 0;
            T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc&&cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var*sign;
        }
        inline int c() { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) {
            input_integer<int>(var);
            return *this;
        }
        inline MaiScanner& operator>>(long long& var) {
            input_integer<long long>(var);
            return *this;
        }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> void in(IT begin, IT end) {
            for (auto it = begin; it != end; ++it) *this >> *it;
        }
    };
}
MaiScanner scanner;



template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lcm(T a, T b) { return a * b / gcd(a, b); }

ll m, n, kei;

int aa[10010];
vector<int> divi[10010];

set<pair<int, int>> ss[10010];

int main() {

    // i番目を固定し,それ以降で最小公倍数ソートをすることを考える.

    // i+1以降で再びソートされるので,実質選択ソート.

    // やりたい事は,aa[i+1..N]から最小公倍数が最小の値を高速に求めたい.

    // あらかじめ,全ての要素を素因数分解しておくと,最小公倍数を求めやすい?

    // --
    // 分からん.kmjpさんの解説を見る.

    // あらかじめ約数列挙をしておく.
    // 同じ約数を持つもの同士を調べるようにすると,オーダーを減らせる.

    // O(N^1.5logN)くらい?


    scanner >> n;

    repeat(i, n) {
        int a;
        scanner >> a;
        aa[i] = a;

        for (int d = 1; d*d <= a; ++d) {
            if (a%d) continue;

            divi[i].push_back(d);
            if (d*d != a) divi[i].push_back(a / d);

            ss[d].emplace(a, i);
            ss[a/d].emplace(a, i);
        }
    }

    int ptr = 0;

    repeat(i, n - 1) {
        int a = aa[ptr];
        printf("%d ", a);

        int nxt = -1e9;
        int nxtval = 1.5e9;
        for (auto d : divi[ptr]) { // 約数列挙
            ss[d].erase(make_pair(a, ptr));
            if (ss[d].empty()) continue;

            auto p = *ss[d].begin(); // dを約数として持つものの中で最小
            int l = lcm(p.first, a);
            if (l < nxtval || (nxtval == l && p.first < aa[nxt])) {
                nxt = p.second;
                nxtval = l;
            }
        }
        ptr = nxt;
    }

    cout << aa[ptr] << endl;


    return 0;
}
0