Bzoj 4052: [cerc2013]magical GCD

Source: Internet
Author: User

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

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.