Bzoj4488 [Jsoi2015] Greatest common divisor

Source: Internet
Author: User
Tags gcd greatest common divisor

Time Limit:10 Sec Memory limit:256 MB
submit:172 solved:101

Description

Given a sequence of positive integers of length N ai for any successive subsequence of it
{al,al+1...ar}, we define its weight w (l,r) as the product of its length with the greatest common divisor of all elements in the sequence, i.e. w (l,r) = (r-l+1) ∗gcd (Al: AR).
Jyy want to find out which sub-sequence is the most weighted.

Input

The input line consists of a positive integer N.
The next line, containing n positive integers, represents the sequence AI
1 < = Ai < = 10^12, 1 < = N < = 100,000

Output

The output file contains a single positive integer that represents the weight of the subsequence with the highest weight.

Sample Input5
theSample Output80
The best sub-sequence is a subsequence of the last 4 elements. Hintsource

Scanning water Problems

It's time for the water problem again ~

The sequence of gcd converges quickly, as if it were concluded that different gcd would not exceed the log (sequence length) of

Use a map to save the current GCD where it first appeared, scan the sequence of statistical answers.

If that conclusion is correct, the complexity is probably $ O (n log^2 N) $ (map comes with a log)

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <map>7 #defineLL Long Long8 using namespacestd;9 Const intmxn=100010;Ten LL Read () { OneLL x=0, f=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; the } -ll GCD (ll A,ll b) {returnB?GCD (b,a%b): A;} -Map<ll,int>mp,tmp; -Map<ll,int>:: iterator it; + intN; - LL A[MXN]; + intMain () { A //freopen ("In.txt", "R", stdin); at     inti,j; -n=read (); -LL ans=0; -      for(i=1; i<=n;i++){ -A[i]=read (); ans=Max (ans,a[i]); -          for(It=mp.begin (); It!=mp.end (); it++){ inLL G=GCD (*it). First,a[i]); -Ans=max (ans,g* (i-(*it). second+1)); to             if(!tmp.count (g)) tmp[g]= (*it). Second; +             ElseTmp[g]=min (Tmp[g], (*it). second); -         } the         if(!tmp.count (A[i])) tmp[a[i]]=i; *mp=tmp; $ tmp.clear ();Panax Notoginseng     } -printf"%lld\n", ans); the     return 0; +}

Bzoj4488 [Jsoi2015] Greatest common divisor

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.