結果

問題 No.917 Make One With GCD
ユーザー htensaihtensai
提出日時 2019-12-10 11:56:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 933 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 71,656 KB
実行使用メモリ 60,428 KB
最終ジャッジ日時 2023-09-06 05:39:52
合計ジャッジ時間 7,647 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
59,916 KB
testcase_01 AC 126 ms
56,084 KB
testcase_02 AC 126 ms
56,532 KB
testcase_03 AC 128 ms
55,824 KB
testcase_04 AC 125 ms
55,900 KB
testcase_05 AC 131 ms
55,560 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.*;

public class Main {
    static int n;
    static int[] arr;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(search(0, -1));
  }
   
   static long search(int idx, int value) {
       if (idx >= n) {
           return 0;
       }
       long ans = search(idx + 1, value);
       if (value == -1) {
           value = arr[idx];
       } else {
           value = gcd(value, arr[idx]);
       }
       if (value == 1) {
           ans += (long)(Math.pow(2, n - idx - 1));
       } else {
           ans += search(idx + 1, value);
       }
       return ans;
   }
   
   static int gcd(int x, int y) {
       if (x % y == 0) {
           return y;
       } else {
           return gcd(y, x % y);
       }
   }
}
0