結果

問題 No.917 Make One With GCD
ユーザー tenten
提出日時 2020-08-27 07:36:09
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,306 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 79,816 KB
実行使用メモリ 41,852 KB
最終ジャッジ日時 2024-11-07 15:26:01
合計ジャッジ時間 8,921 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 11 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	TreeMap<Integer, Integer> dp = new TreeMap<>();
    	for (int i = 0; i < n; i++) {
    	    int a = sc.nextInt();
    	    HashMap<Integer, Integer> tmp = new HashMap<>();
    	    for (int x : dp.keySet()) {
    	        int gcd = getGCD(a, x);
    	        if (dp.containsKey(gcd)) {
    	            dp.put(gcd, dp.get(gcd) + dp.get(x));
    	        } else {
    	            if (tmp.containsKey(gcd)) {
    	                tmp.put(gcd, tmp.get(gcd) + dp.get(x));
    	            } else {
    	                tmp.put(gcd, dp.get(x));
    	            }
    	        }
    	    }
    	    if (dp.containsKey(a)) {
    	        dp.put(a, dp.get(a) + 1);
    	    } else {
    	        dp.put(a, 1);
    	    }
    	    for (Map.Entry<Integer, Integer> entry : tmp.entrySet()) {
    	        dp.put(entry.getKey(), entry.getValue());
    	    }
    	}
    	if (dp.containsKey(1)) {
    	    System.out.println(dp.get(1));
    	} else {
    	    System.out.println(0);
    	}
    }
    
    static int getGCD(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
}
0