📌  相关文章
📜  最小化系列 a, a/b^1, a/b^2, a/b^3, ..., a/b^n 中 a 的值,使得初始非零项的总和至少变为 S

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

最小化系列 a, a/b^1, a/b^2, a/b^3, ..., a/b^n 中 a 的值,使得初始非零项的总和至少变为 S

给定两个整数bS 。任务是找到“ a ”的最小值,使得对于初始非零项的总和等于或大于“ S ”。

例子:

方法:这个问题可以通过二分查找来解决。显然,如果数字“ a ”是一个答案,那么每个数字n > a也是一个答案,因为值只会变得更多,但我们需要找到最小值。因此,要检查某个数字“a”,我们可以使用问题本身中给出的公式。请按照以下步骤解决问题:

  • 将变量a初始化为1,低0S。
  • 在 while 循环中遍历直到low小于等于high并执行以下任务:
    • 将变量mid初始化为高的平均值。
    • x初始化为b并将sum初始化为mid。
    • 在while循环中遍历直到mid/x大于0并执行以下任务:
      • mid/x的值添加到变量sum中。
      • 将值b乘以变量x。
    • 如果sum大于等于S ,则将a设置为mid ,将high设置为mid-1。
    • 否则设置中+1。
  • 执行上述步骤后,打印a的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum value
// of numerator such that sum of certain
// terms in the given series become
// equal or greater than X
int findMinNumerator(int b, int S)
{
 
    // Variable to store the ans
    // initialized with 1 which
    // can be the minimum answer
    int a = 1;
    int low = 0, high = S;
 
    // Iterate till low is less or
    // equal to high
    while (low <= high) {
 
        // Find the mid value
        int mid = (low + high) / 2;
        int x = b, sum = mid;
 
        // While mid / x is greater than
        // 0 keep updating sum and x
        while (mid / x > 0) {
            sum += mid / x;
            x *= b;
        }
 
        // If sum is greater than S,
        // store mid in ans and update
        // high to search other minimum
        if (sum >= S) {
            a = mid;
            high = mid - 1;
        }
 
        // Else update low as (mid + 1)
        else if (sum < S) {
            low = mid + 1;
        }
    }
 
    // Return the answer
    return a;
}
 
// Driver Code
int main()
{
    int b = 2, S = 4;
 
    cout << findMinNumerator(b, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
public class GFG
{
// Function to find the minimum value
// of numerator such that sum of certain
// terms in the given series become
// equal or greater than X
static int findMinNumerator(int b, int S)
{
     
    // Variable to store the ans
    // initialized with 1 which
    // can be the minimum answer
    int a = 1;
    int low = 0, high = S;
 
    // Iterate till low is less or
    // equal to high
    while (low <= high) {
 
        // Find the mid value
        int mid = (low + high) / 2;
        int x = b, sum = mid;
 
        // While mid / x is greater than
        // 0 keep updating sum and x
        while (mid / x > 0) {
            sum += mid / x;
            x *= b;
        }
 
        // If sum is greater than S,
        // store mid in ans and update
        // high to search other minimum
        if (sum >= S) {
            a = mid;
            high = mid - 1;
        }
 
        // Else update low as (mid + 1)
        else if (sum < S) {
            low = mid + 1;
        }
    }
 
    // Return the answer
    return a;
}
 
// Driver Code
public static void main(String args[])
{
    int b = 2, S = 4;
    System.out.println(findMinNumerator(b, S));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python 3 program for the above approach
 
# Function to find the minimum value
# of numerator such that sum of certain
# terms in the given series become
# equal or greater than X
def findMinNumerator(b, S):
 
    # Variable to store the ans
    # initialized with 1 which
    # can be the minimum answer
    a = 1
    low = 0
    high = S
 
    # Iterate till low is less or
    # equal to high
    while (low <= high):
 
        # Find the mid value
        mid = (low + high) // 2
        x = b
        sum = mid
 
        # While mid / x is greater than
        # 0 keep updating sum and x
        while (mid // x > 0):
            sum += mid // x
            x *= b
 
        # If sum is greater than S,
        # store mid in ans and update
        # high to search other minimum
        if (sum >= S):
            a = mid
            high = mid - 1
 
        # Else update low as (mid + 1)
        elif (sum < S):
            low = mid + 1
 
    # Return the answer
    return a
 
# Driver Code
if __name__ == "__main__":
 
    b = 2
    S = 4
 
    print(findMinNumerator(b, S))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections;
 
public class GFG
{
// Function to find the minimum value
// of numerator such that sum of certain
// terms in the given series become
// equal or greater than X
static int findMinNumerator(int b, int S)
{
     
    // Variable to store the ans
    // initialized with 1 which
    // can be the minimum answer
    int a = 1;
    int low = 0, high = S;
 
    // Iterate till low is less or
    // equal to high
    while (low <= high) {
 
        // Find the mid value
        int mid = (low + high) / 2;
        int x = b, sum = mid;
 
        // While mid / x is greater than
        // 0 keep updating sum and x
        while (mid / x > 0) {
            sum += mid / x;
            x *= b;
        }
 
        // If sum is greater than S,
        // store mid in ans and update
        // high to search other minimum
        if (sum >= S) {
            a = mid;
            high = mid - 1;
        }
 
        // Else update low as (mid + 1)
        else if (sum < S) {
            low = mid + 1;
        }
    }
 
    // Return the answer
    return a;
}
 
// Driver Code
public static void Main()
{
    int b = 2, S = 4;
    Console.Write(findMinNumerator(b, S));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

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