最小化系列 a, a/b^1, a/b^2, a/b^3, ..., a/b^n 中 a 的值,使得初始非零项的总和至少变为 S
给定两个整数b和S 。任务是找到“ a ”的最小值,使得对于初始非零项的总和等于或大于“ S ”。
a, a/b1, a/b2, a/b3, …………., a/bn
例子:
Input: b = 2, S = 4
Output: 3
Explanation:
- Let a = 1, S = 1/20 + 1/21 = 1 + 0 = 1 < 4.
- Let a =2, S = 2/20 + 2/21 + 2/22 = 2 + 1 + 0 = 3 < 4.
- Let a = 3, S = 3/20 + 3/21 + 3/22 = 3 + 1 + 0 = 4 = S.
So, a = 3 is the answer.
Input: b = 8, S = 25
Output: 23
方法:这个问题可以通过二分查找来解决。显然,如果数字“ a ”是一个答案,那么每个数字n > a也是一个答案,因为值只会变得更多,但我们需要找到最小值。因此,要检查某个数字“a”,我们可以使用问题本身中给出的公式。请按照以下步骤解决问题:
- 将变量a初始化为1,低为0 ,高为S。
- 在 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)