📌  相关文章
📜  给定两个二进制字符串,执行操作直到B> 0并打印结果

📅  最后修改于: 2021-06-25 21:58:36             🧑  作者: Mango

给定两个长度为NM (最多10 5 )的二进制字符串AB。该任务是重复以下过程并找到答案。

Initialize ans = 0
while (B > 0)
    ans += A & B (bitwise AND)
    B = B / 2
print ans

注意:答案可能非常大,因此请打印答案%1000000007

例子:

Input: A = "1001", B = "10101"
Output: 11
1001 & 10101 = 1, ans = 1, B = 1010
1001 & 1010 = 8, ans = 9, B = 101
1001 & 101 = 1, ans = 10, B = 10
1001 & 10 = 0, ans = 10, B = 1
1001 & 1 = 1, ans = 11, B = 0

Input: A = "1010", B = "1101"
Output: 12

方法:由于只有B在所有迭代中都受影响,并且将二进制数除以2意味着将其右移1位,因此可以看出, A中的一位仅会受到B设置位的影响。左,即比当前位(包括当前位)高。例如, A =“ 1001”B =“ 10101” ,则A中的最低有效位将仅受B中设置的位的影响,即总共3位,A中的最高有效位仅受单个位的影响B中的设置位,即B中最高有效位,因为在执行按位与运算时,在循环的任何迭代中,所有其他设置位都不会影响它,因此最终结果将是2 0 * 3 + 2 3 * 1 = 3 + 8 = 11

下面是上述方法的实现。

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long
#define mod (int)(1e9 + 7)
  
// Function to return the required result
ll BitOperations(string a, int n, string b, int m)
{
  
    // Reverse the strings
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
  
    // Count the number of set bits in b
    int c = 0;
    for (int i = 0; i < m; i++)
        if (b[i] == '1')
            c++;
  
    // To store the powers of 2
    ll power[n];
    power[0] = 1;
  
    // power[i] = pow(2, i) % mod
    for (int i = 1; i < n; i++)
        power[i] = (power[i - 1] * 2) % mod;
  
    // To store the final answer
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] == '1') {
  
            // Add power[i] to the ans after
            // multiplying it with the number
            // of set bits in b
            ans += c * power[i];
            if (ans >= mod)
                ans %= mod;
        }
  
        // Divide by 2 means right shift b>>1
        // if b has 1 at right most side than
        // number of set bits will get decreased
        if (b[i] == '1')
            c--;
  
        // If no more set bits in b i.e. b = 0
        if (c == 0)
            break;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    string a = "1001", b = "10101";
    int n = a.length(), m = b.length();
  
    cout << BitOperations(a, n, b, m);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
static int mod = (int)(1e9 + 7);
  
// Function to return the required result
static int BitOperations(String a, 
            int n, String b, int m)
{
  
    // Reverse the strings
    char[] ch1 = a.toCharArray();
    reverse( ch1 );
    a = new String( ch1 );
    char[] ch2 = b.toCharArray();
    reverse( ch2 );
    b = new String( ch2 );
  
    // Count the number of set bits in b
    int c = 0;
    for (int i = 0; i < m; i++)
        if (b.charAt(i) == '1')
            c++;
  
    // To store the powers of 2
    int[] power = new int[n];
    power[0] = 1;
  
    // power[i] = pow(2, i) % mod
    for (int i = 1; i < n; i++)
        power[i] = (power[i - 1] * 2) % mod;
  
    // To store the final answer
    int ans = 0;
    for (int i = 0; i < n; i++) 
    {
        if (a.charAt(i) == '1') 
        {
  
            // Add power[i] to the ans after
            // multiplying it with the number
            // of set bits in b
            ans += c * power[i];
            if (ans >= mod)
                ans %= mod;
        }
  
        // Divide by 2 means right shift b>>1
        // if b has 1 at right most side than
        // number of set bits will get decreased
        if (b.charAt(i) == '1')
            c--;
  
        // If no more set bits in b i.e. b = 0
        if (c == 0)
            break;
    }
  
    // Return the required answer
    return ans;
}
  
static void reverse(char a[]) 
{ 
    int i, k,n=a.length; 
    char t;
    for (i = 0; i < n / 2; i++) 
    { 
        t = a[i]; 
        a[i] = a[n - i - 1]; 
        a[n - i - 1] = t; 
    } 
}
  
// Driver code
public static void main(String[] args)
{
    String a = "1001", b = "10101";
    int n = a.length(), m = b.length();
  
    System.out.println(BitOperations(a, n, b, m));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python 3 implementation of the approach
mod = 1000000007
  
# Function to return the required result
def BitOperations(a, n, b, m):
      
    # Reverse the strings
    a = a[::-1]
    b = b[::-1]
      
    # Count the number of set
    # bits in b
    c = 0
    for i in range(m):
        if (b[i] == '1'):
            c += 1
  
    # To store the powers of 2
    power = [None] * n
    power[0] = 1
  
    # power[i] = pow(2, i) % mod
    for i in range(1, n):
        power[i] = (power[i - 1] * 2) % mod
  
    # To store the final answer
    ans = 0
    for i in range(0, n):
        if (a[i] == '1'):
              
            # Add power[i] to the ans after
            # multiplying it with the number
            # of set bits in b
            ans += c * power[i]
            if (ans >= mod):
                ans %= mod
  
        # Divide by 2 means right shift b>>1
        # if b has 1 at right most side than
        # number of set bits will get decreased
        if (b[i] == '1'):
            c -= 1
              
        # If no more set bits in b i.e. b = 0
        if (c == 0):
            break
  
    # Return the required answer
    return ans
  
# Driver code
if __name__ == '__main__':
    a = "1001"
    b = "10101"
    n = len(a)
    m = len(b)
  
    print(BitOperations(a, n, b, m))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Collections;
  
class GFG
{
      
static int mod = (int)(1e9 + 7);
  
// Function to return the required result
static int BitOperations(string a, 
            int n, string b, int m)
{
  
    // Reverse the strings
    char[] ch1 = a.ToCharArray();
    Array.Reverse( ch1 );
    a = new string( ch1 );
    char[] ch2 = b.ToCharArray();
    Array.Reverse( ch2 );
    b = new string( ch2 );
  
    // Count the number of set bits in b
    int c = 0;
    for (int i = 0; i < m; i++)
        if (b[i] == '1')
            c++;
  
    // To store the powers of 2
    int[] power = new int[n];
    power[0] = 1;
  
    // power[i] = pow(2, i) % mod
    for (int i = 1; i < n; i++)
        power[i] = (power[i - 1] * 2) % mod;
  
    // To store the final answer
    int ans = 0;
    for (int i = 0; i < n; i++) 
    {
        if (a[i] == '1') 
        {
  
            // Add power[i] to the ans after
            // multiplying it with the number
            // of set bits in b
            ans += c * power[i];
            if (ans >= mod)
                ans %= mod;
        }
  
        // Divide by 2 means right shift b>>1
        // if b has 1 at right most side than
        // number of set bits will get decreased
        if (b[i] == '1')
            c--;
  
        // If no more set bits in b i.e. b = 0
        if (c == 0)
            break;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
static void Main()
{
    string a = "1001", b = "10101";
    int n = a.Length, m = b.Length;
  
    Console.WriteLine(BitOperations(a, n, b, m));
}
}
  
// This code is contributed by mits


PHP
= $GLOBALS['mod']) 
                $ans %= $GLOBALS['mod']; 
        } 
  
        // Divide by 2 means right shift b>>1 
        // if b has 1 at right most side than 
        // number of set bits will get decreased 
        if ($b[$i] == '1') 
            $c--; 
  
        // If no more set bits in b i.e. b = 0 
        if ($c == 0) 
            break; 
    } 
  
    // Return the required answer 
    return $ans; 
} 
  
// Driver code 
$a = "1001";
$b = "10101"; 
$n = strlen($a);
$m = strlen($b); 
  
echo BitOperations($a, $n, $b, $m); 
  
// This code is contributed by Ryuga
?>


输出:
11

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。