結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
merom686
|
| 提出日時 | 2020-04-25 01:21:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,028 ms / 2,000 ms |
| コード長 | 1,614 bytes |
| コンパイル時間 | 1,411 ms |
| コンパイル使用メモリ | 92,724 KB |
| 最終ジャッジ日時 | 2025-01-10 00:49:37 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;
ll gcd(ll n, ll m) {
if (n < m) swap(n, m);
while (m != 0) {
ll r = n % m;
n = m;
m = r;
}
return n;
}
template <class T>
struct SegmentTree {
SegmentTree(int n) : n(n) {
for (m = 2; m < n; m *= 2);
t = vector<T>(m + n + n % 2);
}
T &operator[](int i) {
return t[m + i];
}
const T &root() const {
return t[1];
}
void build() {
for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
}
void update(int i, const T &o) {
for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
}
T query(int l, int r) {
T o0, o1;
for (l += m, r += m; l < r; l /= 2, r /= 2) {
if (l & 1) o0 = o0 * t[l++];
if (r & 1) o1 = t[--r] * o1;
}
return o0 * o1;
}
vector<T> t;
int n, m;
};
struct Node {
constexpr Node() : v(0) {}
Node(ll v) : v(v) {}
Node operator*(const Node &o) const {
return gcd(v, o.v);
}
ll v;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
SegmentTree<Node> st(n);
for (int i = 0; i < n; i++) {
cin >> st[i].v;
}
st.build();
ll r = 0;
for (int i0 = 0, i1 = 0; i0 < n; i0++) {
while (i1 <= n && st.query(i0, i1).v != 1) i1++;
r += n - i1 + 1;
}
cout << r << endl;
return 0;
}
merom686