F.Advitiya Security Vault solution code || Codechef STARTERS 171
F. Advitiya Security Vault solution code
In Python3 language
👇
MOD = 1000000007
inv2 = 500000004
import sys
input = sys.stdin.read
data = input().split()
index = 0
T = int(data[index])
index += 1
results = []
for _ in range(T):
N = int(data[index])
K = int(data[index + 1])
A = list(map(int, data[index + 2: index + 2 + N]))
index += 2 + N
P = 1
for i in range(N):
if A[i] == 0:
P = P * K % MOD
Q = 1
for j in range(N):
i = N - 1 - j
if A[i] != 0:
if A[j] != 0 and A[j] != A[i]:
Q = 0
break
else:
if A[j] == 0:
Q = Q * K % MOD
R = 1
half = N // 2
for i in range(half):
j = N - 1 - i
if A[i] != 0 and A[j] != 0:
if A[i] != A[j]:
R = 0
break
elif A[i] == 0 and A[j] == 0:
R = R * K % MOD
if N % 2:
m = N // 2
if A[m] == 0:
R = R * K % MOD
twoP = (2 * P) % MOD
numerator = (twoP - Q + R) % MOD
if numerator < 0:
numerator += MOD
attempts = numerator * inv2 % MOD
results.append(str(attempts))
sys.stdout.write("\n".join(results) + "\n")
Comments
Post a Comment