結果

問題 No.1036 Make One With GCD 2
ユーザー SSRSSSRS
提出日時 2020-04-24 22:45:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,109 ms / 2,000 ms
コード長 1,402 bytes
コンパイル時間 1,528 ms
コンパイル使用メモリ 168,320 KB
実行使用メモリ 15,268 KB
最終ジャッジ日時 2023-10-14 19:30:44
合計ジャッジ時間 20,856 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 686 ms
15,160 KB
testcase_01 AC 394 ms
15,268 KB
testcase_02 AC 107 ms
15,208 KB
testcase_03 AC 99 ms
8,512 KB
testcase_04 AC 187 ms
13,648 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 138 ms
8,708 KB
testcase_08 AC 113 ms
8,508 KB
testcase_09 AC 513 ms
15,132 KB
testcase_10 AC 480 ms
14,652 KB
testcase_11 AC 524 ms
15,116 KB
testcase_12 AC 485 ms
14,672 KB
testcase_13 AC 656 ms
14,880 KB
testcase_14 AC 663 ms
14,852 KB
testcase_15 AC 618 ms
14,704 KB
testcase_16 AC 624 ms
14,668 KB
testcase_17 AC 643 ms
14,816 KB
testcase_18 AC 3 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 611 ms
14,636 KB
testcase_23 AC 449 ms
14,032 KB
testcase_24 AC 636 ms
14,900 KB
testcase_25 AC 580 ms
14,504 KB
testcase_26 AC 605 ms
14,832 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 518 ms
15,116 KB
testcase_39 AC 1,109 ms
15,168 KB
testcase_40 AC 448 ms
13,912 KB
testcase_41 AC 927 ms
15,116 KB
testcase_42 AC 915 ms
15,188 KB
testcase_43 AC 864 ms
15,176 KB
testcase_44 AC 933 ms
15,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<long long> segtree(vector<long long> &A){
	int N = 1;
	while (N < A.size()){
		N = N * 2;
	}
	vector<long long> ST(N * 2 - 1);
	for (int i = 0; i < A.size(); i++){
		ST[i + N - 1] = A[i];
	}
	for (int i = A.size() + N - 1; i < N * 2 - 1; i++){
		ST[i] = 0;
	}
	for (int i = N - 2; i >= 0; i--){
		ST[i] = __gcd(ST[i * 2 + 1], ST[i * 2 + 2]);
	}
	return ST;
}
long long segtree_query(vector<long long> &ST, int x, int y, int i, int L, int R){
	if (y <= L || R <= x){
		return 0;
	} else if (x <= L && R <= y){
		return ST[i];
	} else {
		int M = (L + R) / 2;
		long long A = segtree_query(ST, x, y, i * 2 + 1, L, M);
		long long B = segtree_query(ST, x, y, i * 2 + 2, M, R);
		return __gcd(A, B);
	}
}
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<long long> ST = segtree(A);
  if (ST[0] != 1){
    cout << 0 << endl;
  } else {
    int X = (ST.size() + 1) / 2;
    long long ans = 0;
    int L = 0;
    int R = 0;
    while (1){
      while (R < N){
        if (segtree_query(ST, L, R + 1, 0, 0, X) == 1){
          break;
        }
        R++;
        if (R > N){
          R--;
          break;
        }
      }
      ans += N - R;
      L++;
      if (L >= N){
        break;
      }
    }
    cout << ans << endl;
  }
}
0