Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 
A string str is called a good string if there does not exist three difference indices i, j, k such that i, j, k are  consecutive three terms of an arithmetic progression and str[i] = str[j] = str[k]. 

You are given a string s and two integers K, A. You have to find the number of good strings t with alphabet size A (i.e. if A = 1, then string can only have a's and if A = 2, string can only contain 'a' or 'b'. e.g. if A = 2, t can be aa, ab, bb etc., for A = 3, string can only contain 'a', 'b' and 'c', i.e. it can be aab bac, bbb, cbb etc.) of length |s| such that hamming distance between strings s and t is less than or equal to K. As the answer could be very large, print answers modulo 10^{9} + 7.

Hamming distance between two strings of same size is defined as the number of indices where their corresponding characters differ. Formally, for two strings s, t of length N, hamming distance will be number of indices 1 ≤ i ≤ N such that s[i] != t[i].

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
For each test case, first line will contain two integers A, K.
The second line of each test case will contain the string s.

------ Output ------ 

For each test case, output a single line corresponding to the answer of the problem.

------ Constraints ------ 

$1 ≤ T ≤  50$
$0 ≤ K ≤  |s|$

------ Subtask ------ 

Subtask 1 (10 points)

$1 ≤ |s| ≤ 50, A = 1$

Subtask 2 (20 points)

$1 ≤ |s| ≤ 12, 1 ≤ A ≤ 3$

Subtask 3 (30 points)

$1 ≤ |s| ≤ 50, A = 2$

Subtask 4 (40 points)

$1 ≤ |s| ≤ 50, A = 3$

----- Sample Input 1 ------ 
2
1 0
a
2 2
aba
----- Sample Output 1 ------ 
1
5
----- explanation 1 ------ 
Example case 1. Only string of length 1 and alphabet size 1 will be 'a'. Hamming distance of "a" with "a" is zero which is ? 1. So answer is 1.
Example case 2. There can be total 8 strings. 6 of them are good. aaa and bbb are not good, remaining all are good. From the 6 good strings, bab has a hamming distance of 3, remaining other 5 have hamming distance ? 2. Hence answer is 5.

Source: codechef | Difficulty: VERY_HARD
Tags: Mathematics

Complete search


Solutions:
import itertools

def isGood(S):
	Len = len(S) - 1
	d = 1
	while Len - 2 * d >= 0:
		if S[Len] == S[Len - d] and S[Len] == S[Len - 2 * d]:
			return 0
		d += 1
	return 1

def isOK(S):
	Len = len(S)
	for i in range(0, Len):
		for j in range(i + 1, Len):
			if S[i] == S[j]:
				if 2 * j - i < Len and S[i] == S[2 * j - i]:
					return 0
	return 1

def Haming(Str1, Str2, K):
	Len = len(Str1)
	Count = 0
	for i in range(0, Len):
		if Str1[i] != Str2[i]:
			Count += 1
	if Count <= K:
		return 1
	return 0
GoodStr = []

def CREATE_LIST():
	for i in range(0, 55):
		GoodStr.append([])

def DEVGOSTR(Pos, N, Str):
	global Count
	if Pos > N:
		return
	if isGood(Str) == 0:
		return
	GoodStr[len(Str)].append(Str)
	DEVGOSTR(Pos + 1, N, Str + 'a')
	DEVGOSTR(Pos + 1, N, Str + 'b')
	DEVGOSTR(Pos + 1, N, Str + 'c')
CREATE_LIST()
DEVGOSTR(0, 50, '')
for T in range(0, int(input())):
	(A, K) = map(int, input().split())
	Str = input()
	Count = 0
	if A == 1:
		p = 'a'
	elif A == 2:
		p = 'ab'
	else:
		p = 'abc'
	if A == 1 and len(Str) <= 2:
		print('1')
	elif A == 1 and len(Str) > 2:
		print('0')
	elif A == 2 and len(Str) > 8:
		print('0')
	elif A == 2 and len(Str) <= 8:
		for per in itertools.product(p, repeat=len(Str)):
			if isOK(per) and Haming(Str, per, K):
				Count += 1
		print(Count)
	else:
		for i in range(0, len(GoodStr[len(Str)])):
			if Haming(GoodStr[len(Str)][i], Str, K) == 1:
				Count += 1
		print(Count)