結果

問題 No.917 Make One With GCD
ユーザー htensai
提出日時 2019-12-18 15:57:12
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,455 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 78,148 KB
実行使用メモリ 52,388 KB
最終ジャッジ日時 2024-07-06 13:12:43
合計ジャッジ時間 4,849 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20 WA * 12
権限があれば一括ダウンロードができます

ソースコード

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);
        if (value == -1) {
            value = arr[idx];
        } else {
            value = gcd(value, arr[idx]);
        }
        if (value == 1) {
            count += (long)(Math.pow(2, arr.length - idx - 1));
        } else {
            count += getCount(idx + 1, arr, value);
        }
        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