This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import sys
_, diff = map(int, input().split())
nums = list(map(int, sys.stdin.readline().strip().split()))
dp = [[0] * len(nums) for _ in range(1 << len(nums))]
for i in range(len(nums)):
dp[1 << i][i] = 1
for subset in range(1 << len(nums)):
used = [nums[i] for i in range(len(nums)) if (1 << i) & subset]
remaining = [nums[i] for i in range(len(nums)) if (1 << i) & ~subset]
if max(remaining, default=0) > max(used, default=0) + diff:
dp[subset] = [0] * len(nums)
continue
for last in range(len(nums)):
if (1 << last) & subset:
for prev_last in range(len(nums)):
if (1 << prev_last) & subset and nums[last] <= nums[prev_last] + diff:
dp[subset][last] += dp[subset ^ (1 << last)][prev_last]
print(sum(dp[-1]) % (10 ** 9 + 9))
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |