結果

問題 No.1036 Make One With GCD 2
ユーザー NrKmDevNrKmDev
提出日時 2020-04-25 11:03:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 966 ms / 2,000 ms
コード長 2,042 bytes
コンパイル時間 2,620 ms
コンパイル使用メモリ 221,324 KB
実行使用メモリ 19,288 KB
最終ジャッジ日時 2023-10-14 19:45:13
合計ジャッジ時間 19,106 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 556 ms
19,052 KB
testcase_01 AC 224 ms
19,192 KB
testcase_02 AC 415 ms
18,988 KB
testcase_03 AC 83 ms
10,112 KB
testcase_04 AC 151 ms
16,280 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 97 ms
10,268 KB
testcase_08 AC 79 ms
9,812 KB
testcase_09 AC 352 ms
18,756 KB
testcase_10 AC 326 ms
18,368 KB
testcase_11 AC 355 ms
19,036 KB
testcase_12 AC 329 ms
18,412 KB
testcase_13 AC 494 ms
18,732 KB
testcase_14 AC 498 ms
18,768 KB
testcase_15 AC 468 ms
18,492 KB
testcase_16 AC 473 ms
18,332 KB
testcase_17 AC 488 ms
18,624 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 462 ms
18,388 KB
testcase_23 AC 341 ms
16,196 KB
testcase_24 AC 480 ms
18,448 KB
testcase_25 AC 438 ms
17,896 KB
testcase_26 AC 454 ms
18,048 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,348 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 657 ms
18,984 KB
testcase_39 AC 966 ms
19,116 KB
testcase_40 AC 343 ms
16,256 KB
testcase_41 AC 780 ms
19,048 KB
testcase_42 AC 761 ms
19,288 KB
testcase_43 AC 775 ms
19,112 KB
testcase_44 AC 816 ms
19,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
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;
}
//グリッド:(典型)dp,dfs,bfs,最短経路,その他
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}

struct SegmentTree{
int n;
vector<ll> node;
SegmentTree(vector<ll> v){
int sz=v.size();
n=1;
while(n<sz) n*=2;
node.resize(2*n-1,-1);
for(int i=0;i<sz;i++) node[i+n-1]=v[i];
for(int i=n-2;i>=0;i--){
    if(node[2*i+1]==-1&&node[2*i+2]==-1)node[i]=-1;
    else if(node[2*i+1]==-1&&node[2*i+2]!=-1) node[i]=node[2*i+2];
    else if(node[2*i+1]!=-1&&node[2*i+2]==-1)node[i]=node[2*i+1];
    else 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]=min(node[2*x+1],node[2*x+2]);
}
}
ll getgcd(ll a,ll b,ll k=0,ll l=0,ll r=-1){
if(r<0) r=n;
if(r<=a||b<=l) return -1;
if(a<=l&&r<=b) return node[k];
ll v1=getgcd(a,b,2*k+1,l,(r+l)/2);
ll v2=getgcd(a,b,2*k+2,(r+l)/2,r);
if(v1==-1&&v2!=-1)return v2;
if(v1!=-1&&v2==-1)return v1;
return gcd(v1,v2);
}
};
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;cin>>n;
    vl a(n);rep(i,n)cin>>a[i];
    SegmentTree seg(a);
    ll j=1;
    ll ans=0;
    rep(i,n){
        while(seg.getgcd(i,j)!=1&&j<=n){
            j++;
        }
        if(j==n&&seg.getgcd(i,n)!=1)break;
        ans+=n-j+1;
    }
    cout<<ans<<endl;
    return 0;
}
0