Consider adding a number to a collection whose gcd are either unchanged or become an approximate of the original GCD. So the different gcd have only log ones.
So for each location, maintain a table that stores all the different gcd from this location forward and its initial position, and then violence updates the answer, anyway this table won't be very long.
#include <cstdio> #include <algorithm> #include <cstring>using namespace std; #define N 100001typedef Long long ll;typedef pair<ll,int> point; Point Table[101],t[101];int t,n,en,e2;ll gcd (ll A,ll b) {return (!b)? A:GCD (b,a%b);} int main () {ll x;scanf ("%d", &t); T;--T) { ll ans=0; en=0; scanf ("%d", &n); for (int i=1;i<=n;++i) { scanf ("%lld", &x); for (int j=1;j<=en;++j) table[j].first=gcd (table[j].first,x); Table[++en]=make_pair (x,i); Sort (table+1,table+1+en); e2=0; for (int j=1;j<=en;++j) if (Table[j].first!=table[j-1].first) t[++e2]=table[j]; En=e2; memcpy (table+1,t+1,sizeof (point) *e2); for (int j=1;j<=en;++j) Ans=max (ans,table[j].first* (LL) (i-table[j].second+1)); } printf ("%lld\n", ans); } return 0;}
"Number theory", "Violence" bzoj4052 [cerc2013]magical GCD