結果

問題 No.1036 Make One With GCD 2
ユーザー momoharamomohara
提出日時 2020-04-24 22:50:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,552 ms / 2,000 ms
コード長 2,963 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 173,992 KB
実行使用メモリ 19,176 KB
最終ジャッジ日時 2023-10-14 19:31:57
合計ジャッジ時間 19,147 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 737 ms
18,936 KB
testcase_01 AC 114 ms
19,080 KB
testcase_02 AC 313 ms
19,076 KB
testcase_03 AC 23 ms
10,116 KB
testcase_04 AC 40 ms
16,316 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 40 ms
10,328 KB
testcase_08 AC 34 ms
9,852 KB
testcase_09 AC 281 ms
18,828 KB
testcase_10 AC 265 ms
18,280 KB
testcase_11 AC 289 ms
19,176 KB
testcase_12 AC 268 ms
18,212 KB
testcase_13 AC 466 ms
18,776 KB
testcase_14 AC 468 ms
18,784 KB
testcase_15 AC 440 ms
18,400 KB
testcase_16 AC 443 ms
18,448 KB
testcase_17 AC 452 ms
18,512 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 429 ms
18,296 KB
testcase_23 AC 315 ms
16,204 KB
testcase_24 AC 448 ms
18,412 KB
testcase_25 AC 404 ms
17,684 KB
testcase_26 AC 421 ms
18,064 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,356 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 496 ms
19,068 KB
testcase_39 AC 1,552 ms
19,072 KB
testcase_40 AC 315 ms
16,316 KB
testcase_41 AC 1,035 ms
19,148 KB
testcase_42 AC 1,025 ms
19,008 KB
testcase_43 AC 962 ms
19,128 KB
testcase_44 AC 1,041 ms
19,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);i--)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
//constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;


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;
}

struct Edge {
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
	to = _to; cap = _cap; rev = _rev;
	}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}


template<typename T, typename F>
class SegmentTree {
private:
	T e;
	F op;
	ll n;
	vector<T> dat;
public:
	SegmentTree(F f, T _e, vector<T> v) :op(f), e(_e) {
		int _n = v.size();
		n = 1;
		while (n < _n)n *= 2;
		dat.resize(2 * n - 1, e);
		REP(i, _n)dat[n + i - 1] = v[i];
		for (int i = n - 2; i >= 0; i--)dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	SegmentTree(F f, T _e, int _n) :op(f), e(_e) {
		n = 1;
		while (n < _n)n *= 2;
		dat.resize(2 * n - 1, e);
		for (int i = n - 2; i >= 0; i--)dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	void update(int i, T x) {
		i += n - 1;
		dat[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	T query(int l, int r) {
		//[l,r)でのクエリ処理
		T left = e, right = e;
		l += n - 1; r += n - 1;
		while (l < r) {
			if ((l & 1) == 0)left = op(left, dat[l]);
			if ((r & 1) == 0)right = op(dat[r - 1], right);
			l = l / 2;
			r = (r - 1) / 2;
		}
		return op(left, right);
	}
};


//最大公約数
ll gcd(ll m, ll n) {
 if (n == 0)return m;
 return gcd(n, m % n);
}//gcd



void solve(){
    ll n;
    cin>>n;
    Array a(n);
    REP(i,n) cin>>a[i];

    auto op = [](ll a, ll b) {return gcd(a,b); };
    SegmentTree<ll, decltype(op)> seg(op,0,a);

    ll r=1;
    ll ans=0;
    REP(l,n){
        if(l==r) r++;
        while(r<n && seg.query(l,r)!=1) r++;
        if(seg.query(l,r)!=1) continue;
        ans += n-r+1;
    }
    cout<<ans<<en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();
    //ll t;cin>>t;REP(i,t) solve();

    return 0;
}
0