結果

問題 No.1036 Make One With GCD 2
ユーザー beetbeet
提出日時 2020-04-25 11:46:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,305 ms / 2,000 ms
コード長 2,676 bytes
コンパイル時間 2,766 ms
コンパイル使用メモリ 206,612 KB
実行使用メモリ 85,288 KB
最終ジャッジ日時 2024-04-24 19:19:28
合計ジャッジ時間 29,530 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 704 ms
85,176 KB
testcase_01 AC 693 ms
85,120 KB
testcase_02 AC 210 ms
85,248 KB
testcase_03 AC 49 ms
32,128 KB
testcase_04 AC 87 ms
56,576 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 243 ms
36,480 KB
testcase_08 AC 194 ms
30,464 KB
testcase_09 AC 885 ms
83,368 KB
testcase_10 AC 834 ms
77,824 KB
testcase_11 AC 892 ms
84,992 KB
testcase_12 AC 889 ms
78,464 KB
testcase_13 AC 1,287 ms
81,280 KB
testcase_14 AC 1,305 ms
82,180 KB
testcase_15 AC 1,206 ms
77,376 KB
testcase_16 AC 1,233 ms
77,812 KB
testcase_17 AC 1,260 ms
80,216 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 1,197 ms
76,416 KB
testcase_23 AC 890 ms
56,920 KB
testcase_24 AC 1,246 ms
79,260 KB
testcase_25 AC 1,154 ms
72,576 KB
testcase_26 AC 1,216 ms
75,148 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 210 ms
85,248 KB
testcase_39 AC 828 ms
85,248 KB
testcase_40 AC 891 ms
56,832 KB
testcase_41 AC 966 ms
85,276 KB
testcase_42 AC 969 ms
85,244 KB
testcase_43 AC 826 ms
85,288 KB
testcase_44 AC 925 ms
85,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/4072"

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

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
struct DisjointSparseTable{
  using F = function<T(T, T)>;
  vector< vector<T> > dat;
  vector<int> ht;
  const F f;

  DisjointSparseTable(){}
  DisjointSparseTable(F f):f(f){}

  void build(const vector<T> &vs){
    int n=vs.size(),h=1;
    while((1<<h)<=n) h++;
    dat.assign(h,vector<T>(n));
    ht.assign((1<<h)+1,0);
    for(int j=2;j<(1<<h)+1;j++) ht[j]=ht[j>>1]+1;

    for(int j=0;j<n;j++) dat[0][j]=vs[j];
    for(int i=1;i<h;i++){
      int s=1<<i;
      for(int j=0;j<n;j+=s<<1){
        int t=min(j+s,n);
        dat[i][t-1]=vs[t-1];
        for(int k=t-2;k>=j;k--) dat[i][k]=f(vs[k],dat[i][k+1]);
        if(n<=t) break;
        dat[i][t]=vs[t];
        int r=min(t+s,n);
        for(int k=t+1;k<r;k++) dat[i][k]=f(dat[i][k-1],vs[k]);
      }
    }
  }

  T query(int l,int r){
    if(l>=--r) return dat[0][l];
    return f(dat[ht[l^r]][l],dat[ht[l^r]][r]);
  }

};
//END CUT HERE
#ifndef call_from_test

// find with non-idempotent monoid
signed CODECHEF_SEGPROD(){
  int T;
  scanf("%d",&T);

  int p;
  auto f=[&](int a,int b)->int{return (long long)a*b%p;};
  DisjointSparseTable<int> dst(f);

  for(int t=1;t<=T;t++){
    int n,q;
    scanf("%d %d %d",&n,&p,&q);
    vector<int> v(n);
    for(int i=0;i<n;i++) scanf("%d",&v[i]),v[i]%=p;
    vector<int> b(q/64+2);
    for(int i=0;i<(q/64+2);i++) scanf("%d",&b[i]);

    dst.build(v);

    int x=0,l=0,r=0;
    for(int i=0;i<q;i++){
      if(i%64==0){
        l=(b[i/64]+x)%n;
        r=(b[i/64+1]+x)%n;
      }else{
        l=(l+x)%n;
        r=(r+x)%n;
      }
      if(l>r) swap(l,r);
      x=dst.query(l,r+1)+1;
      if(x>=p) x-=p;
    }
    printf("%d\n",x);
  }
  return 0;
}
/*
  verified on 2019/10/29
  https://www.codechef.com/problems/SEGPROD
*/

//INSERT ABOVE HERE
signed main(){
  //CODECHEF_SEGPROD();
  return 0;
}
#endif

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  using ll = long long;
  int n;
  cin>>n;
  vector<ll> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  auto f=[&](ll a,ll b){return gcd(a,b);};
  DisjointSparseTable<ll> dst(f);
  dst.build(as);

  ll ans=0;
  for(int i=0;i<n;i++){
    int l=i;
    ll pre=as[i],lst=dst.query(i,n);
    while(lst!=pre){
      int r=n,pl=l;
      while(l+1<r){
        int m=(l+r)>>1;
        if(dst.query(i,m)!=pre) r=m;
        else l=m;
      }
      if(pre==1) ans+=l-pl;
      pre=dst.query(i,r);
    }
    if(lst==1) ans+=n-l;
  }

  cout<<ans<<endl;
  return 0;
}
0