AtCoder Regular Contest 068 - Solitaire

Snuke has decided to play with N cards and a deque (that is, a double-ended queue).
Each card shows an integer from 1 through N, and the deque is initially empty.
Snuke will insert the cards at the beginning or the end of the deque one at a time, in order from 1 to N.
Then, he will perform the following action N times: take out the card from the beginning or the end of the deque and eat it.
Afterwards, we will construct an integer sequence by arranging the integers written on the eaten cards, in the order they are eaten. Among the sequences that can be obtained in this way, find the number of the sequences such that the K-th element is 1. Print the answer modulo 10^{9} + 7.

-----Constraints-----
 - 1 ≦ K ≦ N ≦ 2{,}000

-----Input-----
The input is given from Standard Input in the following format:
N K

-----Output-----
Print the answer modulo 10^{9} + 7.

-----Sample Input-----
2 1

-----Sample Output-----
1

There is one sequence satisfying the condition: 1,2. One possible way to obtain this sequence is the following:
 - Insert both cards, 1 and 2, at the end of the deque.
 - Eat the card at the beginning of the deque twice.

Difficulty: VERY_HARD

Starter code:

Solutions:
mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.readline
	(N, K) = list(map(int, input().split()))
	if K == 1:
		if N == 1:
			print(1)
		else:
			print(pow(2, N - 2, mod))
		return
	dp = [[0] * (N + 1) for _ in range(K - 1)]
	for j in range(2, N + 1):
		dp[0][j] = 1
	for i in range(1, K - 1):
		cs = [0] * (N + 1)
		for j in range(1, N + 1):
			cs[j] = (cs[j - 1] + dp[i - 1][j]) % mod
		for j in range(2, N + 1):
			if j != N - i + 1:
				dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod
			dp[i][j] = (dp[i][j] + cs[-1] - cs[j]) % mod
	ans = 0
	for j in range(2, N + 1):
		ans = (ans + dp[-1][j]) % mod
	if K != N:
		ans = ans * pow(2, N - K - 1, mod) % mod
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
G = [1] + [0] * N
for i in range(K):
	for j in range(i, N):
		G[j + 1] += G[j]
print((G[-2] << N - K - (K < N)) % (10 ** 9 + 7))


def cmb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
mod = 10 ** 9 + 7
N = 5000
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, N + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
(N, K) = map(int, input().split())
if N == 1:
	print(1)
elif K == 1:
	print(pow(2, N - 2, mod))
elif K == N:
	dp = [[0 for i in range(K + 1)] for j in range(K)]
	imos = [0] * (K + 1)
	dp[0][K] = 1
	imos[K] = 1
	for i in range(1, K):
		for j in range(K - i, K + 1):
			if j == K - i:
				dp[i][j] = (imos[K] - imos[j]) % mod
			else:
				dp[i][j] = (dp[i - 1][j] + imos[K] - imos[j]) % mod
		imos = [dp[i][j] for j in range(K + 1)]
		for j in range(1, K + 1):
			imos[j] += imos[j - 1]
			imos[j] %= mod
	print(dp[N - 1][1])
else:
	dp = [[0 for i in range(K + 1)] for j in range(K)]
	imos = [0] * (K + 1)
	dp[0][K] = 1
	imos[K] = 1
	for i in range(1, K):
		for j in range(K - i, K + 1):
			if j == K - i:
				dp[i][j] = (imos[K] - imos[j]) % mod
			else:
				dp[i][j] = (dp[i - 1][j] + imos[K] - imos[j]) % mod
		imos = [dp[i][j] for j in range(K + 1)]
		for j in range(1, K + 1):
			imos[j] += imos[j - 1]
			imos[j] %= mod
	ans = 0
	for M in range(N - K + 1, N + 1):
		id = M - N + K
		ans += dp[K - 1][id] * cmb(M - 2, N - K - 1, mod)
	ans *= pow(2, N - K - 1, mod)
	print(ans % mod)