結果

問題 No.1036 Make One With GCD 2
ユーザー tanimani364tanimani364
提出日時 2020-04-25 17:49:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,191 ms / 2,000 ms
コード長 2,916 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 204,772 KB
実行使用メモリ 15,308 KB
最終ジャッジ日時 2023-10-14 19:57:56
合計ジャッジ時間 20,010 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 587 ms
15,308 KB
testcase_01 AC 203 ms
15,140 KB
testcase_02 AC 349 ms
15,216 KB
testcase_03 AC 60 ms
8,472 KB
testcase_04 AC 107 ms
13,684 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 82 ms
8,752 KB
testcase_08 AC 66 ms
8,472 KB
testcase_09 AC 379 ms
15,220 KB
testcase_10 AC 378 ms
15,044 KB
testcase_11 AC 414 ms
15,132 KB
testcase_12 AC 380 ms
14,872 KB
testcase_13 AC 607 ms
14,868 KB
testcase_14 AC 616 ms
14,872 KB
testcase_15 AC 551 ms
14,752 KB
testcase_16 AC 561 ms
14,856 KB
testcase_17 AC 569 ms
14,968 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 538 ms
14,688 KB
testcase_23 AC 387 ms
13,896 KB
testcase_24 AC 570 ms
14,872 KB
testcase_25 AC 503 ms
14,472 KB
testcase_26 AC 533 ms
14,696 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 352 ms
15,152 KB
testcase_39 AC 1,191 ms
15,184 KB
testcase_40 AC 398 ms
13,860 KB
testcase_41 AC 794 ms
15,232 KB
testcase_42 AC 803 ms
15,204 KB
testcase_43 AC 755 ms
15,196 KB
testcase_44 AC 780 ms
15,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define rrep(i,a) for(int i=(int)a-1;i>=0;--i)
#define REP(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RREP(i,a,b) for(int i=(int)a-1;i>=b;--i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;
typedef std::vector<long long> vl;
typedef std::vector<std::vector<long long>> vvl;
#define out(x) cout<<x<<"\n";
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}


ll gcd(ll n, ll m) {
    ll tmp;
    while (m!=0) {
        tmp = n % m;
        n = m;
        m = tmp;
    }
    return n;
}
 
ll lcm(ll n, ll m) {
    return abs(n) / gcd(n, m)*abs(m);//gl=xy
}

using namespace std;

template <typename Monoid>
struct Segmenttree{//0-indexed
    using F=function<Monoid(Monoid,Monoid)>;

    int sz;
    vector<Monoid>seg;
    const F f;//2つの区間の要素をマージする二項演算
    const Monoid unit;//単位元
    Segmenttree(int n,const F f,const Monoid &unit):f(f),unit(unit){
        sz=1;
        while(sz<n)sz<<=1;//簡単のため要素数を2のべき乗に
        seg.assign(2*sz,unit);
    }
    void set(int k,const Monoid &x){//k番目の要素にxを代入
        seg[k+sz]=x;
    }
    void build(){//セグメント木の構築 O(N)
        for(int k=sz-1;k>0;k--)seg[k]=f(seg[2*k+0],seg[2*k+1]);
    }
    void update(int k,const Monoid &x){//k番目の要素をxに変更する
        k+=sz;
        seg[k]=x;
        while(k>>=1){
            seg[k]=f(seg[2*k+0],seg[2*k+1]);
        }
    }
    Monoid query(int a, int b) {//区間[a,b)に対して二項演算した結果を返す
        Monoid L = unit, R = unit;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
        if(a & 1) L = f(L, seg[a++]);
        if(b & 1) R = f(seg[--b], R);
            }
        return f(L, R);
    }
 
    Monoid operator[](const int &k) const {//k番目の要素を返す
        return seg[k + sz];
    }
};

void solve()
{
	ll n;
	cin >> n;
	vector<ll> a(n);
	rep(i, n) cin >> a[i];
	auto f = [](ll a, ll b) {
		return gcd(a, b);
	};
	Segmenttree<ll> sg(n,f,0LL);
	rep(i,n)sg.update(i, a[i]);
	ll x = 0;
	ll ans = 0;
	ll pos = 0;
	for (ll i = 0; i < n; ++i)
	{
		x = sg.query(i, pos);
		for (ll j = pos; j < n; ++j)
		{
			x = gcd(x, a[j]);
			if (x == 1)
			{
				ans += n - pos;
				break;
			}
			++pos;
			}
			pos = max(pos, i+1);
	}
		cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(15);
    solve();
    return 0;
}
0