1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include<bits/stdc++.h> using namespace std; #define int long long void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; } void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double #define ll long long #define pii pair<int,int > #define vec vector<int > #define pb push_back #define mp make_pair #define fr first #define sc second #define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) const int maxn = 1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7; int v[maxn],cnt,n; struct _data {int x;ll t;}t[maxn]; #define ls p<<1 #define rs p<<1|1 #define mid ((l+r)/2) struct ST_table { int f[50050][16],lg[50050]; void build() { for(int i=1;i<=n;i++) f[i][0]=v[i]; for(int j=1;j<=15;j++) for(int i=1;i<=n;i++) f[i][j]=__gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]); for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1; } int query(int x,int y) { int t=lg[y-x+1]; return __gcd(f[x][t],f[y-(1<<t)+1][t]); } }T; void prepare() { map<int,ll > s; T.build(); for(int i=1;i<=n;i++) { int p=i,now=v[i]; while(p<=n) { int l=p,r=n,pre=p; while(l<=r) if(T.query(i,mid)==now) p=mid,l=mid+1; else r=mid-1; s[now]+=p-pre+1; p++;now=__gcd(now,v[p]); } } for(map<int,ll > :: iterator i=s.begin();i!=s.end();i++) t[++cnt]=(_data){i->fr,i->sc}; } ll f(ll a,ll b,ll c,ll n) { if(n<0) return 0; if(!a) return (n+1)*(b/c); if(a>=c||b>=c) return f(a%c,b%c,c,n)+n*(n+1)/2*(a/c)+(n+1)*(b/c); ll m=(n*a+b)/c; return n*m-f(c,c-b-1,a,m-1); } ll sum[maxn],ct[maxn]; ll s(ll a,ll b,ll c) { if(c<0) return 0; int x=f(a,c%a,b,c/a)+c/a+1; return x; } ll calc(ll a,ll b,ll md) { if(a>=b) return 0; ll ss=md-(ct[b-1]-ct[a]),x=t[a].x,y=t[b].x;ss-=x,ss-=y; if((t[a].t-1)*x+(t[b].t-1)*y<=ss) return t[a].t*t[b].t; return s(x,y,ss)-s(x,y,ss-t[a].t*x)-s(x,y,ss-t[b].t*y)+s(x,y,ss-t[a].t*x-t[b].t*y); } ll get(ll x) { ll p=1,ans=0; for(int i=1;i<=cnt;i++) { ll w=min(x/t[i].x,t[i].t); ans+=(t[i].t+1)*w-w*(w+1)/2; } for(int i=1;i<=cnt;i++) { if(p>i+1) ans+=1ll*t[i].t*(sum[p-1]-sum[i]); while(p<=cnt&&ct[p]-ct[i]<=x) ans+=calc(i,p,x),p++; if(p<=cnt) ans+=calc(i,p,x); } return ans; } signed main() { int st=clock(); read(n); for(int i=1;i<=n;i++) read(v[i]); prepare(); for(int i=1;i<=cnt;i++) sum[i]=sum[i-1]+t[i].t; for(int i=1;i<=cnt;i++) ct[i]=ct[i-1]+1ll*t[i].t*t[i].x; ll l=1,r=ct[cnt]+1,w=1ll*n*(n+1)/2; w=w*(w+1)/2;w=(w+1)/2; while(l<r) { if(get(mid)<w) l=mid+1; else r=mid; }write(l); cerr<<(lf)(clock()-st)/1e3<<endl; return 0; }
|