結果

問題 No.1036 Make One With GCD 2
ユーザー carrot46carrot46
提出日時 2020-04-24 22:46:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,205 ms / 2,000 ms
コード長 2,483 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 171,032 KB
実行使用メモリ 22,876 KB
最終ジャッジ日時 2023-10-14 19:31:16
合計ジャッジ時間 23,554 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 788 ms
22,628 KB
testcase_01 AC 380 ms
22,624 KB
testcase_02 AC 654 ms
22,572 KB
testcase_03 AC 60 ms
10,432 KB
testcase_04 AC 106 ms
15,672 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 139 ms
11,360 KB
testcase_08 AC 114 ms
9,744 KB
testcase_09 AC 495 ms
22,100 KB
testcase_10 AC 464 ms
20,788 KB
testcase_11 AC 506 ms
22,760 KB
testcase_12 AC 468 ms
21,080 KB
testcase_13 AC 727 ms
21,704 KB
testcase_14 AC 734 ms
21,848 KB
testcase_15 AC 687 ms
20,628 KB
testcase_16 AC 692 ms
20,872 KB
testcase_17 AC 712 ms
21,188 KB
testcase_18 AC 2 ms
4,356 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 4 ms
4,352 KB
testcase_22 AC 680 ms
20,304 KB
testcase_23 AC 500 ms
15,676 KB
testcase_24 AC 706 ms
21,116 KB
testcase_25 AC 643 ms
19,568 KB
testcase_26 AC 667 ms
20,492 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 788 ms
22,532 KB
testcase_39 AC 1,205 ms
22,480 KB
testcase_40 AC 500 ms
15,692 KB
testcase_41 AC 1,009 ms
22,532 KB
testcase_42 AC 984 ms
22,528 KB
testcase_43 AC 1,004 ms
22,728 KB
testcase_44 AC 1,041 ms
22,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
const ll INF = 1LL<<60;
typedef pair<ll, ll> P;

ll gcd(ll a, ll b){
    if(a < b) swap(a, b);
    if(b < 0) return a;
    ll d = a;
    if(b != 0){
        d = gcd(b, a%b);
    }
    return d;
}
class SEGTREE{
            ll n, dflt;
      		vector<ll> dat;
        public:
        	SEGTREE(unsigned long _n, ll _a) : dat(_n * 4, _a){
        	    n = 1;
        	    dflt = _a;
        	    while(n < _n) n *= 2;
            }
            void update(ll k, ll a){
                k += n;
                dat[k] = a;
                while(k > 0){
                    k /= 2;
                    dat[k] = gcd(dat[k * 2], dat[k * 2 + 1]);
                }
            }
            void init(vector<ll> &v){
        	    for(int i = 0; i < v.size(); ++i) dat[i + n] = v[i];
        	    for(int i = n - 1; i >= 1; --i) {
        	        dat[i] = gcd(dat[i * 2], dat[i * 2 + 1]);
        	    }
        	}
            ll Q(ll a, ll b){
        	    return query(a, b, 1, 0, n);
        	}
            ll query(ll a, ll b, ll k, ll l, ll r){
                if(r <= a || b <= l) return dflt;
                if(a <= l && r <= b) return dat[k];
                ll vl = query(a, b, k * 2, l, (l + r) / 2);
                ll vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
                return gcd(vl, vr);
            }
            //単項にアクセスする[]だが、書き換え厳禁
            //一応後でinitすれば書き換えてもよさそうだが…
            ll & operator [](int k) {return dat[k + n];}
    };
int main() {
    cin>>N;
    vec a(N);
    rep(i,N) cin>>a[i];
    SEGTREE seg(N, -1);
    seg.init(a);
    ll ans = 0, l = 0, r = 1;
    ll now = seg.Q(l, r);
    while(l != N){
        if(now == 1){
            ans += N - r + 1;
            if(++l == r) ++r;
        }else if(r != N){
            ++r;
        }else{
            ++l;
        }
        if(l == N) break;
        now = seg.Q(l, r);
        //cout<<l<<' '<<r<<' '<<ans<<endl;
    }
    cout<<ans<<endl;
    //cout<<seg.Q(1, 3)<<endl;
}
0