Test instructions
A sequence is given to make a continuous subsequence, so that mgcd (i, j) = The length of the subsequence (j-i+1) x sub-sequence GCD maximum, and outputs the maximum value.
Analysis:
It may feel like a priority queue, but it doesn't seem to be used.
But similarly, the right endpoint is enumerated from left to right, and it is not difficult to find that the greatest common divisor of its subsequence is non-incrementing as the length of the sequence increases. In general, it is a ladder-like decline. So we just keep the same gcd, the smallest left end of the sequence (because the same GCD has the largest length, MGCD is the largest).
When the right endpoint is updated, the GCD of each subsequence is updated, and the subsequence of the repeating gcd is removed, leaving only the one with the largest length.
This reduces the number of repetitive gcd calculations.
1#include <cstdio>2#include <algorithm>3#include <vector>4 using namespacestd;5typedefLong LongLL;6 7 Const intMAXN =100000+Ten;8 LL A[MAXN];9 Ten structHEHE One { ALL G;//GCD - intP//Start Position -HEHE (LL g=0,intp=0): g (g), p (p) {} the BOOL operator< (Consthehe& RHS)Const - { - returnG < RHS.G | | (g = = RHS.G && p <RHS.P); - } + }; - +ll GCD (ll A, ll b) {returnb = =0? A:GCD (b, a%b); } A at intMain () - { - intT, N; -scanf"%d", &T); - while(t--) - { inscanf"%d", &n); - for(inti =0; I < n; ++i) scanf ("%lld", &a[i]); toLL ans =0; +Vectorcur; - for(intj =0; J < N; ++J)//Enumerate right Endpoints the { *Cur.push_back (HEHE (0, J)); $ for(intK =0; K < Cur.size (); ++K)//Update the value of GCDPanax NotoginsengCUR[K].G =gcd (CUR[K].G, a[j]); - sort (Cur.begin (), Cur.end ()); the +VectorNani; A for(intK =0; K < Cur.size (); ++k) the if(k = =0|| cur[k-1].G! =cur[k].g) +{//The same GCD only takes the first number at a time. - Nani.push_back (Cur[k]); $ans = max (ans, cur[k].g * (J-CUR[K].P +1)); $ } -Cur =Nani; - } the -printf"%lld\n", ans);Wuyi } the - return 0; Wu}
code June
UVa 1642 (integrated) magical GCD