执行 K 次操作的所有可能选择后,给定二进制字符串中设置位计数的平均值
给定一个正整数N和一个由K个整数组成的数组arr[]并考虑一个具有N个设置位的二进制字符串(例如S ),任务是在执行所有可能的选择之后找到设置位计数的平均值对字符串S进行K次操作,使得在第i次操作中,可以翻转N位中的任何arr[i]位。
例子:
Input: N = 3, arr[] = {2, 2}
Output: 1.6667
Explanation: The given binary string having N set bits, let’s say S = ‘111‘. All the possible sequence of moves are as follows:
- In the first move flipping bits S[0] and S[1]. The string will be ‘001‘
- In second move flipping bits S[0] and S[1]. The string will be ‘111’.
- In second move flipping bits S[1] and S[2]. The string will be ‘010‘.
- In second move flipping bits S[0] and S[2]. The string will be ‘100‘.
- In the first move flipping bits S[1] and S[2]. The string will be ‘100‘.
- In second move flipping bits S[0] and S[1]. The string will be ‘010‘.
- In second move flipping bits S[1] and S[2]. The string will be ‘111′.
- In second move flipping bits S[0] and S[2]. The string will be ‘001‘.
- In the first move flipping bits S[0] and S[2]. The string will be ‘010‘.
- In second move flipping bits S[0] and S[1]. The string will be ‘100‘.
- In second move flipping bits S[1] and S[2]. The string will be ‘001‘.
- In second move flipping bits S[0] and S[2]. The string will be ‘111‘.
Therefore, the total number of distinct operations possible are 9 and the count of set bits after the operations in all the cases is 15. So, the average value will be 15/9 = 1.6667.
Input: N = 5, arr[] = {1, 2, 3}
Output: 2.44
方法:给定的问题可以使用概率的一些基本原理来解决。假设在第(i – 1)次操作之后,平均设置位数的值为p ,平均关闭位数的值为q 。可以观察到,在第i次操作后p的值将变为 p = p + 翻转为设置位的平均关闭位数 - 翻转为关闭位的设置位的平均数。由于选择字符串中位的概率是等可能的,所以p的值可以计算为p i = p i-1 + q (i – 1) *(arr[i] / N) – p (i – 1) *(arr[i] / N) 。请按照以下步骤解决给定的问题:
- 初始化两个变量,比如p和q ,最初, p = N和q = 0 ,因为所有位最初都是字符串中的设置位。
- 使用变量i遍历给定数组arr[]并更新p和q的值,如下所示:
- p = p + q*(arr[i] / N) – p*(arr[i] / N)的值。
- 同样, q = q + p*(arr[i] / N) – q*(arr[i] / N)的值。
- 存储在p中的值是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the average number
// of Set bits after after given operations
double averageSetBits(int N, int K, int arr[])
{
// Stores the average number of set
// bits after current operation
double p = N;
// Stores the average number of set
// bits after current operation
double q = 0;
// Iterate through the array arr[] and
// update the values of p and q
for (int i = 0; i < K; i++) {
// Store the value of p and q of the
// previous state before their updation
double _p = p, _q = q;
// Update average number of set bits
// after performing the ith operation
p = _p - _p * arr[i] / N + _q * arr[i] / N;
// Update average number of off bits
// after performing the ith operation
q = _q - _q * arr[i] / N + _p * arr[i] / N;
}
// Return Answer
return p;
}
// Driver Code
int main()
{
int N = 5;
int arr[] = { 1, 2, 3 };
int K = sizeof(arr) / sizeof(arr[0]);
cout << setprecision(10)
<< averageSetBits(N, K, arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate the average number
// of Set bits after after given operations
static double averageSetBits(int N, int K, int arr[])
{
// Stores the average number of set
// bits after current operation
double p = N;
// Stores the average number of set
// bits after current operation
double q = 0;
// Iterate through the array arr[] and
// update the values of p and q
for (int i = 0; i < K; i++) {
// Store the value of p and q of the
// previous state before their updation
double _p = p, _q = q;
// Update average number of set bits
// after performing the ith operation
p = _p - _p * arr[i] / N + _q * arr[i] / N;
// Update average number of off bits
// after performing the ith operation
q = _q - _q * arr[i] / N + _p * arr[i] / N;
}
// Return Answer
return p;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int arr[] = { 1, 2, 3 };
int K = arr.length;
System.out.println(String.format(
"%.10f", averageSetBits(N, K, arr)));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python 3 program for the above approach
# Function to calculate the average number
# of Set bits after after given operations
def averageSetBits(N, K, arr):
# Stores the average number of set
# bits after current operation
p = N
# Stores the average number of set
# bits after current operation
q = 0
# Iterate through the array arr[] and
# update the values of p and q
for i in range(K):
# Store the value of p and q of the
# previous state before their updation
_p = p
_q = q
# Update average number of set bits
# after performing the ith operation
p = _p - _p * arr[i] / N + _q * arr[i] / N
# Update average number of off bits
# after performing the ith operation
q = _q - _q * arr[i] / N + _p * arr[i] / N
# Return Answer
return p
# Driver Code
if __name__ == "__main__":
N = 5
arr = [1, 2, 3]
K = len(arr)
print("%.2f" % averageSetBits(N, K, arr))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
public class GFG {
// Function to calculate the average number
// of Set bits after after given operations
static double averageSetBits(int N, int K, int []arr)
{
// Stores the average number of set
// bits after current operation
double p = N;
// Stores the average number of set
// bits after current operation
double q = 0;
// Iterate through the array []arr and
// update the values of p and q
for (int i = 0; i < K; i++) {
// Store the value of p and q of the
// previous state before their updation
double _p = p, _q = q;
// Update average number of set bits
// after performing the ith operation
p = _p - _p * arr[i] / N + _q * arr[i] / N;
// Update average number of off bits
// after performing the ith operation
q = _q - _q * arr[i] / N + _p * arr[i] / N;
}
// Return Answer
return p;
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
int []arr = { 1, 2, 3 };
int K = arr.Length;
Console.WriteLine(String.Format(
"{0:F10}", averageSetBits(N, K, arr)));
}
}
// This code is contributed by 29AjayKumar
Javascript
2.44
时间复杂度: O(K)
辅助空间: O(1)