結果

問題 No.1036 Make One With GCD 2
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-25 13:36:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,425 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 131,212 KB
実行使用メモリ 20,260 KB
最終ジャッジ日時 2023-10-25 13:36:44
合計ジャッジ時間 12,667 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;
template<class T,T (*op)(T,T),T (*e)()> struct SegmentTree
{
    private:
    int siz;
    vector<T> node;

    public:
    SegmentTree(int n) noexcept
    {
        siz = 1;
        while(siz < n) siz <<= 1;

        node.assign(2*siz-1,e());
    }

    void replace(int pos,T x) noexcept {func_update(pos,x,[](T u,T v){return u = v;});}

    void add(int pos,T x) noexcept {func_update(pos,x,[](T u,T v){return u+v;});}

    void func_update(int pos,T x) noexcept {func_update(pos,x,op);}
    
    void func_update(int pos,T x,T (*func)(T,T)) noexcept
    {
        assert(0 <= pos && pos < siz);
        pos += siz - 1;

        node[pos] = func(node[pos],x);

        while(pos)
        {
            pos = (pos-1)/2;
            node[pos] = op(node[2*pos+1],node[2*pos+2]);
        }
    }

    T operator [](int pos) noexcept
    {
        assert(0 <= pos && pos < siz);
        return node[pos+siz-1];
    }

    T prod(int l,int r) noexcept
    {
        assert(0 <= l && l <= r && r <= siz);

        l += siz-1;
        r += siz-1;

        T Lval = e();
        T Rval = e();

        while(l < r)
        {
            if(!(l & 1)) Lval = op(Lval,node[l++]);
            if(!(r & 1)) Rval = op(node[--r],Rval);

            l >>= 1;
            r >>= 1;
        }

        return op(Lval,Rval);
    }

    T prod() noexcept {return node[0];}
};
long long op(long long a,long long b)
{
	while(b)
	{
		a %= b;
		swap(a,b);
	}
	return a;
}
long long e() {return 0;}
int N;
void solve()
{
	cin >> N;
	SegmentTree<long long,op,e> seg(N);
	for(int i = 0;i < N;i++)
	{
		long long A;
		cin >> A;
		seg.replace(i,A);
	}
	long long ans = (long long)N*(N+1)/2;
	for(int i = 0;i < N;i++)
	{
		if(seg[i] == 1) continue;
		if(seg.prod(i,N) > 1) ans -= N-i;
		else
		{
			int ok = i+1,ng = N;
			while(ng-ok > 1)
			{
				int mid = (ok+ng)/2;
				if(seg.prod(i,mid) > 1) ok = mid;
				else ng = mid;
			}
			ans -= ok-i;
		}
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0