CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Wiki » Tutorial for Product of Divisors

Tutorial for Product of Divisors

The problem Product of Divisors asks us to find the last 4 digits of the product of all the divisors of a number. So, suppose we are given a number like 12. The divisors for any number which is not a perfect square will occur in pairs. So, the divisors for the number 12 will be in the following pairs (1,12), (2,6), (3,4). Now, according to the problem statement, we are not to consider the number 12 while computing the answer, so effectively, we can remove the first pair. So, the answer we need will be the product of the numbers 2,6,3,4 which equals 144. Now, the purpose of grouping the numbers in pairs was to show that the product of the numbers in each pair is 12. So, if for a number N, which is not a perfect square, having D divisors, we can always group them into D/2 pairs such that the product of each pair is the original number N. Thus, we effectively transform the answer into N*N*..*N (D-2)/2 times. Thus, The answer in this case becomes N ^ (D/2) where ^ represents the power function and D represents the number of divisors. Thus, for the number 12, the number of divisors is 6, so the answer is 12 ^ ((6-2)/2) = 12 ^ 2 = 144.

Now, let us take the case of a number which is a perfect square. Consider the number 36 which is a perfect square. The divisors of 36 are 1,36,2,18,3,12,4,9,6. So now, if we start grouping them into pairs such that the product of each pair is 36, we get the following pairs (1,36), (2,18), (3,12), (4,9), (6,6). Now, we see that the divisor 6 needs to be repeated to form the pairs of the sort mentioned previously. We have 9 divisors for the number 36. As earlier, we remove 2 of the divisors, that is, the pair (1,36) as the problem statement asks us not to consider the number N while taking the product. Thus we start grouping the numbers into pairs again : (2,18), (3,12), (4,9) and we get one divisor 6 which is left without a divisor to pair up with. So, consider that we group up 6 with 6 itself to get another pair which gives us the value 36. So, the total product will be (36 ^ (number of pairs))/6. So, effectively if we have to calculate the product of divisors for a number N which is a perfect square, our equation becomes N^((D-2)/2) which is N^(D-2)/2. In the earlier case, as D would always be even, D-2 would be even and (D-2)/2 would be an integer. But in this case, as N is a perfect square, (D-2) is odd. We can reframe this as (N^(1/2))^(D-2). Now, as N is a perfect square, N^(1/2) will be an integer. So the answer in this case becomes sqrt(N)^(D-2).

So, we can see that given a number, we can find the required answer using the expressions mentioned above. So now, the only thing left to do is to find the number of divisors. Now, let us see how to calculate the number of divisors of a particular number. Suppose the number N is factorized into its prime factors and their powers as p1^a1 * p2^a2 * ... * pn^an. Then, the number of divisors of the number N including 1 and N are (a1+1)*(a2+1)*...*(an+1). Let us see why this is so. Take the case that N is a prime number, then the prime factorization of N can be p1^1 where p1=N. N has 2 divisors 1 and N so the answer is (1+1) which is 2. Now consider a number N which has prime factorization p1^a1 * p2^a2 * ... * pn^an. Now, suppose a number B which equals N*P where P is a prime number. Now, the prime factorization of B becomes p1^a1 * p2^a2 * ... * pn^an * p^1. Suppose the number N had X divisors. B equals N*P, so all divisors of N are divisors of B. Also, if T is a divisor of B, then so is P*T. Thus, we see that the number of divisors in this case has doubled. That is because of the introduction of P^1 in the sequence, the number of divisors has become X * (1+1). This can easily be extended to introduction of P^Z in the sequence to show that because of this introduction, the number of divisors is increased by a factor (Z+1).

So, now we know how to calculate the number of divisors of a number. We also know that once we know the number of divisors of a number, we can find the required answer using the formulae we derived. But, there is a small catch here. The number N can be pretty large and we need to find the power of N raised to a number which can be a about 100. Also, we need to do this for about 300000 numbers. If we calculate the power of the number modulo some value using an iterative function that iterates over the value of the exponent, our code won't run within time. We can use a nice little trick here to calculate the value of (a^b)%p by using the observation that if 'b' is even, then a^b = a^(b/2) * a^(b/2) and if 'b' is odd, a^b = a*pow(a,b-1). Using this, we can calculate the power of a number pretty quickly taking about log(exponent) iterations.

Thus, the rough outline of the required code would be something as follows :

[code]


Start
Precompute all primes below 500000
Precompute what numbers below 500000 are perfect squares and their squareroots
Take in number of test cases
While there exists a test case we have not processed
Take in the number.
Find the number of divisors.
Calculate the answer using the formulae we derived.
Take the remainder with 10000 while calculating the answers.
Take care to make sure that we print the last 4 digits correctly with necessary number of 0s. Simply taking modulo 10000 will not work.
Print the answer
Stop

[/code]

We leave it to the reader to handle the part where the last 4 digits have to be correctly displayed.


Comments

  • Login or Register to post a comment.

reallllllllllllllllllllllllll

dabbcomputers @ 19 Jan 2010 10:27 PM

realllllllllllllllllllllllllllllllllllly nice tutorial.....

its g8

rajesh3646 @ 25 Jan 2010 10:10 PM

its g8

I think there is a typo error

Recca @ 16 Feb 2010 03:01 PM

I think there is a typo error in the second last line of the tutorial . It should be "a^b = a*pow(a,b-1)" instead of "a^b = b*pow(a,b-1)". It is a very small error and probably everyone knew the correct expression. :)

by the way nice tutorial

Recca @ 16 Feb 2010 03:02 PM

by the way nice tutorial

can u plz clarify what is

shubh09 @ 16 May 2010 09:10 PM

can u plz clarify what is meant by 'precomputing' all the primes less than 500000. doing so will be included in the running time of the program, right? Or is there some way by which this can be avoided?

Precompute means compute them

triplem @ 17 May 2010 07:04 AM

Precompute means compute them before starting to process test cases. In other words, you calculate them once, rather than recalculating them 300000 times when there are 300000 test cases.

Right. Thanks. I was doing

shubh09 @ 17 May 2010 03:56 PM

Right. Thanks. I was doing exactly that.

I have a further query. I am writing the solution as said in the tutorial, except the precomputation of the perfect squares and their square roots. I don't understand the benefit of doing so.

(I am getting a TLE error. Won't have bothered otherwise.Can't think of anything else that might be causing it.)

Your 'epow' function is the

triplem @ 17 May 2010 04:56 PM

Your 'epow' function is the problem. For example, epow(8) calls epow(4) twice; each of those calls epow(2) twice (so 4 times total), and each of those calls epow(1) twice (so 8 times total). In other words, your epow method is just as slow as multiplying a*a..*a b times.

The point of halving the power is that you only need to call the function recursively once. Calculate epow(a,b/2) once and then multiply that value by itself. Don't call the function twice.

Thanks again. Should have

shubh09 @ 17 May 2010 11:24 PM

Thanks again. Should have realized that myself. But I am still getting a TLE error.

Nice article! Concepts are

rajneesh2k10 @ 14 Jan 2011 04:32 PM

Nice article! Concepts are usefull but you have made the solution complicated!

hii.. Really a nice tutorial,

codeur @ 24 Feb 2011 09:04 PM

hii..

Really a nice tutorial, I was wondering If i have to select first 4 digits instead of last 4, how can this be done? Cant figure it out! please help

hii.. Really a nice tutorial,

codeur @ 24 Feb 2011 09:04 PM

hii..

Really a nice tutorial, I was wondering If i have to select first 4 digits instead of last 4, how can this be done? Cant figure it out! please help

it is hell difficult to

aspiration007 @ 15 Jun 2011 09:22 PM
it is hell difficult to follow.. i cannot make out what the last 2-3 paragraphs say..

hmmm got it

sweetestsahil @ 3 Jul 2011 12:57 PM
hmmm got it

good tutorial,thanks...

sehriyar @ 16 Nov 2011 01:57 PM
good tutorial,thanks...

i am not getting how to

amita14 @ 27 Feb 2012 11:34 PM
i am not getting how to compute last 4 digits of the product.

thanks for the tutorial

sidrakesh @ 2 Mar 2012 04:20 PM
thanks for the tutorial
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com