結果

問題 No.14 最小公倍数ソート
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 01:45:09
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,785 bytes
コンパイル時間 2,533 ms
コンパイル使用メモリ 79,204 KB
実行使用メモリ 60,336 KB
最終ジャッジ日時 2023-08-24 19:10:59
合計ジャッジ時間 6,763 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
58,580 KB
testcase_01 AC 168 ms
58,320 KB
testcase_02 AC 164 ms
58,928 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static int gcd(int x,int y){
        while(y!=0){
            int r=x%y;
            x=y;
            y=r;
        }
        return x;
    }
    static final int N=1001;
    static int[][]memo;
    static int gcdMemo(int x,int y){
        while(x>=N&&y!=0){
            int r=x%y;
            x=y;
            y=r;
        }
        return y==0?x:memo[x][y];
    }
    static void init(){
        memo=new int[N][N];
        for(int i=0;i<N;++i)
            for(int j=0;j<N;++j)
                memo[i][j]=gcd(i,j);
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        init();
        int n=sc.nextInt();
        int[]a=sc.nextIntArray(n);
        for(int i=0;i<n-1;++i){
            long min=Long.MAX_VALUE;
            int minind=-1;
            for(int j=i+1;j<n;++j){
                int lcm=a[i]/gcdMemo(a[i],a[j])*a[j];
                long v=(long)lcm<<20|a[j];
                if(min>v){
                    min=v;
                    minind=j;
                }
            }
            if(minind!=i+1){
                int t=a[i+1];
                a[i+1]=a[minind];
                a[minind]=t;
            }
        }
        for(int i=0;i<n;++i)
            out.print(a[i]+(i==n-1?"\n":" "));
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0