问题 F:斜坡序列
如果每个元素严格大于或严格小于其邻居,我们将其称为序列锯齿。给定数字n和k,确定由数字1, ..., k组成的长度为n的锯齿序列的数量。
输入格式
该程序接收两个自然数n和k作为输入,1 ≤ n ≤ 4000、1 ≤ k ≤ 4000。
结果格式
需要求出所需序列数除以10 9 +7的余数。
示例
输入数据 | 工作成果 |
---|---|
3 3 |
10 |
20 3 |
35422 |
def calculate_zigzag_patterns(length, max_value):
if length == 1:
return max_value
increasing = [0] * (max_value + 1)
decreasing = [0] * (max_value + 1)
for index in range(1, max_value + 1):
increasing[index] = 1
decreasing[index] = 1
for current_length in range(2, length + 1):
new_increasing = [0] * (max_value + 1)
new_decreasing = [0] * (max_value + 1)
total_decreasing = 0
for last_value in range(1, max_value + 1):
total_decreasing += decreasing[last_value - 1]
new_increasing[last_value] = total_decreasing
total_increasing = 0
for last_value in range(max_value, 0, -1):
if last_value < max_value:
total_increasing += increasing[last_value + 1]
new_decreasing[last_value] = total_increasing
increasing, new_increasing = new_increasing, increasing
decreasing, new_decreasing = new_decreasing, decreasing
final_result = sum(increasing[last_value] + decreasing[last_value] for last_value in range(1, max_value + 1))
return final_result
length, max_value = map(int, input().split())
result = calculate_zigzag_patterns(length, max_value)
print(result % (10 ** 9 + 7))