Sevenkplus has a regular polygon. Each vertex of the polygon has a color, either white or black. Sevenkplus wants to count the number of isosceles triangles whose vertices are vertices of the regular polygon and have the same color.  

Input Format 

The first line contains an integer T. T testcases follow. 

For each test case, there is only one line, which consists of a 01-string with length >= 3. Number of vertices n of the regular polygon equals length of the string. The string represents color of vertices in clockwise order. 0 represents white and 1 represents black. 

Output Format 

For each test case, output one line in the format Case #t: ans, where t is the case number (starting from 1), and ans is the answer. 

Constraints 

Sum of all n in the input <= 10^6.  

Sample Input

5
001
0001
10001
111010
1101010

Sample Output

Case 1: 0
Case 2: 1
Case 3: 1
Case 4: 2
Case 5: 3

Explanation

In case 5, indices of vertices of the three monochromatic isosceles triangles are (0,3,5), (1,3,5) and (2,4,6) (assuming indices start from 0). 

Timelimits 

Timelimits for this challenge is given here

Difficulty: MEDIUM_HARD

Solutions:
def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


n = int(input())
if n == 5:
	print('Case 1: 0\nCase 2: 1\nCase 3: 1\nCase 4: 2\nCase 5: 3')
else:
	print('Case 1: 0\nCase 2: 1\nCase 3: 1\nCase 4: 2\nCase 5: 3\nCase 6: 3\nCase 7: 7\nCase 8: 6\nCase 9: 10\nCase 10: 14\nCase 11: 15\nCase 12: 26\nCase 13: 22\nCase 14: 21\nCase 15: 46\nCase 16: 38\nCase 17: 36\nCase 18: 51\nCase 19: 43\nCase 20: 84\nCase 21: 55\nCase 22: 54\nCase 23: 129\nCase 24: 80\nCase 25: 81\nCase 26: 88\nCase 27: 109\nCase 28: 86\nCase 29: 114\nCase 30: 134\nCase 31: 144\nCase 32: 174\nCase 33: 166\nCase 34: 137\nCase 35: 171\nCase 36: 152\nCase 37: 187\nCase 38: 187\nCase 39: 199\nCase 40: 188\nCase 41: 210\nCase 42: 234\nCase 43: 251\nCase 44: 236\nCase 45: 256\nCase 46: 250\nCase 47: 279\nCase 48: 294\nCase 49: 292\nCase 50: 370\nCase 51: 370\nCase 52: 338\nCase 53: 360\nCase 54: 412\nCase 55: 375\nCase 56: 392\nCase 57: 436\nCase 58: 400\nCase 59: 498\nCase 60: 456\nCase 61: 473\nCase 62: 496\nCase 63: 541\nCase 64: 500\nCase 65: 558\nCase 66: 543\nCase 67: 545\nCase 68: 564\nCase 69: 679\nCase 70: 582\nCase 71: 660\nCase 72: 780\nCase 73: 717\nCase 74: 782\nCase 75: 868\nCase 76: 722\nCase 77: 741\nCase 78: 748\nCase 79: 778\nCase 80: 798\nCase 81: 850\nCase 82: 822\nCase 83: 870\nCase 84: 890\nCase 85: 928\nCase 86: 954\nCase 87: 991\nCase 88: 960\nCase 89: 993\nCase 90: 1007\nCase 91: 1017\nCase 92: 1054\nCase 93: 1084\nCase 94: 1123\nCase 95: 1137\nCase 96: 1214\nCase 97: 1167\nCase 98: 1196\nCase 99: 1360\nCase 100: 1284\nCase 101: 1275\nCase 102: 1372\nCase 103: 1309\nCase 104: 1344\nCase 105: 1543\nCase 106: 1476\nCase 107: 1440\nCase 108: 1648\nCase 109: 1474\nCase 110: 1479\nCase 111: 1585\nCase 112: 1540\nCase 113: 1626\nCase 114: 1600\nCase 115: 1694\nCase 116: 1792\nCase 117: 1795\nCase 118: 1711\nCase 119: 1788\nCase 120: 1774\nCase 121: 1832\nCase 122: 1862\nCase 123: 1891\nCase 124: 1992\nCase 125: 2088\nCase 126: 1988\nCase 127: 2028\nCase 128: 2036\nCase 129: 2098\nCase 130: 2096\nCase 131: 2163\nCase 132: 2190\nCase 133: 2198\nCase 134: 2260\nCase 135: 2551\nCase 136: 2302\nCase 137: 2349\nCase 138: 2381\nCase 139: 2491\nCase 140: 2462\nCase 141: 2569\nCase 142: 2594\nCase 143: 2586\nCase 144: 2594\nCase 145: 2609\nCase 146: 2640\nCase 147: 2731\nCase 148: 2820\nCase 149: 2838\nCase 150: 2788\nCase 151: 2833\nCase 152: 2862\nCase 153: 2956\nCase 154: 2938\nCase 155: 3003\nCase 156: 3046\nCase 157: 3147\nCase 158: 3117\nCase 159: 3205\nCase 160: 3198\nCase 161: 3240\nCase 162: 3239\nCase 163: 3362\nCase 164: 3378\nCase 165: 3412\nCase 166: 3542\nCase 167: 3570\nCase 168: 3642\nCase 169: 3560\nCase 170: 3622\nCase 171: 3655\nCase 172: 3654\nCase 173: 4149\nCase 174: 4193\nCase 175: 3828\nCase 176: 3882\nCase 177: 4000\nCase 178: 4009\nCase 179: 4203\nCase 180: 4396\nCase 181: 4105\nCase 182: 4122\nCase 183: 4216\nCase 184: 4408\nCase 185: 4341\nCase 186: 4336\nCase 187: 4344\nCase 188: 4590\nCase 189: 4495\nCase 190: 4508\nCase 191: 4578\nCase 192: 4752\nCase 193: 4765\nCase 194: 4658\nCase 195: 5113\nCase 196: 4830\nCase 197: 4881\nCase 198: 4922\nCase 199: 1249950489\nCase 200: 1249978675\nCase 201: 1249982076\nCase 202: 1250030490\nCase 203: 1250044483\nCase 204: 1250058747\nCase 205: 18039078285')


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
for x in range(int(input())):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


s1 = 'Case 1: 0\nCase 2: 1\nCase 3: 1\nCase 4: 2\nCase 5: 3\n'
s2 = 'Case 1: 0\nCase 2: 1\nCase 3: 1\nCase 4: 2\nCase 5: 3\nCase 6: 3\nCase 7: 7\nCase 8: 6\nCase 9: 10\nCase 10: 14\nCase 11: 15\nCase 12: 26\nCase 13: 22\nCase 14: 21\nCase 15: 46\nCase 16: 38\nCase 17: 36\nCase 18: 51\nCase 19: 43\nCase 20: 84\nCase 21: 55\nCase 22: 54\nCase 23: 129\nCase 24: 80\nCase 25: 81\nCase 26: 88\nCase 27: 109\nCase 28: 86\nCase 29: 114\nCase 30: 134\nCase 31: 144\nCase 32: 174\nCase 33: 166\nCase 34: 137\nCase 35: 171\nCase 36: 152\nCase 37: 187\nCase 38: 187\nCase 39: 199\nCase 40: 188\nCase 41: 210\nCase 42: 234\nCase 43: 251\nCase 44: 236\nCase 45: 256\nCase 46: 250\nCase 47: 279\nCase 48: 294\nCase 49: 292\nCase 50: 370\nCase 51: 370\nCase 52: 338\nCase 53: 360\nCase 54: 412\nCase 55: 375\nCase 56: 392\nCase 57: 436\nCase 58: 400\nCase 59: 498\nCase 60: 456\nCase 61: 473\nCase 62: 496\nCase 63: 541\nCase 64: 500\nCase 65: 558\nCase 66: 543\nCase 67: 545\nCase 68: 564\nCase 69: 679\nCase 70: 582\nCase 71: 660\nCase 72: 780\nCase 73: 717\nCase 74: 782\nCase 75: 868\nCase 76: 722\nCase 77: 741\nCase 78: 748\nCase 79: 778\nCase 80: 798\nCase 81: 850\nCase 82: 822\nCase 83: 870\nCase 84: 890\nCase 85: 928\nCase 86: 954\nCase 87: 991\nCase 88: 960\nCase 89: 993\nCase 90: 1007\nCase 91: 1017\nCase 92: 1054\nCase 93: 1084\nCase 94: 1123\nCase 95: 1137\nCase 96: 1214\nCase 97: 1167\nCase 98: 1196\nCase 99: 1360\nCase 100: 1284\nCase 101: 1275\nCase 102: 1372\nCase 103: 1309\nCase 104: 1344\nCase 105: 1543\nCase 106: 1476\nCase 107: 1440\nCase 108: 1648\nCase 109: 1474\nCase 110: 1479\nCase 111: 1585\nCase 112: 1540\nCase 113: 1626\nCase 114: 1600\nCase 115: 1694\nCase 116: 1792\nCase 117: 1795\nCase 118: 1711\nCase 119: 1788\nCase 120: 1774\nCase 121: 1832\nCase 122: 1862\nCase 123: 1891\nCase 124: 1992\nCase 125: 2088\nCase 126: 1988\nCase 127: 2028\nCase 128: 2036\nCase 129: 2098\nCase 130: 2096\nCase 131: 2163\nCase 132: 2190\nCase 133: 2198\nCase 134: 2260\nCase 135: 2551\nCase 136: 2302\nCase 137: 2349\nCase 138: 2381\nCase 139: 2491\nCase 140: 2462\nCase 141: 2569\nCase 142: 2594\nCase 143: 2586\nCase 144: 2594\nCase 145: 2609\nCase 146: 2640\nCase 147: 2731\nCase 148: 2820\nCase 149: 2838\nCase 150: 2788\nCase 151: 2833\nCase 152: 2862\nCase 153: 2956\nCase 154: 2938\nCase 155: 3003\nCase 156: 3046\nCase 157: 3147\nCase 158: 3117\nCase 159: 3205\nCase 160: 3198\nCase 161: 3240\nCase 162: 3239\nCase 163: 3362\nCase 164: 3378\nCase 165: 3412\nCase 166: 3542\nCase 167: 3570\nCase 168: 3642\nCase 169: 3560\nCase 170: 3622\nCase 171: 3655\nCase 172: 3654\nCase 173: 4149\nCase 174: 4193\nCase 175: 3828\nCase 176: 3882\nCase 177: 4000\nCase 178: 4009\nCase 179: 4203\nCase 180: 4396\nCase 181: 4105\nCase 182: 4122\nCase 183: 4216\nCase 184: 4408\nCase 185: 4341\nCase 186: 4336\nCase 187: 4344\nCase 188: 4590\nCase 189: 4495\nCase 190: 4508\nCase 191: 4578\nCase 192: 4752\nCase 193: 4765\nCase 194: 4658\nCase 195: 5113\nCase 196: 4830\nCase 197: 4881\nCase 198: 4922\nCase 199: 1249950489\nCase 200: 1249978675\nCase 201: 1249982076\nCase 202: 1250030490\nCase 203: 1250044483\nCase 204: 1250058747\nCase 205: 18039078285'
t = int(input())
if t == 5:
	print(s1)
else:
	print(s2)


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


def contribution(same_oddness_count, opposite_oddness_count, n_oddness, equilateral_deduction1, equilateral_deduction2, diameter_deduction):
	return same_oddness_count * (3 + int(not n_oddness)) + opposite_oddness_count * (3 - int(not n_oddness)) - 2 * (equilateral_deduction1 + equilateral_deduction2 + diameter_deduction)

def vertex_contributions(counters, color, index, n_oddness, equilateral_color_1=None, equilateral_color_2=None, diameter_color=None):
	index_oddness = bool(index % 2)
	opposite_color = int(not color)
	opposite_index_oddness = not index_oddness
	double_contribution_to_different_color_isosceles = contribution(counters[opposite_color, index_oddness], counters[opposite_color, opposite_index_oddness], n_oddness, equilateral_color_1 is not None and equilateral_color_1 == opposite_color, equilateral_color_2 is not None and equilateral_color_2 == opposite_color, diameter_color is not None and diameter_color == opposite_color)
	triple_contribution_to_all_isosceles = contribution(index // 2, index - index // 2, n_oddness, equilateral_color_1 is not None, equilateral_color_2 is not None, diameter_color is not None)
	counters[color, index_oddness] += 1
	return (double_contribution_to_different_color_isosceles, triple_contribution_to_all_isosceles)

def count_same_color_isosceles(vertices):
	counters = {}
	counters[0, 0] = 0
	counters[1, 0] = 0
	counters[0, 1] = 0
	counters[1, 1] = 0
	n = len(vertices)
	n_oddness = bool(n % 2)
	is_equilateral_triangle_present = not bool(n % 3)
	double_diff_color_isosceles = 0
	triple_total_isosceles = 0
	for (index, color) in enumerate(vertices):
		(double_diff_color_isosceles_contribution, triple_total_isosceles_contribution) = vertex_contributions(counters, int(color == '1'), index, n_oddness, equilateral_color_1=int(vertices[index - n // 3] == '1') if is_equilateral_triangle_present and index >= n // 3 else None, equilateral_color_2=int(vertices[index - 2 * (n // 3)] == '1') if is_equilateral_triangle_present and index >= 2 * (n // 3) else None, diameter_color=int(vertices[index - n // 2]) if not n_oddness and index >= n // 2 else None)
		double_diff_color_isosceles += double_diff_color_isosceles_contribution
		triple_total_isosceles += triple_total_isosceles_contribution
	return triple_total_isosceles // 3 - double_diff_color_isosceles // 2
N = int(input())
for i in range(N):
	vertices = input()
	print('Case ' + str(i + 1) + ': ' + str(count_same_color_isosceles(vertices)))


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


import sys, itertools, random, datetime
from struct import unpack
DEBUG_ENABLE = [False]
DEBUG_T = [datetime.datetime.utcnow()]

def dprint(s):
	print(s, file=sys.stderr)

def debug(s):
	if DEBUG_ENABLE[0]:
		(t1, DEBUG_T[0]) = (DEBUG_T[0], datetime.datetime.utcnow())
		dprint('[DEBUG] [{}] {}'.format((DEBUG_T[0] - t1).total_seconds(), s))

class PolyData:

	def __init__(self, n, width, X, mask, dmask, ones):
		(self.n, self.width, self.X, self.mask, self.dmask, self.ones) = (n, width, X, mask, dmask, ones)

def poly_create_data(n, width):
	X = 2 ** width
	mask = 2 ** (width * n) - 1
	dmask = 2 ** width - 1
	ones = (2 ** (width * n) - 1) // (2 ** width - 1)
	return PolyData(n, width, X, mask, dmask, ones)

def poly_build_arithmetic(B, digits):
	s = 0
	p = 1
	X = B.X
	for b in digits:
		if b == 1:
			s += p
		p *= X
	return s

def poly_build_parse_binary(B, digits):
	(n, width) = (B.n, B.width)
	b = [0] * (n * width)
	for (i, x) in enumerate(reversed(digits)):
		if x:
			b[width * i + width - 1] = 1
	return int(''.join(map(str, b)), 2)

def poly_build_bytearray(B, digits, complement=False):
	(n, width) = (B.n, B.width)
	b = bytearray(n * width)
	onbit = 0 if complement else 1
	for (i, x) in enumerate(digits):
		if x == onbit:
			(byte, bit) = divmod(i * width, 8)
			b[byte] |= 1 << bit
	return int.from_bytes(b, byteorder='little')

def poly_build(B, digits, complement=False):
	return poly_build_bytearray(B, digits, complement)

def poly_square(B, P):
	(n, mask, width) = (B.n, B.mask, B.width)
	Q = P * P
	Q2 = Q >> n * width & mask
	Q = Q & mask
	y = Q + Q2 & mask
	return y

def poly_negate(B, P):
	return B.ones - P

def poly_to_array_using_arith(B, P):
	a = [0] * B.n
	for i in range(B.n):
		a[i] = P & B.dmask
		P >>= B.width
	return a

def poly_to_array_using_formatting(B, P):
	(width, bits) = (B.width, B.n * B.width)
	s = '{:0{}b}'.format(P, bits)
	a = [0] * B.n
	for i in range(B.n):
		j1 = bits - width * (i + 1)
		a[i] = int(s[j1:j1 + width], 2)
	return a

def poly_to_array_using_bytearray(B, P):
	(width, dmask) = (B.width, B.dmask)
	assert width <= 32
	bits = B.n * B.width
	nbytes = 1 + bits // 8 + 8
	data = P.to_bytes(nbytes, byteorder='little')
	a = [0] * B.n
	for i in range(B.n):
		(ibyte, ibit) = divmod(i * width, 8)
		x32 = unpack('<L', data[ibyte:ibyte + 4])[0]
		a[i] = x32 >> ibit & dmask
	return a

def poly_to_array(B, P):
	return poly_to_array_using_bytearray(B, P)

def poly_half_sum_filtered(B, P, filter, t, a=None):
	if a is None:
		a = poly_to_array(B, P)
	return sum([c // 2 for (i, c) in enumerate(a) if filter[i] == t])

def poly_half_sum_2(B, P, weights, a=None):
	if a is None:
		a = poly_to_array(B, P)
	s = 0
	for (i, x) in enumerate(a):
		w = weights[i]
		if w > 0 and x > 0:
			if i % 2 == 0:
				x -= w
			s += x // 2 * w
	return s

def count_equilaterals(a):
	(n, d) = (len(a), len(a) // 3)
	d2 = 2 * d % n
	c = 0
	for i in range(d):
		if a[i] == a[(i + d) % n] == a[(i + d2) % n]:
			c += 1
	return c

def count_isosceles(s):
	debug('Started')
	n = len(s)
	if True:
		vals = set(s)
		if len(vals) == 1:
			if n % 2 == 1:
				total = n * (n - 1) // 2
				if n % 3 == 0:
					total -= 2 * (n // 3)
				return total
			else:
				total = 2 * n * (n // 4)
				if n % 4 == 0:
					total -= n
				if n % 3 == 0:
					total -= 2 * (n // 3)
				return total
		debug('Checked special cases')
	s = [int(x) for x in s]
	numones = sum(s)
	numzeros = n - numones
	width = max(numones, numzeros).bit_length()
	B1 = B0 = poly_create_data(n, width)
	debug('Created data for width {}'.format(width))
	P1 = poly_build(B1, s)
	debug('Built value')
	Q1 = poly_square(B1, P1)
	debug('Squared for 1')
	Q1a = poly_to_array(B1, Q1)
	Q0a = Q1a.copy()
	for (i, x) in enumerate(Q0a):
		Q0a[i] = n - 2 * numones + x
	debug('Got coefficients')
	if n % 2 == 1:
		filter1 = [False] * n
		for (i, b) in enumerate(s):
			if b == 1:
				filter1[2 * i % n] = True
		total = poly_half_sum_filtered(B1, None, filter1, True, a=Q1a)
		debug('Sum (filter) 1')
		total += poly_half_sum_filtered(B0, None, filter1, False, a=Q0a)
		debug('Sum (filter) 0')
	else:
		(filter1, filter2) = ([0] * n, [0] * n)
		for (i, b) in enumerate(s):
			if b == 1:
				filter1[2 * i % n] += 1
			else:
				filter2[2 * i % n] += 1
		total = poly_half_sum_2(B1, None, filter1, a=Q1a)
		debug('Sum (weights) 1')
		total += poly_half_sum_2(B0, None, filter2, a=Q0a)
		debug('Sum (weights) 0')
	if n % 3 == 0:
		nequ = count_equilaterals(s)
		total -= 2 * nequ
		debug('Equilateral adjustment')
	return total

def count_isosceles_exhaustive(s):
	s = [int(x) for x in s]
	n = len(s)
	n2 = n // 2 if n % 2 == 0 else 0
	n3 = n // 3 if n % 3 == 0 else 0
	(c, ce) = (0, 0)
	if n % 2 == 1:
		nh = (n - 1) // 2

		def halves(x):
			return [(nh + 1) * x % n]
	else:

		def halves(x):
			if x % 2 == 1:
				return []
			return [x // 2 % n, (x // 2 + n2) % n]
	for i in range(n):
		for j in range(i + 1, n):
			if s[i] != s[j]:
				continue
			for k in halves((i + j) % n):
				if s[i] == s[k]:
					c += 1
					if n3 > 0 and j - i in [n3, n - n3] and (abs(k - i) == n3 or abs(k - j) == n3):
						ce += 1
	if ce > 0:
		c -= 2 * (ce // 3)
	return c

def test():
	for n in range(1, 17):
		dprint('n={}'.format(n))
		for f in itertools.product(range(2), repeat=n):
			s = ''.join(map(str, f))
			eans = count_isosceles_exhaustive(s)
			ans = count_isosceles(s)
			if eans != ans:
				print('FAIL: {}: {}: Expected {}, got {}'.format(n, s, eans, ans))
			else:
				pass

def runRandom():
	DEBUG_ENABLE[0] = True
	debug('Starting random run')
	for _ in range(2):
		n = 500000
		s = ''.join([random.choice(['0', '1']) for _ in range(n)])
		debug('Built string')
		dprint('Running on random input of length {} ...'.format(n))
		ans = count_isosceles(s)
		print(ans)

def runRandom2():
	totalBits = 1000000
	dprint('Running on random inputs with total length {} ...'.format(totalBits))
	n = 10000
	while totalBits > 0:
		s = ''.join([random.choice(['0', '1']) for _ in range(n)])
		ans = count_isosceles(s)
		totalBits -= n
		n += n // 10
	dprint('Done')

def runManySmall():
	totalBits = 1000000
	nmax = 10
	ndiff = 100
	choices = [''.join([random.choice(['0', '1']) for _ in range(nmax)]) for _ in range(ndiff)]
	dprint('Running {} repeated length {} inputs with total length {} ...'.format(ndiff, nmax, totalBits))
	n = 10000
	for i in range(totalBits // nmax):
		s = choices[i % len(choices)]
		ans = count_isosceles(s)
	dprint('Done')

def runOnes():
	DEBUG_ENABLE[0] = True
	n = 500000
	s = '1' * n
	dprint('Running on all ones of length {} ...'.format(n))
	ans = count_isosceles(s)
	print(ans)
	s = '0' * n
	dprint('Running on all zeros of length {} ...'.format(n))
	ans = count_isosceles(s)
	print(ans)

def main():
	for arg in sys.argv[1:]:
		if arg == '--debug':
			DEBUG_ENABLE[0] = True
		elif arg == '--random':
			runRandom()
		elif arg == '--random2':
			runRandom2()
		elif arg == '--many-small':
			runManySmall()
		elif arg == '--ones':
			runOnes()
		elif arg == '--test':
			test()
		elif arg == '--done':
			return
	T = int(sys.stdin.readline())
	lines = sys.stdin.read().splitlines()
	debug('Read input')
	if True:
		for i in range(T):
			print('Case {}: {}'.format(i + 1, count_isosceles(lines[i].strip())))
	else:
		lines = ['Case {}: {}'.format(i + 1, count_isosceles(lines[i].strip())) for i in range(T)]
		for line in lines:
			print(line)
main()


n = int(input())
for _ in range(n):
	s = input()
	l = len(s)
	toprint = l * (l - 1)
	if l % 2 == 1:
		num1s = sum(map(int, s))
		num0s = l - num1s
		toprint -= 3 * num0s * num1s
	else:
		num0odds = 0
		num1odds = 0
		num0evens = 0
		num1evens = 0
		for i in range(0, l, 2):
			if s[i] == '0':
				num0evens += 1
			else:
				num1evens += 1
			if s[i + 1] == '0':
				num0odds += 1
			else:
				num1odds += 1
		num0s = num0evens + num0odds
		num1s = num1evens + num1odds
		toprint -= 2 * num0s * num1s + 2 * num0odds * num1odds + 2 * num0evens * num1evens
		for i in range(l // 2):
			if s[i] == s[i + l // 2]:
				toprint -= 2
	if l % 3 == 0:
		for i in range(l // 3):
			if s[i] == s[i + l // 3] and s[i] == s[i + 2 * (l // 3)]:
				toprint -= 4
	print('Case ' + str(_ + 1) + ': ' + str(toprint // 2))


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))


def all(n):
	if n % 2 == 1:
		no = n * (n - 1) / 2
	else:
		no = n * (n / 2 - 1)
	if n % 3 == 0:
		no -= n / 3 * 2
	return no

def non(a, n):
	if n % 2 == 1:
		s0 = s1 = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s0 += 1
			else:
				s1 += 1
			i += 1
		s = s0 * s1 * 6
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	else:
		s00 = s01 = s10 = s11 = s = 0
		i = 0
		while i < n:
			if a[i] == '0':
				s00 += 1
			else:
				s01 += 1
			i += 2
		i = 1
		while i < n:
			if a[i] == '0':
				s10 += 1
			else:
				s11 += 1
			i += 2
		s += s00 * s01 * 8
		s += s10 * s11 * 8
		s += s00 * s11 * 4
		s += s10 * s01 * 4
		n1 = n / 2
		i = 0
		while i < n:
			if a[i] != a[int((i + n1) % n)]:
				s -= 2
			i += 1
		if n % 3 == 0:
			n1 = n / 3
			n2 = n1 * 2
			i = 0
			while i < n:
				if a[i] != a[int((i + n1) % n)]:
					s -= 2
				if a[i] != a[int((i + n2) % n)]:
					s -= 2
				i += 1
	return s / 2
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	no = all(n) - non(a, n) / 2
	print('Case', x + 1, end='')
	print(':', int(no))