4052: [cerc2013]magical GCDDescriptiona sequence of positive integers with a length of less than 100 000 is given, and the size does not exceed 10^12. a continuous sub-sequence so that in all successive sub-sequences, their gcd values are multiplied by their maximum length. Sample Input1
5
theSample Output theThe following:a little god. Read the gcd the most logn species of the sequence starting at a point (at all). ) We can use ST table to maintain the GCD value of an interval, and find the longest gcd the same interval can be used in two points.
#include <stdio.h> #include <iostream> #include <math.h>using namespace Std;const int n=100005;# Define ll long longint t,n,i,j,x,l,r,mid,log[n];ll ans,f[n<<1][20];inline void Read (ll &v) {char ch,fu=0; for (ch= ' * '; (ch< ' 0 ' | | Ch> ' 9 ') &&ch!= '-'; Ch=getchar ()); if (ch== '-') fu=1, Ch=getchar (); for (v=0; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) v=v*10+ch-' 0 '; if (FU) v=-v;} ll GCD (ll A,ll b) {if (b==0) return a;else return gcd (b,a%b);} ll solve (int l,int r) {int x=log[r-l+1]; Return gcd (f[l][x],f[r-(1<<x) +1][x]);} int main () {scanf ("%d", &t); for (i=1;i<=100000;i++) log[i]=log2 (i); while (t--) {scanf ("%d", &n); ans=0; for (i=1;i<=n;i++) read (f[i][0]); For (J=1, (1<<j) <=n;j++) for (i=1;i<=n;i++) F[I][J]=GCD (f[i][j-1],f[i+ (1<<j-1)][j-1]); for (i=1;i<=n;i++) {x=i; while (x<=n) {L=x;r=n; ll S=solve (i,x); while (l<=r) {mid= (l+r) >>1; if (S==solve (i,mid)) L=mid+1;else r=mid-1; } Ans=max (Ans,solve (i,r) * (r-i+1)); X=l; }} printf ("%lld\n", ans); } return 0;}
Bzoj 4052: [cerc2013]magical GCD