結果

問題 No.1036 Make One With GCD 2
ユーザー cardano1016cardano1016
提出日時 2020-04-24 23:16:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 988 ms / 2,000 ms
コード長 3,032 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 103,936 KB
実行使用メモリ 19,264 KB
最終ジャッジ日時 2023-10-14 19:34:58
合計ジャッジ時間 18,442 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 573 ms
19,072 KB
testcase_01 AC 236 ms
19,180 KB
testcase_02 AC 118 ms
19,072 KB
testcase_03 AC 64 ms
10,096 KB
testcase_04 AC 116 ms
16,312 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 93 ms
10,324 KB
testcase_08 AC 78 ms
9,764 KB
testcase_09 AC 417 ms
18,888 KB
testcase_10 AC 388 ms
18,332 KB
testcase_11 AC 424 ms
19,128 KB
testcase_12 AC 390 ms
18,304 KB
testcase_13 AC 619 ms
18,860 KB
testcase_14 AC 627 ms
18,756 KB
testcase_15 AC 588 ms
18,396 KB
testcase_16 AC 592 ms
18,356 KB
testcase_17 AC 610 ms
18,660 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 581 ms
18,048 KB
testcase_23 AC 432 ms
16,536 KB
testcase_24 AC 605 ms
18,684 KB
testcase_25 AC 554 ms
17,812 KB
testcase_26 AC 574 ms
18,008 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 2 ms
4,368 KB
testcase_38 AC 117 ms
19,264 KB
testcase_39 AC 988 ms
19,108 KB
testcase_40 AC 433 ms
16,332 KB
testcase_41 AC 690 ms
19,028 KB
testcase_42 AC 681 ms
19,196 KB
testcase_43 AC 601 ms
19,132 KB
testcase_44 AC 674 ms
19,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e16;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
const ll MAX = 100010;
constexpr ll nx[8] = {1,0,-1,0,-1,-1,1,1};
constexpr ll ny[8] = {0,1,0,-1,-1,1,-1,1};
class SegTree{
    private:
        ll n;
        vector<ll> node;
    public:
        SegTree(vector<ll> v){
            ll sz = v.size();
            n = 1;
            while(n < sz) n *= 2;
            node.resize(2*n+1,INF);
            rep(i,sz) node[i+n-1] = v[i];
            for(int i = n-2;i >= 0;i--) node[i] = gcd(node[2*i+1],node[2*i+2]);
        }
        void update(ll x,ll val){
            x += (n-1);
            node[x] = val;
            while(x > 0){
                x = (x-1)/2;
                node[x] = gcd(node[2*x+1],node[2*x+2]);
            }
        }
        ll query(ll a,ll b,ll k = 0,ll l = 0,ll r = -1){
            if (r < 0) r = n;
            if (r <= a || b <= l) return 0;
            if (a <= l && r <= b) return node[k];
            ll vl = query(a,b,2*k+1,l,(l+r)/2);
            ll vr = query(a,b,2*k+2,(l+r)/2,r);
            if (vl == INF) vl = vr;
            else if (vr == INF) vr = vl;
            return gcd(vl,vr);
        }
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    vector<ll> a(n);
    cins(all(a));
    ll now = a[0],r = 0,ans = 0;
    SegTree t(a);
    rep(l,n){
        if (l >= r) r = l;
        now = t.query(l,r+1);
        //PRINT(now);
        while(r < n && gcd(now,a[r]) != 1){
            now = gcd(now,a[r]);
            r++;
        }
        //cout << l << " " << r << endl;
        if (r == n){
            if (now == 1) ans++;
            break;
        }
        ans += n-r;
    }
    PRINT(ans);
}    
0