Hackerearth: Little Shino and Common factors

Little Shino loves maths. Today her teacher gave her two integers. Shino is now wondering how many integers can divide both the numbers. She is busy with her assignments. Help her to solve the problem.
Input:
First line of the input file contains two integers, a and b.
Output:
Print the number of common factors of a and b.
Constraints:
1a,b1012



Editorial
Brief Description: Given two integers a and b, find the number of factors common to both a and b.
Detailed Explanation:
We know that the highest factor common to both a and b is gcd(a,b). So, the problem dilutes to finding the number of factors of gcd(a,b).
But how to find this?
Approach 1:
Let N be the number and we want to find its all factors. So, we can iterate from 1 to N to get all the factors. What we do is we check if 1 N divides N or not, if yes, then i and N/i are the factors of N. Thus, we count all of them in O(N). See the author's solution to get implementation details of this approach.
Approach 2:
Let N be a number then we can use its prime factors to count the total number of factors of N.
Let N= ap * bq *cr where a,b and c are prime factors of N. Then according to the Product Rule of counting the total number of combinations - each combination being a factor of N - equals 
(1+p)*(1+q)*(1+r)
and this is our answer as we wanted to find the total number of factors of N. So, we can start by finding all prime factors and their exponents for given N and then apply the above formula to get our final answer. 
We can find prime factors and their exponents in O(N). See the tester's solution for implementation.
Complexity: O(N )

Comments

  1. https://devenc234.blogspot.com/2018/10/hackerearth-little-shino-and-common.html

    ReplyDelete

Post a Comment

Popular Posts