UVa 1642 (integrated) magical GCD

Source: Internet
Author: User
Tags greatest common divisor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.