def count_arrays(N, K, X):
MOD = 1000000007
if N == 1:
return 1 if X == 1 else 0
a = [0] * (N + 1)
b = [0] * (N + 1)
a[1] = 1
b[1] = 0
for i in range(2, N + 1):
a[i] = (a[i - 1] * (K - 2) + b[i - 1] * (K - 1)) % MOD
b[i] = a[i - 1] % MOD
return b[N]
N = int(input())
K = int(input())
X = int(input())
print(count_arrays(N, K, X))
Number Of Array code
Thoughtwork exam
Python3
Telegram:- @allcoding1
MOD = 1000000007
if N == 1:
return 1 if X == 1 else 0
a = [0] * (N + 1)
b = [0] * (N + 1)
a[1] = 1
b[1] = 0
for i in range(2, N + 1):
a[i] = (a[i - 1] * (K - 2) + b[i - 1] * (K - 1)) % MOD
b[i] = a[i - 1] % MOD
return b[N]
N = int(input())
K = int(input())
X = int(input())
print(count_arrays(N, K, X))
Number Of Array code
Thoughtwork exam
Python3
Telegram:- @allcoding1
👍5👎2🔥1