📌  相关文章
📜  要求的最小大小的二进制字符串,以便随机删除两个1的概率为1 / X

📅  最后修改于: 2021-04-26 05:36:46             🧑  作者: Mango

给出的值X,该任务是找到最小尺寸二进制字符串,使得如果任何2字符被随机删除,这两个字符将是“1”的概率为1 / X。打印此类二进制字符串的大小。
例子:

方法:我们将尝试找到解决该问题的公式。

  • 如果希望2个字符为1,则例数= r C 2
  • 因此,P(均为1)= r C2 /(r + b) C2
    \dfrac{\dbinom{r}{2}}{\dbinom{r+b}{2}} = \dfrac1x \\ \\ => \dfrac{r(r - 1)}{(r+b)(r + b - 1)} = \dfrac1x
  • 进一步进行计算的棘手观察是:
    \dfrac{r}{r+b} > \dfrac{r-1}{r+b-1}
  • 平方不等式并与等式进行比较,我们得到
    (\dfrac{r}{r+b}) ^ { 2 } > \dfrac{1}{x} > (\dfrac{r - 1}{r+b - 1}) ^ { 2 }
  • 如果r> 1,我们在所有3边取平方根。
    \dfrac{r}{r+b} > \dfrac{1}{\sqrt x} > \dfrac{r - 1}{r + b - 1}
  • 取不等式最左边的部分,我们得到:
    \dfrac{r}{r+b} > \dfrac{1}{\sqrt x} \newline => r \sqrt{x} > r+ b \\ => r( \sqrt {x} - 1) > b \\ => r > \dfrac{b}{\sqrt {x} - 1} \\ => r > (\sqrt {x} + 1) b
  • 同样,考虑不平等的最右边部分,我们得到:
     \dfrac{1}{\sqrt x} > \dfrac{r - 1}{r+b - 1}  \newline =>  r+ b -1 > r \sqrt x - \sqrt x \\ => b > r(\sqrt x - 1) - (\sqrt x - 1) \\ => \dfrac{b}{\sqrt x - 1} > r - 1 \\ => r < 1 + \dfrac{b}{\sqrt x - 1} \\ => r < 1 + (\sqrt x + 1) b
  • 结合得出的结论,我们得到以b表示的r的范围。
    (\sqrt x+1)b + 1 > r > (\sqrt x + 1) b
  • 对于字符串的最小值,我们设置b = 1
    (\sqrt x + 1).1 + 1 > r > (\sqrt x + 1) \\ => \sqrt x + 2 > r > \sqrt x + 1
  • 为了获得有效的最小值r,我们取该范围内r的第一个整数值。

下面是上述方法的实现。

C++
// C++ implementation of the
// above approach
  
#include 
using namespace std;
  
// Function returns the minimum
// size of the string
int MinimumString(int x)
{
    // From formula
    int b = 1;
  
    // Left limit of r
    double left_lim = sqrt(x) + 1.0;
  
    // Right limit of r
    double right_lim = sqrt(x) + 2.0;
  
    int r;
    for (int i = left_lim; i <= right_lim; i++) {
        if (i > left_lim and i < right_lim) {
            // Smallest integer in
            // the valid range
            r = i;
            break;
        }
    }
  
    return b + r;
}
  
// Driver Code
int main()
{
  
    int X = 2;
    cout << MinimumString(X);
    return 0;
}


Java
// Java implementation of the
// above approach
import java.util.*;
  
class GFG{
  
// Function returns the minimum
// size of the String
static int MinimumString(int x)
{
      
    // From formula
    int b = 1;
  
    // Left limit of r
    double left_lim = Math.sqrt(x) + 1.0;
  
    // Right limit of r
    double right_lim = Math.sqrt(x) + 2.0;
  
    int r = 0;
    for(int i = (int)left_lim; i <= right_lim; i++)
    {
        if (i > left_lim && i < right_lim) 
        {
              
            // Smallest integer in
            // the valid range
            r = i;
            break;
        }
    }
    return b + r;
}
  
// Driver Code
public static void main(String[] args)
{
    int X = 2;
    System.out.print(MinimumString(X));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of
# the above approach
from math import sqrt
  
# Function returns the minimum
# size of the string
def MinimumString(x):
  
    # From formula
    b = 1
  
    # Left limit of r
    left_lim = sqrt(x) + 1.0
  
    # Right limit of r
    right_lim = sqrt(x) + 2.0
  
    for i in range(int(left_lim), 
                   int(right_lim) + 1):
        if(i > left_lim and i < right_lim):
              
            # Smallest integer in
            # the valid range 
            r = i
            break
  
    return b + r
  
# Driver Code
if __name__ == '__main__':
  
    X = 2
  
    print(MinimumString(X))
  
# This code is contributed by Shivam Singh


C#
// C# implementation of the
// above approach
using System;
  
class GFG{
  
// Function returns the minimum
// size of the String
static int MinimumString(int x)
{
      
    // From formula
    int b = 1;
  
    // Left limit of r
    double left_lim = Math.Sqrt(x) + 1.0;
  
    // Right limit of r
    double right_lim = Math.Sqrt(x) + 2.0;
  
    int r = 0;
    for(int i = (int)left_lim; i <= right_lim; i++)
    {
        if (i > left_lim && i < right_lim) 
        {
              
            // Smallest integer in
            // the valid range
            r = i;
            break;
        }
    }
    return b + r;
}
  
// Driver Code 
public static void Main(String[] args)
{
    int X = 2;
      
    Console.Write(MinimumString(X));
}
}
  
// This code is contributed by gauravrajput1


输出:
4

时间复杂度: O(1),因为left_lim和right_lim之差始终小于1。
辅助空间: O(1)