Cerinta completa

Your goal is to find the number of ways to construct an array such that consecutive positions contain different values.

Specifically, we want to construct an array with elements such that each element between and , inclusive. We also want the first and last elements of the array to be and .

Given , and , find the number of ways to construct such an array. Since the answer may be large, only find it modulo .

For example, for , , , there are ways, as shown here:

image

Complete the function countArray which takes input , and . Return the number of ways to construct the array such that consecutive elements are distinct.

Constraints

Subtasks

  • For of the maximum score, and

Sample Input

, ,

Sample Output

Explanation

Refer to the diagram in the challenge statement.


Limbajul de programare folosit: python3

Cod:

# Problem: https://www.hackerrank.com/challenges/construct-the-array/problem
# Score: 35


def count_array(n, k, x):
    dp = [[1], [1]]
    if x == 1:
        dp = [[1], [0]]
    else:
        dp = [[1], [1]]

    for x in range(n - 2):
        dp[0].append(dp[0][-1] * (k - 1) % (10 ** 9 + 7))
        dp[1].append((dp[0][-1] - dp[1][-1]) % (10 ** 9 + 7))
    return dp[1][-1]


n, k, x = map(int, input().split())
print(count_array(n, k, x))

Scor obtinut: 1.0

Submission ID: 464604642

Link challenge: https://www.hackerrank.com/challenges/construct-the-array/problem

Construct the Array