結果

問題 No.917 Make One With GCD
ユーザー tentententen
提出日時 2020-08-27 07:37:47
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 79,408 KB
実行使用メモリ 41,724 KB
最終ジャッジ日時 2024-04-25 05:23:32
合計ジャッジ時間 9,126 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
41,420 KB
testcase_01 AC 116 ms
39,688 KB
testcase_02 AC 131 ms
41,336 KB
testcase_03 AC 119 ms
40,432 KB
testcase_04 AC 116 ms
40,208 KB
testcase_05 AC 134 ms
41,260 KB
testcase_06 AC 137 ms
41,192 KB
testcase_07 WA -
testcase_08 AC 135 ms
40,924 KB
testcase_09 AC 152 ms
41,588 KB
testcase_10 AC 153 ms
41,596 KB
testcase_11 AC 152 ms
41,312 KB
testcase_12 AC 136 ms
40,336 KB
testcase_13 AC 129 ms
40,496 KB
testcase_14 AC 150 ms
41,508 KB
testcase_15 AC 135 ms
40,512 KB
testcase_16 AC 147 ms
41,336 KB
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,136 KB
testcase_28 AC 130 ms
41,148 KB
testcase_29 AC 130 ms
40,880 KB
testcase_30 AC 120 ms
40,012 KB
testcase_31 AC 116 ms
40,284 KB
testcase_32 AC 124 ms
41,064 KB
testcase_33 AC 128 ms
41,172 KB
testcase_34 AC 126 ms
41,252 KB
testcase_35 AC 130 ms
40,940 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, Long> dp = new TreeMap<>();
    	for (int i = 0; i < n; i++) {
    	    int a = sc.nextInt();
    	    HashMap<Integer, Long> 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, 1L);
    	    }
    	    for (Map.Entry<Integer, Long> 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