結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-04-24 22:32:16 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,292 ms / 2,000 ms |
コード長 | 1,011 bytes |
コンパイル時間 | 3,026 ms |
コンパイル使用メモリ | 77,296 KB |
実行使用メモリ | 112,672 KB |
最終ジャッジ日時 | 2024-09-16 13:23:02 |
合計ジャッジ時間 | 34,838 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
import java.io.BufferedReader;import java.io.InputStreamReader;import java.math.BigInteger;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[] sa = br.readLine().split(" ");long[] a = new long[n];for (int i = 0; i < n; i++) {a[i] = Long.parseLong(sa[i]);}br.close();long ans = 0;long now = 0;int l = 0;for (int r = 0; r < n; r++) {now = gcd(now, a[r]);if (now == 1) {long now2 = a[r];for (int k = r; k >= l; k--) {long next = gcd(now2, a[k]);if (next == 1) {ans += (long) (n - r) * (k - l + 1);l = k + 1;if (l > r) {now = 0;} else {now = now2;}break;}now2 = next;}}}System.out.println(ans);}static long gcd(long a, long b) {return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).longValue();}}