結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-04-25 11:49:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,679 ms / 2,000 ms |
コード長 | 2,670 bytes |
コンパイル時間 | 2,858 ms |
コンパイル使用メモリ | 203,556 KB |
最終ジャッジ日時 | 2025-01-10 01:02:08 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#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, typename F> struct DisjointSparseTable{ 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, decltype(f)> 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; }