Given two numbers A and B ( A and B can be up to 106 ) which forms a number N = (A!/B!). The task is to reduce N to 1 by performing maximum number of operations possible.
In each operation, one can replace N with N/X if N is divisible by X.
Find the maximum number of operations that can be possible.
Input : A = 6, B = 3 Output : 5 Explanation : N is 120 and the divisors are 2, 2, 2, 3, 5 Input : A = 2, B = 1 Output : 1 Explanation : N is 2 and the divisor is 2.
Observe that factorization of number A!/B! is this same as factorization of numbers (B + 1)*(B + 2)*…*(A – 1)*A.
Also, the number of operations will be maximum if we divide N with only with it’s prime factors. So, in other words we need to find the count of prime factors of N including duplicates.
Let’s count the number of prime factors in the factorization of every number from 2 to 1000000.
First, use Sieve of Eratosthenes to find a prime divisor of each of these numbers. Then we can calculate the number of prime factors in the factorization of a using the formula:
primefactors[num] = primefactors[num / primediviser[num]] + 1
Now, one can use prefix sum array for prime factors and then answer for the sum on an interval [A, B].
Below is the implementation of the above approach:
- Reduce a number to 1 by performing given operations | Set 2
- Reduce N to 1 with minimum number of given operations
- Minimum number of operations required to reduce N to 1
- Count operations of the given type required to reduce N to 0
- Find maximum value of the last element after reducing the array with given operations
- Minimum number of given operations required to reduce the array to 0 element
- Count the number of operations required to reduce the given number
- Maximum sum of all elements of array after performing given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum value in an array after m range increment operations
- Maximum count of equal numbers in an array after performing given operations
- Find if it is possible to make all elements of an array equal by the given operations
- Find the final sequence of the array after performing given operations
- Find the modified array after performing k operations of given type
- Find minimum operations needed to make an Array beautiful
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.