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