結果

問題 No.917 Make One With GCD
ユーザー htensaihtensai
提出日時 2019-12-18 15:58:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 76,144 KB
実行使用メモリ 50,924 KB
最終ジャッジ日時 2023-09-06 16:47:20
合計ジャッジ時間 5,343 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,540 KB
testcase_01 AC 44 ms
49,384 KB
testcase_02 AC 43 ms
47,388 KB
testcase_03 AC 45 ms
49,456 KB
testcase_04 AC 45 ms
49,492 KB
testcase_05 AC 45 ms
49,464 KB
testcase_06 AC 46 ms
49,468 KB
testcase_07 AC 45 ms
49,276 KB
testcase_08 AC 45 ms
49,444 KB
testcase_09 AC 54 ms
50,680 KB
testcase_10 AC 54 ms
50,532 KB
testcase_11 AC 53 ms
50,136 KB
testcase_12 AC 54 ms
50,920 KB
testcase_13 AC 53 ms
50,924 KB
testcase_14 AC 53 ms
50,172 KB
testcase_15 AC 53 ms
50,548 KB
testcase_16 AC 53 ms
50,020 KB
testcase_17 AC 45 ms
49,368 KB
testcase_18 AC 46 ms
49,376 KB
testcase_19 AC 43 ms
49,272 KB
testcase_20 AC 44 ms
50,020 KB
testcase_21 AC 45 ms
49,488 KB
testcase_22 AC 44 ms
49,584 KB
testcase_23 AC 50 ms
50,396 KB
testcase_24 AC 46 ms
49,852 KB
testcase_25 AC 50 ms
50,508 KB
testcase_26 AC 44 ms
49,296 KB
testcase_27 AC 44 ms
49,368 KB
testcase_28 AC 44 ms
49,376 KB
testcase_29 AC 43 ms
49,380 KB
testcase_30 AC 44 ms
49,368 KB
testcase_31 AC 44 ms
49,364 KB
testcase_32 AC 43 ms
49,352 KB
testcase_33 AC 44 ms
49,360 KB
testcase_34 AC 43 ms
49,336 KB
testcase_35 AC 43 ms
47,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static HashMap<Integer, Long>[] maps;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] first = br.readLine().split(" ", n);
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(first[i]);
        }
        maps = new HashMap[n];
        for (int i = 0; i < n; i++) {
            maps[i] = new HashMap<Integer, Long>();
        }
        System.out.println(getCount(0, arr, -1));
    }
    
    static long getCount(int idx, int[] arr, int value) {
        if (idx >= arr.length) {
            return 0;
        }
        if (maps[idx].containsKey(value)) {
            return maps[idx].get(value);
        }
        long count = getCount(idx + 1, arr, value);
        int next;
        if (value == -1) {
            next = arr[idx];
        } else {
            next = gcd(value, arr[idx]);
        }
        if (next == 1) {
            count += (long)(Math.pow(2, arr.length - idx - 1));
        } else {
            count += getCount(idx + 1, arr, next);
        }
        maps[idx].put(value, count);
        return count;
    }
    
    static int gcd(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return gcd(y, x % y);
        }
    }
}
0