結果

問題 No.917 Make One With GCD
ユーザー tentententen
提出日時 2023-01-10 18:34:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 2,300 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 90,288 KB
実行使用メモリ 51,136 KB
最終ジャッジ日時 2023-08-23 10:16:25
合計ジャッジ時間 7,026 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
51,136 KB
testcase_01 AC 43 ms
49,744 KB
testcase_02 AC 43 ms
49,504 KB
testcase_03 AC 43 ms
49,496 KB
testcase_04 AC 43 ms
49,388 KB
testcase_05 AC 43 ms
49,488 KB
testcase_06 AC 45 ms
49,340 KB
testcase_07 AC 45 ms
49,460 KB
testcase_08 AC 44 ms
49,484 KB
testcase_09 AC 54 ms
50,420 KB
testcase_10 AC 54 ms
51,052 KB
testcase_11 AC 53 ms
50,536 KB
testcase_12 AC 55 ms
50,204 KB
testcase_13 AC 52 ms
50,872 KB
testcase_14 AC 53 ms
51,116 KB
testcase_15 AC 54 ms
50,064 KB
testcase_16 AC 54 ms
51,040 KB
testcase_17 AC 43 ms
49,264 KB
testcase_18 AC 48 ms
49,480 KB
testcase_19 AC 43 ms
49,368 KB
testcase_20 AC 44 ms
49,260 KB
testcase_21 AC 47 ms
49,640 KB
testcase_22 AC 45 ms
49,380 KB
testcase_23 AC 49 ms
50,096 KB
testcase_24 AC 48 ms
49,500 KB
testcase_25 AC 50 ms
50,072 KB
testcase_26 AC 43 ms
49,628 KB
testcase_27 AC 43 ms
49,352 KB
testcase_28 AC 43 ms
49,372 KB
testcase_29 AC 43 ms
47,468 KB
testcase_30 AC 44 ms
49,836 KB
testcase_31 AC 42 ms
49,596 KB
testcase_32 AC 44 ms
49,448 KB
testcase_33 AC 43 ms
49,408 KB
testcase_34 AC 43 ms
49,348 KB
testcase_35 AC 45 ms
49,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        HashMap<Integer, Long> ans = new HashMap<>();
        for (int i = 0; i < n; i++) {
            HashMap<Integer, Long> next = new HashMap<>();
            for (int x : ans.keySet()) {
                int v = getGCD(x, values[i]);
                next.put(v, next.getOrDefault(v, 0L) + ans.get(x));
            }
            next.put(values[i], next.getOrDefault(values[i], 0L) + 1);
            for (int x : next.keySet()) {
                ans.put(x, ans.getOrDefault(x, 0L) + next.get(x));
            }
        }
        System.out.println(ans.getOrDefault(1, 0L));
    }
    
    static int getGCD(int x, int y) {
        if (y == 0) {
            return x;
        } else {
            return getGCD(y, x % y);
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0