結果

問題 No.1917 LCMST
ユーザー 37zigen37zigen
提出日時 2022-04-30 00:17:24
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,876 bytes
コンパイル時間 3,657 ms
コンパイル使用メモリ 79,568 KB
実行使用メモリ 67,796 KB
最終ジャッジ日時 2023-09-11 16:02:39
合計ジャッジ時間 18,439 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 268 ms
63,564 KB
testcase_01 AC 266 ms
63,564 KB
testcase_02 AC 260 ms
63,808 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 271 ms
63,860 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main implements Runnable { //Runnableを実装する
    public static void main(String[] args) {
//        new Thread(null, new Main(), "", 16 * 1024 * 1024).start(); //16MBスタックを確保して実行
    	new Main().run();
    }
    
    long gcd(long a, long b) {
    	return a==0?b:gcd(b%a, a);
    }
    
    class DSU {
    	int[] parent;
    	public DSU(int n) {
    		parent=new int[n];
    		Arrays.fill(parent, -1);
    	}
    	int root(int x) {
    		return parent[x] < 0 ? x : (parent[x]=root(parent[x]));
    	}
    	boolean equiv(int x, int y) {
    		return root(x)==root(y);
    	}
    	void union(int x, int y) {
    		x=root(x);y=root(y);
    		if (x==y) return;
    		if (parent[x] < parent[y]) {
    			x^=y;y^=x;x^=y;
    		}
    		parent[y]+=parent[x];
    		parent[x]=y;
    	}
    }
    
    final int AMAX=100000;
    
    public void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		long[] A=new long[N];
		ArrayList<Integer>[] list=new ArrayList[AMAX+1];
		ArrayList<Integer>[] rev=new ArrayList[AMAX+1];
		for (int i=0;i<=AMAX;++i) list[i]=new ArrayList<>();
		for (int i=0;i<=AMAX;++i) rev[i]=new ArrayList<>();
		for (int i=0;i<N;++i) A[i]=sc.nextInt();
		for (int i=0;i<N;++i) rev[(int)A[i]].add(i);
		for (int gcd=1;gcd<=AMAX;++gcd) {
			for (int v=1;v*gcd<=AMAX;++v) {
				for (int id : rev[v*gcd]) list[gcd].add(id);
			}
		}
		DSU dsu=new DSU(N);
		long ans=0;
		for (int t=0;t<30;++t) {
			long[][] e=new long[N][3];
			for (int i=0;i<N;++i) for (int j=0;j<3;++j) e[i][j]=-1;
			for (int i=0;i<N;++i) e[i][0]=i;
			for (int gcd=1;gcd<=AMAX;++gcd) {
				int dst=-1;
				for (int i : list[gcd]) {
					if (dst == -1 || A[i] < A[dst]) dst=i;
				}
				for (int i : list[gcd]) {
					if (dsu.equiv(i, dst)) continue;
					int src = dsu.root(i);
					if (e[src][1] == -1 || e[src][1] > A[i] * A[dst] / gcd) {
						e[src][1] = dst;
						e[src][2] = A[i] * A[dst] / gcd;
					}
				}
				if (dst == -1) continue;
				int dst_root=dsu.root(dst);
				for (int i : list[gcd]) {
					if (dsu.equiv(i, dst)) continue;
					if (e[dst_root][1] == -1 || e[dst_root][2] > A[i] * A[dst] / gcd) {
						e[dst_root][1] = i;
						e[dst_root][2] = A[i] * A[dst] / gcd;
					}
				}
				
			}
			Arrays.sort(e, (x, y) -> Long.compare(x[2], y[2]));
			for (int i=0;i<N;++i) {
				int src = (int) e[i][0];
				int dst = (int) e[i][1];
				long cost = e[i][2];
				if (dst == -1) continue;
				if (dsu.equiv(src, dst)) continue;
				ans += cost;
				dsu.union(src, dst);
			}
		}
		System.out.println(ans);
    }
	
	void tr(Object ... o) {System.out.println(Arrays.deepToString(o));}
}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
0