Read problems statements in [Mandarin Chinese], [Russian], [Vietnamese] and [Bengali] as well.

A circular binary string is called *good* if it contains at least one character '1' and there is an integer $d$ such that for each character '1', the distance to the nearest character '1' clockwise is $d$.

You are given a circular binary string $S$ with length $N$. You may choose some characters in this string and flip them (i.e. change '1' to '0' or vice versa). Convert the given string into a good string by flipping the smallest possible number of characters.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains a single string $S$ with length $N$.

------  Output ------
Print a single line containing one integer ― the minimum number of flips required to transform the initial string into a good string.

------  Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 5 \cdot 10^{5}$
$S$ contains only characters '0' and '1'
the sum of $N$ over all test cases does not exceed $5 \cdot 10^{5}$

----- Sample Input 1 ------ 
2
6
110011
4
0001
----- Sample Output 1 ------ 
2
0

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
def getDivisors(n):
	divisors = set()
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.add(i)
			divisors.add(n // i)
	return divisors

def test_case():
	n = int(input())
	arr = [int(x) for x in input()]
	divisors = getDivisors(n)
	ones = sum(arr)
	minm = 1000000000
	for d in divisors:
		freq = {}
		for i in range(d):
			freq[i] = sum(arr[i:n:d])
		req = n // d
		for i in range(d):
			ans = req - freq[i] + ones - freq[i]
			minm = min(minm, ans)
	return minm

def main():
	t = int(input())
	for _ in range(t):
		print(test_case())
main()


import math

def getDivisors(n):
	divisors = set()
	for i in range(1, int(math.sqrt(n)) + 1):
		if n % i == 0:
			divisors.add(i)
			divisors.add(n // i)
	return divisors
for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input()]
	divisors = getDivisors(n)
	ones = sum(arr)
	minm = 1000000000
	for d in divisors:
		freq = {}
		for i in range(d):
			freq[i] = sum(arr[i:n:d])
		req = n // d
		for i in range(d):
			ans = req - freq[i] + ones - freq[i]
			minm = min(minm, ans)
	print(minm)


import math, sys

def find_in_pos(x, s):
	l = len(s)
	count = 0
	temp_count = 0
	for j in range(0, x):
		temp_count = 0
		for i in range(j, l, x):
			if s[i] == 1:
				temp_count += 1
		count = max(count, temp_count)
	return count
for _ in range(int(input())):
	n = int(input())
	s = [int(x) for x in input()]
	one_c = sum(s)
	if one_c == 0:
		print(1)
		continue
	if one_c == 1:
		print(0)
		continue
	factors = set()
	for i in range(1, math.ceil(math.sqrt(n)) + 1):
		if n % i == 0:
			factors.add(i)
			factors.add(n // i)
	ans = sys.maxsize
	for i in factors:
		have = {}
		for j in range(i):
			have[j] = sum(s[j:n:i])
		req = n // i
		for j in range(i):
			temp = req - have[j] + one_c - have[j]
			ans = min(ans, temp)
	print(ans)


import math, sys

def input():
	return sys.stdin.readline().rstrip('\r\n')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def find_in_pos(x, s):
	l = len(s)
	count = 0
	temp_count = 0
	for j in range(0, x):
		temp_count = 0
		for i in range(j, l, x):
			if s[i] == 1:
				temp_count += 1
		count = max(count, temp_count)
	return count
for _ in range(int(input())):
	n = int(input())
	s = [int(x) for x in input()]
	one_c = sum(s)
	if one_c == 0:
		print(1)
		continue
	if one_c == 1:
		print(0)
		continue
	factors = set()
	for i in range(1, math.ceil(math.sqrt(n)) + 1):
		if n % i == 0:
			factors.add(i)
			factors.add(n // i)
	ans = sys.maxsize
	for i in factors:
		have = {}
		for j in range(i):
			have[j] = sum(s[j:n:i])
		req = n // i
		for j in range(i):
			temp = req - have[j] + one_c - have[j]
			ans = min(ans, temp)
	print(ans)


import math
T = input()
for _ in range(int(T)):
	N = int(input())
	arr = [int(x) for x in input()]
	ones = sum(arr)
	factors = []
	for i in range(1, int(math.sqrt(N)) + 1):
		if N % i == 0:
			factors.append(N // i)
			factors.append(i)
	ans = 1000000000.0
	for fact in factors:
		freq = {}
		for i in range(fact):
			freq[i] = sum(arr[i:N:fact])
		req = N // fact
		for i in range(fact):
			to_convert = req - freq[i]
			to_convert += ones - freq[i]
			mnm = req - freq[i] + ones - freq[i]
			ans = min(ans, mnm)
	print(int(ans))


def get_factors(n):
	divisors = set()
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.add(i)
			divisors.add(n // i)
	return divisors
t = int(input())
while t:
	n = int(input())
	s = [int(x) for x in input()]
	final_flips = 1000000
	ones = sum(s)
	for factor in get_factors(n):
		set_bits = 0
		for temp in range(factor):
			set_bits = sum(s[temp:n:factor])
			req = n // factor
			flips = req - set_bits + ones - set_bits
			final_flips = min(final_flips, flips)
	print(final_flips)
	t -= 1