Find maximum operations to reduce N to 1
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.
Examples:
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:
C++
// CPP program to find maximum // number moves possible #include <bits/stdc++.h> using namespace std; #define N 1000005 // To store number of prime // factors of each number int primeFactors[N]; // Function to find number of prime // factors of each number void findPrimeFactors() { for (int i = 2; i < N; i++) // if i is a prime number if (primeFactors[i] == 0) for (int j = i; j < N; j += i) // increase value by one from // it's preveious multiple primeFactors[j] = primeFactors[j / i] + 1; // make prefix sum // this will be helpful for // multiple test cases for (int i = 1; i < N; i++) primeFactors[i] += primeFactors[i - 1]; } // Driver Code int main() { // Generate primeFactors array findPrimeFactors(); int a = 6, b = 3; // required answer cout << primeFactors[a] - primeFactors[b]; return 0; } |
Java
// Java program to find maximum // number moves possible import java.io.*; class GFG { static int N= 1000005; // To store number of prime // factors of each number static int primeFactors[] = new int[N]; // Function to find number of prime // factors of each number static void findPrimeFactors() { for (int i = 2; i < N; i++) // if i is a prime number if (primeFactors[i] == 0) for (int j = i; j < N; j += i) // increase value by one from // it's preveious multiple primeFactors[j] = primeFactors[j / i] + 1; // make prefix sum // this will be helpful for // multiple test cases for (int i = 1; i < N; i++) primeFactors[i] += primeFactors[i - 1]; } // Driver Code public static void main (String[] args) { // Generate primeFactors array findPrimeFactors(); int a = 6, b = 3; // required answer System.out.println (primeFactors[a] - primeFactors[b]); } } // This code is contributed by jit_t. |
Python3
# Python3 program to find maximum # number moves possible N = 1000005 # To store number of prime # factors of each number primeFactors = [0] * N; # Function to find number of prime # factors of each number def findPrimeFactors() : for i in range(2, N) : # if i is a prime number if (primeFactors[i] == 0) : for j in range(i, N, i) : # increase value by one from # it's preveious multiple primeFactors[j] = primeFactors[j // i] + 1; # make prefix sum this will be # helpful for multiple test cases for i in range(1, N) : primeFactors[i] += primeFactors[i - 1]; # Driver Code if __name__ == "__main__" : # Generate primeFactors array findPrimeFactors(); a = 6; b = 3; # required answer print(primeFactors[a] - primeFactors[b]); # This code is contributed by Ryuga |
C#
// C# program to find maximum // number moves possible using System; class GFG { static int N = 1000005; // To store number of prime // factors of each number static int []primeFactors = new int[N]; // Function to find number of prime // factors of each number static void findPrimeFactors() { for (int i = 2; i < N; i++) // if i is a prime number if (primeFactors[i] == 0) for (int j = i; j < N; j += i) // increase value by one from // it's preveious multiple primeFactors[j] = primeFactors[j / i] + 1; // make prefix sum // this will be helpful for // multiple test cases for (int i = 1; i < N; i++) primeFactors[i] += primeFactors[i - 1]; } // Driver Code static public void Main () { // Generate primeFactors array findPrimeFactors(); int a = 6, b = 3; // required answer Console.WriteLine(primeFactors[a] - primeFactors[b]); } } // This code is contributed by ajit |
PHP
<?php // PHP program to find maximum // number moves possible $N = 10005; // To store number of prime // factors of each number $primeFactors = array_fill(0, $N, 0); // Function to find number of prime // factors of each number function findPrimeFactors() { global $N,$primeFactors; for ($i = 2; $i < $N; $i++) // if i is a prime number if ($primeFactors[$i] == 0) for ($j = $i; $j < $N; $j += $i) // increase value by one from // it's preveious multiple $primeFactors[$j] = $primeFactors[(int)($j / $i)] + 1; // make prefix sum // this will be helpful for // multiple test cases for ($i = 1; $i < $N; $i++) $primeFactors[$i] += $primeFactors[$i - 1]; } // Driver Code // Generate primeFactors array findPrimeFactors(); $a = 6; $b = 3; // required answer print(($primeFactors[$a] - $primeFactors[$b])); // This code is contributed by chandan_jnu ?> |
5
Recommended Posts:
- Reduce N to 1 with minimum number of given operations
- Count operations of the given type required to reduce N to 0
- Minimum number of operations required to reduce N to 1
- 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 value in an array after m range increment operations
- Maximum sum of all elements of array after performing given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Find the modified array after performing k operations of given type
- Find the final sequence of the array after performing given operations
- Find minimum operations needed to make an Array beautiful
- Find minimum number of merge operations to make an array palindrome
- Find the number of operations required to make all array elements Equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



