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:
- Count operations of the given type required to reduce N to 0
- Minimum number of operations required to reduce N to 1
- Maximum Possible Product in Array after performing given Operations
- Maximum value in an array after m range increment operations
- Maximum sum of all elements of array after performing given operations
- Maximum count of equal numbers in an array after performing given operations
- Find minimum operations needed to make an Array beautiful
- Find the number of operations required to make all array elements Equal
- Find minimum number of merge operations to make an array palindrome
- Reduce the array to a single element with the given operation
- Count number of step required to reduce N to 1 by following certain rule
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Reduce the string by removing K consecutive identical characters
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find maximum value of x such that n! % (k^x) = 0