結果

問題 No.917 Make One With GCD
ユーザー htensaihtensai
提出日時 2019-12-18 15:53:11
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,150 bytes
コンパイル時間 1,980 ms
コンパイル使用メモリ 74,048 KB
実行使用メモリ 55,608 KB
最終ジャッジ日時 2023-09-20 18:11:39
合計ジャッジ時間 6,855 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,120 KB
testcase_01 AC 42 ms
49,212 KB
testcase_02 AC 43 ms
49,204 KB
testcase_03 AC 44 ms
49,600 KB
testcase_04 AC 43 ms
49,076 KB
testcase_05 AC 44 ms
49,444 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    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]);
        }
        System.out.println(getCount(0, arr, -1));
    }
    
    static long getCount(int idx, int[] arr, int value) {
        if (idx >= arr.length) {
            return 0;
        }
        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);
        }
        return count;
    }
    
    static int gcd(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return gcd(y, x % y);
        }
    }
}
0