結果
問題 | No.917 Make One With GCD |
ユーザー |
![]() |
提出日時 | 2019-12-18 15:53:11 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,150 bytes |
コンパイル時間 | 2,191 ms |
コンパイル使用メモリ | 77,724 KB |
実行使用メモリ | 59,016 KB |
最終ジャッジ日時 | 2024-07-06 13:06:41 |
合計ジャッジ時間 | 5,941 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 4 |
other | AC * 6 TLE * 1 -- * 25 |
ソースコード
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); } } }