結果

問題 No.917 Make One With GCD
ユーザー tentententen
提出日時 2020-08-27 07:36:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,306 bytes
コンパイル時間 2,505 ms
コンパイル使用メモリ 79,780 KB
実行使用メモリ 54,428 KB
最終ジャッジ日時 2024-04-25 05:23:22
合計ジャッジ時間 8,450 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 129 ms
41,256 KB
testcase_02 AC 130 ms
41,000 KB
testcase_03 AC 129 ms
41,472 KB
testcase_04 AC 123 ms
40,476 KB
testcase_05 WA -
testcase_06 AC 134 ms
39,836 KB
testcase_07 WA -
testcase_08 AC 124 ms
40,600 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 130 ms
41,052 KB
testcase_28 AC 125 ms
40,988 KB
testcase_29 AC 113 ms
40,040 KB
testcase_30 AC 118 ms
39,832 KB
testcase_31 AC 131 ms
41,360 KB
testcase_32 AC 136 ms
41,240 KB
testcase_33 AC 125 ms
41,056 KB
testcase_34 AC 118 ms
40,472 KB
testcase_35 AC 123 ms
41,036 KB
権限があれば一括ダウンロードができます

ソースコード

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