Блог пользователя Tobby_And_Friends

Автор Tobby_And_Friends, история, 10 лет назад, По-английски

UVA 13083

Name: Yet another GCDSUM

Link: https://uva.onlinejudge.org/external/130/13083.pdf

I did get the idea that I'll initially compute all the divisors of N using prime factorization & backtracking but I'm stuck in how to compute the gcd of the divisor pairs with a better complexity than O(n^2). Any help is really appreciated.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
10 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

You can notice that the function is multiplicative. Thus, it is enough to think about the value of the function on the power of primes.

Do you see the formula?

»
10 лет назад, скрыть # |
← Rev. 5  
Проголосовать: нравится -10 Проголосовать: не нравится

Ignore the below idea , it is incorrect

Once you see this function is multiplicative , you can prime factorize the number.After you have that then let's say the prime factorization is {(p1x1)*(p2x2)...}.Then the required ans is G(n)=G(p1x1)*G(p2x2)*.... *G(pnxn). The first term can then be written as G(p1)^x1 , now since p1 is prime G(p1) =GCD(1,p1)+GCD(p1,1)+GCD(p1,p1) = p1+2, from here on you can see the answer.

»
10 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

anta, meant to say that, since the function being multiplicative, we just need to compute the answers for prime powers only.

For doing that, I just used a simple "2 nested for loops". Consider n = p^a, some some prime 'p' and exponent 'a'. Now, if 'm' divides 'n', then it is of the form m = p^i, for 0<=i<=a.

Now, m1 and m2, we need to add their gcd to the answer, and we know gcd(p^i, p^j) = p^min(i,j). This is simply computed using "2 nested for loops"

As far as factorisation of number is considered, I used Pollard-Rho-Brent factorisation, having complexity of O(n^(1/4) log(n)). Also, since log(n) = 46, the nested for loops will not take much time as compared to the factorisation part. This solution of mine written in python3, gets accepted in 0.100 seconds.

For the source code, you can refer to Here .

If you have any doubts, you can ask below. Also, I found finding the formula for p^a tough, So I just wrote simple nested for loops which in this case is much smaller than factorisation which dominates time complexity.