📌  相关文章
📜  查找最大操作以将N减少到1

📅  最后修改于: 2021-06-26 15:27:50             🧑  作者: Mango

给定两个数字A和B(A和B最多为10 6 ),形成一个数字N =(A!/ B!) 。任务是通过执行可能的最大操作数将N减小为1
在每个操作中,如果N可被X整除,则可以用N / X替换N。
找到可以进行的最大操作数。
例子:

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.

观察数字A!/ B!的因式分解。这与(B + 1)*(B + 2)*…*(A – 1)* A的因式分解相同。
此外,如果我们仅将N除以其素数,则运算次数将是最大的。因此,换句话说,我们需要找到N的素数(包括重复项)的数量。
让我们计算从2到1000000的每个数的因式分解中的素数个数。
首先,使用Eratosthenes筛子查找每个数字的质数除数。然后,我们可以使用以下公式计算a的分解中素数的数量:

primefactors[num] = primefactors[num / primediviser[num]] + 1

现在,可以使用前缀求和数组作为主要因子,然后以区间[A,B]求和。
下面是上述方法的实现:

C++
// CPP program to find maximum
// number moves possible
 
#include 
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


Javascript


输出:
5

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。