📜  2键键盘问题

📅  最后修改于: 2022-05-13 01:56:07.867000             🧑  作者: Mango

2键键盘问题

给定一个正整数N和一个字符串S ,最初它是“A” ,任务是通过在每个步骤中执行以下操作之一来最小化形成由NA组成的字符串所需的操作数:

  • 复制字符串S中存在的所有字符。
  • 将所有字符附加到上次复制的字符串S中。

例子:

方法:可以根据以下观察解决给定的问题:

  1. 如果N = P 1 *P 2 *P m其中{P 1 , P 2 , ..., P m }是质数,则可以执行以下移动:
    1. 首先,复制字符串,然后将其粘贴(P 1 – 1)次。
    2. 同样,再次复制字符串并粘贴(P 2 – 1)次。
    3. 用剩余的素数执行M次,将得到具有NA 的字符串。
  2. 因此,所需的最小移动总数由(P 1 + P 2 + ... + P m )给出。

请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0 ,它存储操作的结果数量。
  • 求给定整数 N 的质因数及其幂。
  • 现在,遍历N的所有素因子,并通过素因子及其幂的乘积增加ans的值。
  • 最后,完成上述步骤后,打印ans的值作为结果的最小移动次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of steps required to form N number
// of A's
int minSteps(int N)
{
    // Stores the count of steps needed
    int ans = 0;
 
    // Traverse over the range [2, N]
    for (int d = 2; d * d <= N; d++) {
 
        // Iterate while N is divisible
        // by d
        while (N % d == 0) {
 
            // Increment the value of
            // ans by d
            ans += d;
 
            // Divide N by d
            N /= d;
        }
    }
 
    // If N is not 1
    if (N != 1) {
        ans += N;
    }
 
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
    int N = 3;
    cout << minSteps(N);
 
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the minimum number
    // of steps required to form N number
    // of A's
    static int minSteps(int N)
    {
        // Stores the count of steps needed
        int ans = 0;
 
        // Traverse over the range [2, N]
        for (int d = 2; d * d <= N; d++) {
 
            // Iterate while N is divisible
            // by d
            while (N % d == 0) {
 
                // Increment the value of
                // ans by d
                ans += d;
 
                // Divide N by d
                N /= d;
            }
        }
 
        // If N is not 1
        if (N != 1) {
            ans += N;
        }
 
        // Return the ans
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 3;
        minSteps(N);
        System.out.println(minSteps(N));
    }
}
 
// This code is contributed by lokesh potta.


Python3
# Python3 program for the above approach
# Function to find the minimum number
# of steps required to form N number
# of A's
def minSteps( N):
 
    # Stores the count of steps needed
    ans = 0
 
    # Traverse over the range [2, N]
    d = 2
    while(d * d <= N):
 
        # Iterate while N is divisible
        # by d
        while (N % d == 0):
 
            # Increment the value of
            # ans by d
            ans += d
 
            # Divide N by d
            N /= d
        d += 1
     
    # If N is not 1
    if (N != 1):
        ans += N
     
    # Return the ans
    return ans
 
# Driver Code
N = 3
print(minSteps(N))
 
# This code is contributed by shivanisinghss2110


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the minimum number
// of steps required to form N number
// of A's
static int minSteps(int N)
{
    // Stores the count of steps needed
    int ans = 0;
 
    // Traverse over the range [2, N]
    for (int d = 2; d * d <= N; d++) {
 
        // Iterate while N is divisible
        // by d
        while (N % d == 0) {
 
            // Increment the value of
            // ans by d
            ans += d;
 
            // Divide N by d
            N /= d;
        }
    }
 
    // If N is not 1
    if (N != 1) {
        ans += N;
    }
 
    // Return the ans
    return ans;
}
 
// Driver Code
static public void Main ()
{
     
    int N = 3;
    Console.Write(minSteps(N));
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
3

时间复杂度: O(√N)
辅助空间: O(1)