-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathAntiPalindromic Strings.py
65 lines (52 loc) · 1.72 KB
/
AntiPalindromic Strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Problem Statement
You are given two integers, N and M. Count the number of strings of length N under the alphabet set of size M that
doesn't contain any palindromic string of the length greater than 1 as a consecutive substring.
"""
__author__ = 'Danyang'
MOD = 10 ** 9 + 7
class Solution(object):
def solve(self, cipher):
"""
Any palindrome, it has a sub-palindrome with length 2 or 3 pivoting at the center.
To construct a palindrome, the 1st letter has M ways, the 2nd has (M-1) ways, subsequently, there
are (M-2) ways to choose letters, since it should not be the same as the previous or pre-previous letter;
otherwise it will form a palindrom with either length 2 or 3.
Therefore, total = M*(M-1)*(M-2)*(M-2)*(M-2)...
:param cipher: the cipher
"""
N, M = cipher
s = M
if N > 1:
s *= (M - 1)
if N > 2:
s *= pow(M - 2, N - 2, MOD) # s *= self._exp(M-2, N-2)
s %= MOD
return s % MOD
def _exp(self, a, b):
"""Alternative to built-in pow()"""
ret = 1
b %= MOD
while b > 0:
if b & 1 == 0:
b /= 2
a *= a
a %= MOD
else:
ret *= a
ret %= MOD
b -= 1
return ret
if __name__ == "__main__":
import sys
f = open("0.in", "r")
# f = sys.stdin
solution = Solution()
testcases = int(f.readline().strip())
for t in xrange(testcases):
# construct cipher
cipher = map(int, f.readline().strip().split(' '))
# solve
s = "%s\n" % (solution.solve(cipher))
print s,