結果

問題 No.1917 LCMST
ユーザー 37zigen37zigen
提出日時 2022-04-30 01:27:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,780 ms / 4,000 ms
コード長 5,098 bytes
コンパイル時間 2,608 ms
コンパイル使用メモリ 84,352 KB
実行使用メモリ 137,844 KB
最終ジャッジ日時 2024-10-12 21:24:44
合計ジャッジ時間 95,479 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
53,532 KB
testcase_01 AC 288 ms
53,668 KB
testcase_02 AC 287 ms
53,356 KB
testcase_03 AC 446 ms
56,348 KB
testcase_04 AC 468 ms
56,488 KB
testcase_05 AC 440 ms
56,000 KB
testcase_06 AC 460 ms
56,144 KB
testcase_07 AC 446 ms
55,960 KB
testcase_08 AC 283 ms
54,104 KB
testcase_09 AC 3,780 ms
113,176 KB
testcase_10 AC 3,724 ms
137,844 KB
testcase_11 AC 3,572 ms
112,032 KB
testcase_12 AC 3,405 ms
108,100 KB
testcase_13 AC 2,188 ms
82,492 KB
testcase_14 AC 3,618 ms
113,916 KB
testcase_15 AC 1,544 ms
75,944 KB
testcase_16 AC 2,101 ms
83,796 KB
testcase_17 AC 2,435 ms
96,752 KB
testcase_18 AC 2,839 ms
103,604 KB
testcase_19 AC 1,634 ms
75,596 KB
testcase_20 AC 1,166 ms
74,368 KB
testcase_21 AC 1,673 ms
76,544 KB
testcase_22 AC 1,411 ms
75,644 KB
testcase_23 AC 1,293 ms
75,424 KB
testcase_24 AC 1,682 ms
76,760 KB
testcase_25 AC 1,344 ms
75,940 KB
testcase_26 AC 1,273 ms
75,980 KB
testcase_27 AC 1,467 ms
76,100 KB
testcase_28 AC 1,544 ms
75,292 KB
testcase_29 AC 3,010 ms
129,092 KB
testcase_30 AC 2,881 ms
118,136 KB
testcase_31 AC 3,052 ms
121,588 KB
testcase_32 AC 3,049 ms
128,168 KB
testcase_33 AC 2,997 ms
122,108 KB
testcase_34 AC 414 ms
65,232 KB
testcase_35 AC 435 ms
65,832 KB
testcase_36 AC 493 ms
64,820 KB
testcase_37 AC 3,136 ms
121,240 KB
testcase_38 AC 3,439 ms
126,940 KB
testcase_39 AC 3,229 ms
127,212 KB
testcase_40 AC 3,253 ms
121,540 KB
testcase_41 AC 3,321 ms
124,400 KB
testcase_42 AC 3,262 ms
124,148 KB
testcase_43 AC 763 ms
68,004 KB
testcase_44 AC 713 ms
69,244 KB
権限があれば一括ダウンロードができます

ソースコード

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() {
    	long ans = 0;
		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();
		Arrays.sort(A);
		for (int i = 0; i < N; ++i) {
			int j = i;
			while (j + 1 < N && A[i] == A[j + 1]) ++j;
			ans += A[i] * (j - i);
			i = j;
		}
		A = Arrays.stream(A).distinct().toArray();
		N = A.length;
		for (int i=0;i<N;++i) rev[(int)A[i]].add(i);
		for (int div = 1; div <= AMAX; ++div) {
			for (int v = 1; v * div <= AMAX; ++v) {
				for (int id : rev[v * div]) list[div].add(id);
			}
		}
		DSU dsu = new DSU(N);
		for (int t = 0; t < 30; ++t) {
			long[][] e=new long[N][3];
			for (int i=0;i<N;++i) {
				e[i][0]=i;
				e[i][1]=e[i][2]=-1;
			}
			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][2] > 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