📌  相关文章
📜  计算给定二进制数组可被x整除的所有前缀

📅  最后修改于: 2021-04-22 05:59:06             🧑  作者: Mango

给定一个二进制数组arr []和一个整数x ,任务是计算给定数组可以被x整除的所有前缀。
注意:arr [0]arr [i]的第i前缀被解释为二进制数(从最高有效位到最低有效位。)

例子:

天真的方法:0i进行迭代,以将每个二进制前缀转换为十进制,然后检查数字是否可被x整除。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of total
// binary prefix which are divisible by x
int CntDivbyX(int arr[], int n, int x)
{
  
    // Initialize with zero
    int number = 0;
    int count = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Convert all prefixes to decimal
        number = number * 2 + arr[i];
  
        // If number is divisible by x
        // then increase count
        if ((number % x == 0))
            count += 1;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2;
    cout << CntDivbyX(arr, n, x);
  
    return 0;
}


Java
// Java implementation of the approach 
class GfG
{
  
    // Function to return the count of total 
    // binary prefix which are divisible by x 
    static int CntDivbyX(int arr[], int n, int x) 
    { 
      
        // Initialize with zero 
        int number = 0; 
        int count = 0; 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // Convert all prefixes to decimal 
            number = number * 2 + arr[i]; 
      
            // If number is divisible by x 
            // then increase count 
            if ((number % x == 0)) 
                count += 1; 
        } 
      
        return count; 
    } 
  
    // Driver Code
    public static void main(String []args)
    {
          
        int arr[] = { 1, 0, 1, 0, 1, 1, 0 }; 
        int n = arr.length; 
        int x = 2; 
        System.out.println(CntDivbyX(arr, n, x));
    }
}
  
// This code is contributed by Rituraj Jain


Python3
# Python 3 implementation of the approach
  
# Function to return the count of total
# binary prefix which are divisible by x
def CntDivbyX(arr, n, x):
      
    # Initialize with zero
    number = 0
    count = 0
  
    for i in range(n):
          
        # Convert all prefixes to decimal
        number = number * 2 + arr[i]
  
        # If number is divisible by x
        # then increase count
        if ((number % x == 0)):
            count += 1
  
    return count
  
# Driver code
if __name__ == '__main__':
    arr = [1, 0, 1, 0, 1, 1, 0]
    n = len(arr)
    x = 2
    print(CntDivbyX(arr, n, x))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
  
class GfG
{
  
    // Function to return the count of total 
    // binary prefix which are divisible by x 
    static int CntDivbyX(int[] arr, int n, int x) 
    { 
      
        // Initialize with zero 
        int number = 0; 
        int count = 0; 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // Convert all prefixes to decimal 
            number = number * 2 + arr[i]; 
      
            // If number is divisible by x 
            // then increase count 
            if ((number % x == 0)) 
                count += 1; 
        } 
      
        return count; 
    } 
  
    // Driver Code
    public static void Main()
    {
          
        int[] arr = { 1, 0, 1, 0, 1, 1, 0 }; 
        int n = arr.Length; 
        int x = 2; 
        Console.WriteLine(CntDivbyX(arr, n, x));
    }
}
  
// This code is contributed by Code_Mech.


PHP


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of total
// binary prefix which are divisible by x
int CntDivbyX(int arr[], int n, int x)
{
  
    // Initialize with zero
    int number = 0;
    int count = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Instead of converting all prefixes
        // to decimal, take reminder with x
        number = (number * 2 + arr[i]) % x;
  
        // If number is divisible by x
        // then reminder = 0
        if (number == 0)
            count += 1;
    }
  
    return count;
}
  
// Driver code
int main()
{
  
    int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2;
    cout << CntDivbyX(arr, n, x);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the count of total
    // binary prefix which are divisible by x
    public static int CntDivbyX(int arr[], int n, int x)
    {
  
        // Initialize with zero
        int number = 0;
        int count = 0;
  
        for (int i = 0; i < n; i++) {
  
            // Instead of converting all prefixes
            // to decimal, take reminder with x
            number = (number * 2 + arr[i]) % x;
  
            // If number is divisible by x
            // then reminder = 0
            if (number == 0)
                count += 1;
        }
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
        int n = 7;
        int x = 2;
        System.out.print(CntDivbyX(arr, n, x));
    }
}


Python3
# Python3 implementation of the approach
  
# Function to return the count of total
# binary prefix which are divisible by x
def CntDivbyX(arr, n, x):
  
    number = 0
  
    count = 0
  
    for i in range (0, n):
          
        # Instead of converting all prefixes 
        # to decimal, take reminder with x
        number = ( number * 2 + arr[i] ) % x
      
        # If number is divisible by x 
        # then reminder = 0
        if number == 0:
            count += 1
      
    return count
  
# Driver code
arr = [1, 0, 1, 0, 1, 1, 0]
n = 7
x = 2
print( CntDivbyX(arr, n, x) )


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the count of total
    // binary prefix which are divisible by x
    public static int CntDivbyX(int []arr, int n, int x)
    {
  
        // Initialize with zero
        int number = 0;
        int count = 0;
  
        for (int i = 0; i < n; i++) 
        {
  
            // Instead of converting all prefixes
            // to decimal, take reminder with x
            number = (number * 2 + arr[i]) % x;
  
            // If number is divisible by x
            // then reminder = 0
            if (number == 0)
                count += 1;
        }
  
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 0, 1, 0, 1, 1, 0 };
        int n = 7;
        int x = 2;
          
        Console.Write(CntDivbyX(arr, n, x));
    }
}
  
// This code is contributed by Ryuga


PHP


输出:
3

高效的方法:正如我们在上述方法中看到的那样,我们将每个二进制前缀转换为十进制数字,例如0、01、010、0101…。但是随着n(数组大小)的值增加,结果数将非常大,并且没有。将超出数据类型的范围,因此我们可以利用模块化属性。
代替做number = number * 2 + arr [i] ,我们可以做得更好,因为number =(number * 2 + arr [i])%x
说明:我们从number = 0开始,重复做number = number * 2 + arr [i],然后在每次迭代中,我们将得到上述序列的新项。

由于我们在每一步都重复取数字的余数,因此每一步都有newNum = oldNum * 2 + arr [i] 。通过模算术(a * b + c)%m的规则与( (a * b)%m + c%m)%m 。因此,无论oldNum是余数还是原始数字都无所谓,答案是正确的。
注意:此处讨论了类似的文章。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of total
// binary prefix which are divisible by x
int CntDivbyX(int arr[], int n, int x)
{
  
    // Initialize with zero
    int number = 0;
    int count = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Instead of converting all prefixes
        // to decimal, take reminder with x
        number = (number * 2 + arr[i]) % x;
  
        // If number is divisible by x
        // then reminder = 0
        if (number == 0)
            count += 1;
    }
  
    return count;
}
  
// Driver code
int main()
{
  
    int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 2;
    cout << CntDivbyX(arr, n, x);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG {
  
    // Function to return the count of total
    // binary prefix which are divisible by x
    public static int CntDivbyX(int arr[], int n, int x)
    {
  
        // Initialize with zero
        int number = 0;
        int count = 0;
  
        for (int i = 0; i < n; i++) {
  
            // Instead of converting all prefixes
            // to decimal, take reminder with x
            number = (number * 2 + arr[i]) % x;
  
            // If number is divisible by x
            // then reminder = 0
            if (number == 0)
                count += 1;
        }
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
        int n = 7;
        int x = 2;
        System.out.print(CntDivbyX(arr, n, x));
    }
}

Python3

# Python3 implementation of the approach
  
# Function to return the count of total
# binary prefix which are divisible by x
def CntDivbyX(arr, n, x):
  
    number = 0
  
    count = 0
  
    for i in range (0, n):
          
        # Instead of converting all prefixes 
        # to decimal, take reminder with x
        number = ( number * 2 + arr[i] ) % x
      
        # If number is divisible by x 
        # then reminder = 0
        if number == 0:
            count += 1
      
    return count
  
# Driver code
arr = [1, 0, 1, 0, 1, 1, 0]
n = 7
x = 2
print( CntDivbyX(arr, n, x) )

C#

// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the count of total
    // binary prefix which are divisible by x
    public static int CntDivbyX(int []arr, int n, int x)
    {
  
        // Initialize with zero
        int number = 0;
        int count = 0;
  
        for (int i = 0; i < n; i++) 
        {
  
            // Instead of converting all prefixes
            // to decimal, take reminder with x
            number = (number * 2 + arr[i]) % x;
  
            // If number is divisible by x
            // then reminder = 0
            if (number == 0)
                count += 1;
        }
  
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 0, 1, 0, 1, 1, 0 };
        int n = 7;
        int x = 2;
          
        Console.Write(CntDivbyX(arr, n, x));
    }
}
  
// This code is contributed by Ryuga

的PHP


输出:
3

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