bc#40d GCD Value Statistics

Source: Internet
Author: User
Tags gcd greatest common divisor

It's a bit disgusting, a lot of things piled together. Just beginning to see the essence is the SG, so very happy to knock, and then found that statistics gcd very annoying, pushed a half-day thought to launch, write finished to the last step, only think of violent traversal. After the submission of tle, pour back to think the tree on the arbitrary path weights and problems, this is not the bare tree division, before a glance can be seen. Just the last one to add a tree to the treatment of slightly nausea. The next morning suddenly found that the statistical gcd value of the place, is actually n^2.5, brain residue, complexity is not allowed. The essence of the problem is:

∑NI=1)∑MJ=1)GCD(I,J)=∑NI=1)∑MJ=1)∑D|I&D|J?(D)=∑MIN(n, Md= 1) (d ) ⋅n/d⋅m/ d

< Span class= "Munderover" > < Span class= "Texatom" >< span class= "Mo" > Here you can't insert a formula that encloses the upper and lower bounds of the ∑ in parentheses. is essentially the sum of the greatest common divisor of any I and J in the range of (1~n) and (1~m). Can be preprocessed, the range of values is 10^4, to 10^4 this query, requires sqrt (n) The complexity of solving a query.

First a transform to GCD(I,J) =∑ (d| I&d| j)? (D) is the sum of all the approximate Euler functions of i,j's greatest common divisor for both of them. So there is no need to go to the heavy, the allowance of something. Then for each approximate d in the (1,n) range of the number of N/D contains the approximate d, the same (1,M) has m/d, so get the above formula.

Finally the problem becomes ∑ (MIN(N,Md=1) ? (d) ⋅n/d⋅m/d. Because the results of n/d and m/d have a maximum of sqrt (n) levels of different values, so can be processed out of these values, the corresponding interval segment, for the n/d of sqrt (n), M/d has sqrt (m) segment, the two of their interval segment merge up to sqrt (n) +sqrt (m) segment, The values for each segment are the same as the values in the x,n/x, and the m/x value is the same,

so can be unified solution, preprocessing? (d) with a query complexity at the sqrt (n) level.

bc#40d GCD Value Statistics

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.