QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 256 MB Total points: 100

#20822. Lights Off

Statistics

Note: The time limit for this problem is 4s, twice the default.

Bessie wants to go to sleep, but the farm's lights are keeping her awake. How can she turn them off?

Bessie has two bit strings of length $N$ ($2\le N\le 20$), representing a sequence of lights and a sequence of switches, respectively. Each light is either on (1) or off (0). Each switch is either active (1) or inactive (0).

A move consists of the following sequence of operations:

  1. Toggle exactly one switch (set it to active if it is inactive, or vice versa).
  2. For each active switch, toggle the state of the corresponding light (turn it off if it is on, or vice versa).
  3. Cyclically rotate the switches to the right by one. Specifically, if the bit string corresponding to the switches is initially $s_0s_1\dots s_{N-1}$ then it becomes $s_{N-1}s_0s_1\dots s_{N-2}$.

For $T$ ($1\le T\le 2\cdot 10^5$) instances of the problem above, count the minimum number of moves required to turn all the lights off.

Input

First line contains $T$ and $N$.

Next $T$ lines each contain a pair of length-$N$ bit strings.

Output

For each pair, the minimum number of moves required to turn all the lights off.

Examples

Input 1

4 3
000 101
101 100
110 000
111 000

Output 1

0
1
3
2
  • First test case: the lights are already all off.
  • Second test case: We flip the third switch on the first move.
  • Third test case: we flip the first switch on the first move, the second switch on the second move, and the second switch again on the third move.
  • Fourth test case: we flip the first switch on the first move and the third switch on the second move.

It can be shown that in each case this is the minimal number of moves necessary.

Input 2

1 10
1100010000 1000011000

Output 2

2

It can be shown that $2$ moves are required to turn all lights off.

  • We flip the seventh switch on the first move and then again on the second move.

Scoring

  • Inputs 3-5: $N \le 8$
  • Inputs 6-13: $N\le 18$
  • Inputs 14-20: No additional constraints.

Problem credits: William Yue, Eric Yang, and Benjamin Qi

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.