結果

問題 No.1036 Make One With GCD 2
ユーザー SSRSSSRS
提出日時 2020-04-24 22:33:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,419 bytes
コンパイル時間 1,501 ms
コンパイル使用メモリ 166,152 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2024-04-24 17:57:00
合計ジャッジ時間 9,703 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
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 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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 memo = -1;
    for (int i = 0; i < N; i++){
      int tv = N;
      int fv = memo;
      while (tv - fv > 1){
        int mv = (tv + fv) / 2;
        if (segtree_query(ST, i, mv + 1, 0, 0, X) == 1){
          tv = mv;
        } else {
          fv = mv;
        }
      }
      ans += N - tv;
      memo = fv - 1;
    }
    cout << ans << endl;
  }
}
0