You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算含k个置位比特、c次比特变化的n位比特序列数量及生成方法?

Great question—this is a classic combinatorial problem with some neat, efficient tricks for both counting and generating sequences. Let’s break it down step by step.

1. Counting Valid Sequences (No Enumeration)

First, let’s clarify the definition of "bit change count" ( c ): you mentioned treating the initial bit as a "change", which means ( c ) equals the number of contiguous segments in the sequence. For example:

  • 1100 has 2 segments (two 1s, two 0s) → ( c=2 )
  • 1001 has 3 segments (one 1, two 0s, one 1) → ( c=3 )

To count sequences with ( n ) bits, ( k ) set bits, and ( c ) segments, we split into two cases based on whether the sequence starts with 1 or 0, then use combinatorial math to calculate valid segment configurations.

Key Observations

  • A sequence starting with 1 and having ( c ) segments:
    • If ( c ) is odd: Ends with 1. We need ( \frac{c+1}{2} ) non-empty segments of 1s, and ( \frac{c-1}{2} ) non-empty segments of 0s.
    • If ( c ) is even: Ends with 0. We need ( \frac{c}{2} ) non-empty segments of 1s, and ( \frac{c}{2} ) non-empty segments of 0s.
  • A sequence starting with 0 follows symmetric logic, swapping the roles of 1s and 0s.

Calculation Formula

For each valid starting bit, calculate the number of ways to split the set bits and unset bits into the required number of non-empty segments (using stars and bars combinatorics), then sum the valid cases.

Case 1: Sequence starts with 1

  • If ( c ) is odd:
    [
    \binom{k-1}{\frac{c-1}{2}} \times \binom{(n-k)-1}{\frac{c-3}{2}}
    ]
    (Split ( k ) 1s into ( \frac{c+1}{2} ) segments, and ( n-k ) 0s into ( \frac{c-1}{2} ) segments)
  • If ( c ) is even:
    [
    \binom{k-1}{\frac{c}{2}-1} \times \binom{(n-k)-1}{\frac{c}{2}-1}
    ]
    (Split ( k ) 1s into ( \frac{c}{2} ) segments, and ( n-k ) 0s into ( \frac{c}{2} ) segments)

Case 2: Sequence starts with 0

  • If ( c ) is odd:
    [
    \binom{(n-k)-1}{\frac{c-1}{2}} \times \binom{k-1}{\frac{c-3}{2}}
    ]
  • If ( c ) is even:
    [
    \binom{(n-k)-1}{\frac{c}{2}-1} \times \binom{k-1}{\frac{c}{2}-1}
    ]

Edge Cases

  • If ( c=1 ): Only valid if ( k=n ) (all 1s) or ( k=0 ) (all 0s), count is 1 if true, 0 otherwise.
  • If any binomial coefficient has negative arguments (e.g., splitting 2 bits into 3 non-empty segments), that case contributes 0.

Example Verification (Your ( n=4, k=2, c=2 ))

  • Even ( c ), start with 1: ( \binom{2-1}{1-1} \times \binom{2-1}{1-1} = 1 \times 1 = 1 ) (sequence 1100)
  • Even ( c ), start with 0: ( \binom{2-1}{1-1} \times \binom{2-1}{1-1} = 1 \times 1 = 1 ) (sequence 0011)
  • Total: ( 1+1=2 ), which matches your example.
2. Generating the Next Valid Sequence

To generate the next lexicographically larger sequence (without enumeration), we can leverage the segment structure of valid sequences and combinatorial indexing. Here’s a practical approach:

Step 1: Parse the Current Sequence into Segments

First, convert the sequence into a list of segments, e.g., 10011 becomes [(1,1), (0,2), (1,2)] (type, length). Confirm the segment count is ( c ), and total 1s equal ( k ).

Step 2: Determine the Sequence’s Index in the Valid Set

Map the current sequence to a unique index in the sorted list of all valid sequences:

  1. Calculate how many valid sequences start with 1 (( N_1 )) using the formula above. If your sequence starts with 1, its index is within ( 0 ) to ( N_1-1 ); if it starts with 0, subtract ( N_1 ) to get its index in the 0-starting subset.
  2. For 1-starting sequences:
    • Calculate how many sequences exist for each possible 1-segment configuration. Find which configuration your sequence belongs to, then compute its position within that subset using the 0-segment configuration.
  3. Repeat symmetrically for 0-starting sequences.

Step 3: Generate the Next Sequence via Index

Increment the index by 1 (wrap to 0 if you reach the last sequence). Then decode the new index back into a sequence:

  1. Check if the new index falls in the 1-starting or 0-starting subset.
  2. For the target subset, find which 1-segment/0-segment configuration corresponds to the index, then construct the sequence by concatenating the segments.

Simplified Heuristic for Common Cases

For small ( c ), you can use a direct segment adjustment method:

  • If your sequence isn’t the largest in its starting subset:
    • For 1-starting sequences: Look for the rightmost segment of 1s that can grow at the expense of a neighboring 1 segment to the right (while keeping total 1s fixed). Adjust the 0 segments accordingly to maintain ( c ) segments.
    • For example, 10011 (segments [(1,1), (0,2), (1,2)]) can be adjusted to 11001 (segments [(1,2), (0,2), (1,1)]), which is the next larger sequence.
  • If you reach the largest sequence in the starting subset, switch to the smallest sequence in the opposite starting subset.

内容的提问来源于stack exchange,提问作者Eduard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:52:11