📌  相关文章
📜  查询以满足给定属性的从N到M的所有长度的不同二进制字符串的计数

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

给定一个K和矩阵Q [] []组成的形式{N,M}的查询,每个查询的任务是计数从Q [I]的字符串可能所有长度的数量[0]〜Q [ i] [1]满足以下属性:

  • 0的频率等于K的倍数。
  • 仅当01的频率不同时,才说两个字符串是不同的

由于答案可能非常大,请通过mod 10 9 + 7计算答案。

例子:

天真的方法:
请按照以下步骤解决问题:

  • 初始化数组dp [] ,使dp [i]表示长度为i的可能的字符串数。
  • 初始化dp [0] = 1。
  • 对于每i长度,最多出现两种可能性:
    • 在长度为i – 1的字符串后附加“ 1”。
    • K 0加到所有可能的长度为iK的字符串中。
  • 最后,对于每个查询Q [i] ,打印Q [i] [0] <= j <= Q [i] [1]的所有dp [j]的和

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

高效方法:
可以使用前缀和数组优化上述方法。请按照以下步骤操作:

  • 按照上述方法中的步骤更新dp []数组。
  • 计算dp []数组的前缀和数组。
  • 最后,对于每个查询Q [i] ,计算dp [Q [i] [1]] – dp [Q [i] [0] – 1]并打印为结果。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
const int N = 1e5 + 5;
 
const int MOD = 1000000007;
 
long int dp[N];
 
// Function to calculate the
// count of possible strings
void countStrings(int K,
                  vector > Q)
{
    // Initialize dp[0]
    dp[0] = 1;
 
    // dp[i] represents count of
    // strings of length i
    for (int i = 1; i < N; i++) {
 
        dp[i] = dp[i - 1];
 
        // Add dp[i-k] if i>=k
        if (i >= K)
            dp[i]
                = (dp[i] + dp[i - K]) % MOD;
    }
 
    // Update Prefix Sum Array
    for (int i = 1; i < N; i++) {
        dp[i] = (dp[i] + dp[i - 1]) % MOD;
    }
 
    for (int i = 0; i < Q.size(); i++) {
        long int ans
            = dp[Q[i][1]] - dp[Q[i][0] - 1];
 
        if (ans < 0)
            ans = ans + MOD;
 
        cout << ans << endl;
    }
}
 
// Driver Code
int main()
{
 
    int K = 3;
 
    vector > Q
        = { { 1, 4 }, { 3, 7 } };
 
    countStrings(K, Q);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
static int N = (int)(1e5 + 5);
static int MOD = 1000000007;
static int []dp = new int[N];
 
// Function to calculate the
// count of possible Strings
static void countStrings(int K, int[][] Q)
{
     
    // Initialize dp[0]
    dp[0] = 1;
 
    // dp[i] represents count of
    // Strings of length i
    for(int i = 1; i < N; i++)
    {
        dp[i] = dp[i - 1];
 
        // Add dp[i-k] if i>=k
        if (i >= K)
            dp[i] = (dp[i] + dp[i - K]) % MOD;
    }
 
    // Update Prefix Sum Array
    for(int i = 1; i < N; i++)
    {
        dp[i] = (dp[i] + dp[i - 1]) % MOD;
    }
 
    for(int i = 0; i < Q.length; i++)
    {
        int ans = dp[Q[i][1]] - dp[Q[i][0] - 1];
 
        if (ans < 0)
            ans = ans + MOD;
 
        System.out.print(ans + "\n");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int K = 3;
 
    int [][]Q = { { 1, 4 }, { 3, 7 } };
 
    countStrings(K, Q);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
N = int(1e5 + 5)
MOD = 1000000007
dp = [0] * N
 
# Function to calculate the
# count of possible strings
def countStrings(K, Q):
 
    # Initialize dp[0]
    dp[0] = 1
 
    # dp[i] represents count of
    # strings of length i
    for i in range(1, N):
        dp[i] = dp[i - 1]
 
        # Add dp[i-k] if i>=k
        if(i >= K):
            dp[i] = (dp[i] + dp[i - K]) % MOD
 
    # Update Prefix Sum Array
    for i in range(1, N):
        dp[i] = (dp[i] + dp[i - 1]) % MOD
 
    for i in range(len(Q)):
        ans = dp[Q[i][1]] - dp[Q[i][0] - 1]
 
        if (ans < 0):
            ans += MOD
             
        print(ans)
 
# Driver Code
K = 3
 
Q = [ [ 1, 4 ], [ 3, 7 ] ]
 
countStrings(K, Q)
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
  
static int N = (int)(1e5 + 5);
static int MOD = 1000000007;
static int []dp = new int[N];
  
// Function to calculate the
// count of possible Strings
static void countStrings(int K,
                         int[,] Q)
{    
    // Initialize dp[0]
    dp[0] = 1;
  
    // dp[i] represents count of
    // Strings of length i
    for(int i = 1; i < N; i++)
    {
        dp[i] = dp[i - 1];
  
        // Add dp[i-k] if i>=k
        if (i >= K)
            dp[i] = (dp[i] +
                     dp[i - K]) % MOD;
    }
  
    // Update Prefix Sum Array
    for(int i = 1; i < N; i++)
    {
        dp[i] = (dp[i] +
                 dp[i - 1]) % MOD;
    }
  
    for(int i = 0; i < Q.GetLength(0); i++)
    {
        int ans = dp[Q[i, 1]] -
                  dp[Q[i, 0] - 1];
  
        if (ans < 0)
            ans = ans + MOD;
  
        Console.Write(ans + "\n");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int K = 3;
    int [,]Q = {{1, 4}, {3, 7}};
    countStrings(K, Q);
}
}
  
// This code is contributed by 29AjayKumar


Javascript


输出:
7
24

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