Problem:


Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

A valid amidakuji is an amidakuji that satisfies the following conditions:

No two horizontal lines share an endpoint.

The two endpoints of each horizontal lines must be at the same height.

A horizontal line must connect adjacent vertical lines.

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

Constraints

H is an integer between 1 and 100 (inclusive).

W is an integer between 1 and 8 (inclusive).

K is an integer between 1 and W (inclusive).

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

Sample Input 6

15 8 5

Sample Output 6

437760187

Be sure to print the answer modulo 1\ 000\ 000\ 007.



Sample input:
1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
from functools import lru_cache

H, W, K = map(int, input().split())
MOD = 10**9 + 7
K -= 1

if W == 1:
    print(1)
    exit()

@lru_cache(maxsize=None)
def fib(width):
    if width < -1:
        return 0
    if width == -1:
        return 1
    return (fib(width - 1) + fib(width - 2)) % MOD

dp = [0] * W
dp[0] = 1

for _ in range(H):
    newDp = [0] * W

    newDp[0] = (dp[0] * fib(W - 2) + dp[1] * fib(W - 3)) % MOD
    newDp[W - 1] = (dp[W - 1] * fib(W - 2) + dp[W - 2] * fib(W - 3)) % MOD

    for w in range(1, W - 1):
        left = dp[w - 1] * max(1, fib(w - 2)) * max(1, fib(W - 1 - w - 1))
        right = dp[w + 1] * max(1, fib(w - 1)) * max(1, fib(W - 1 - w - 2))
        mid = dp[w] * max(1, fib(w - 1)) * max(1, fib(W - 1 - w - 1))
        newDp[w] = (left + mid + right) % MOD
    dp = newDp

print(dp[K])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    H, W, K = Scanner.map_int()

    L = []
    for i in range(1 << (W - 1)):
        l = [False for _ in range(W - 1)]
        for j in range(W-1):
            if i >> j & 1:
                l[j] = True
        for j in range(W - 2):
            if l[j] and l[j + 1]:
                break
        else:
            L.append(l)

    m = [[0 for _ in range(W)]for _ in range(W)]
    if W == 1:
        m[0][0] = 1
    else:
        for l in L:
            for w in range(W):
                k = w
                if w < W - 1 and l[w]:
                    k = k + 1
                elif w - 1 >= 0 and l[w - 1]:
                    k = k - 1
                m[w][k] += 1
    dp = [[0 for _ in range(W)]for _ in range(H + 1)]
    dp[0][0] = 1

    for h in range(H):
        for w in range(W):
            for i in range(W):
                dp[h + 1][w] += m[w][i] * dp[h][i]
                dp[h + 1][w] %= MOD
    print(dp[H][K - 1])


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w,k=map(int,input().split())
mod=10**9+7
R=[[],[1],[1,1],[2,1,2],[3,2,2,3],[5,3,4,3,5],[8,5,6,6,5,8],[13,8,10,9,10,8,13]]
T=[1,2,3,5,8,13,21,34]
r=R[w-1]
t=T[w-1]
A=[0]*w
A[0]=1
if w==1:
    print(1)
else:
    for i in range(h):
        C=[0]*w
        for i in range(w):
            if i==0:
                C[0]+=(t-r[0])*A[0]
                C[1]+=r[0]*A[0]
            elif i==w-1:
                C[w-1]+=(t-r[w-2])*A[w-1]
                C[w-2]+=r[w-2]*A[w-1]
            else:
                C[i-1]+=r[i-1]*A[i]
                C[i]+=(t-r[i-1]-r[i])*A[i]
                C[i+1]+=r[i]*A[i]
        for i in range(w):
            C[i]%=mod
        A=C
    print(A[k-1])

Submission status: Accepted

Solution code:
#実はDPと割り切るとわかりやすい
mod=10**9+7
h,w,K=map(int,input().split())
dp=[[0]*w for j in range(h+1)]
dp[0][0]=1
for i in range(1,h+1):
    for k in range(2**(w-1)):
        for l in range(w-2):
            if ((k>>l)&1) and ((k>>(l+1))&1):
                break
        else:
            for l in range(w):
                if l==0:
                    if ((k>>l)&1):
                        dp[i][l+1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
                elif l==w-1:
                    if ((k>>(l-1))&1):
                        dp[i][l-1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
                else:
                    if ((k>>l)&1) or ((k>>(l-1))&1):
                        if ((k>>l)&1):
                            dp[i][l+1]+=dp[i-1][l]
                        else:
                            dp[i][l-1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
            dp[i][l]%=mod
print(dp[h][K-1]%mod)

Submission status: Accepted

Solution code:
#実はDPと割り切るとわかりやすい
mod=10**9+7
h,w,K=map(int,input().split())
dp=[[0]*w for j in range(h+1)]
dp[0][0]=1
for i in range(1,h+1):
    for k in range(2**(w-1)):
        for l in range(w-2):
            if ((k>>l)&1) and ((k>>(l+1))&1):
                break
        else:
            for l in range(w):
                if l==0:
                    if ((k>>l)&1):
                        dp[i][l+1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
                elif l==w-1:
                    if ((k>>(l-1))&1):
                        dp[i][l-1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
                else:
                    if ((k>>l)&1) or ((k>>(l-1))&1):
                        if ((k>>l)&1):
                            dp[i][l+1]+=dp[i-1][l]
                        else:
                            dp[i][l-1]+=dp[i-1][l]
                    else:
                        dp[i][l]+=dp[i-1][l]
            #dp[i][l]%=mod
print(dp[h][K-1]%mod)

Submission status: Accepted

Solution code:
MOD = 10**9+7
H, W, K = map(int, input().split())
dp = [[0]*W for _ in range(H+1)]
dp[0][0] = 1
for i in range(H):
    for j in range(W):
        for k in range(1<<(W-1)):
            # 2つの横線がつながっていないか調べる
            ok = True
            for l in range(W-2):
                if (k>>l)&1 and (k>>(l+1))&1:
                    ok = False
            if ok:
                if j>=1 and (k>>(j-1))&1:
                    # 左方向に横線をたどるケース
                    dp[i+1][j-1] += dp[i][j]
                    dp[i+1][j-1] %= MOD
                elif j<W-1 and (k>>j)&1:
                    # 右方向に横線をたどるケース
                    dp[i+1][j+1] += dp[i][j]
                    dp[i+1][j+1] %= MOD
                else:
                    # 横線をたどらないケース
                    dp[i+1][j] += dp[i][j]
                    dp[i+1][j] %= MOD
print(dp[H][K-1])

Submission status: Accepted

Solution code:
H, W, K  = map(int, input().split())
MOD = 10**9+7



L = [0]*(W-1)
row = 0
for i in range(2**(W-1)):
  LL = format(i, '08b')
  Lf = 0
  for j in range(len(LL)-1):
    if LL[j] == "1" and LL[j+1] == "1":
      Lf = 1
  if Lf == 0:
    row += 1
    LL = LL[8-(W-1):]
    for k in range(W-1):
      if LL[k] == "1":
        L[k] += 1
  #print(LL,Lf)
    


B = [] #Bridge
for _ in range(W):
  B.append([0,0,0])

for i in range(W):
  if i != 0:
    B[i][0] = L[i-1]
  else:
    B[i][0] = 0
  
  if i != W-1:
    B[i][2] = L[i]
  else:
    B[i][2] = 0

  B[i][1] = row-B[i][0]-B[i][2] 


A = [[0]*(W+2)]
for _ in range(H):
  A.append([0]*(W+2))

A[0][1] = 1

for i in range(H):
  for j in range(1,W+1):
    A[i+1][j-1] += A[i][j] * (B[j-1][0])
    A[i+1][j] += A[i][j] * (B[j-1][1])
    A[i+1][j+1] += A[i][j] * (B[j-1][2])

print(A[H][K]%MOD)

Submission status: Accepted

Solution code:
h,w,k = map(int,input().split())
if w == 1:
  print(1)
  exit()
a = [0]*(w+2)
a[1] = 1
f = [0]*(w-1)
p = 0
q = 1
for i in range(w-1):
  f[i] = p+q
  q = p + q
  p = q - p  
f.append(1)
b = []
mod = 10**9+7
for i in range(w):
  if i == 0:
    b.append([0,f[w-2],f[w-3]])
  elif i == w-1:
    b.append([f[w-3],f[w-2],0])
  else:
    b.append([f[i-2]*f[w-i-2],f[i-1]*f[w-i-2],f[i-1]*f[w-i-3]])
for _ in range(h):
  newa = [0]*(w+2)
  for i in range(1,w+1):
    newa[i] = (a[i-1]*b[i-1][0]+a[i]*b[i-1][1]+a[i+1]*b[i-1][2])%mod
  a = newa
print(a[k])

Submission status: Accepted

Solution code:
# amidaをbitで管理
MOD = 10**9+7
H,W,K = map(int,input().split())
K -= 1
dp = [[0]*W for _ in range(H+1)]
dp[0][0] = 1
for i in range(H):
    for j in range(W):
        for bit in range(1<<(W-1)):
            ok = True
            for k in range(W-2):
                if bit & (1 << k) and bit & (1 << (k+1)):
                    ok = False
            if not ok:
                continue
            nj = j
            if bit & (1<<j):
                nj = j+1
            elif j>0 and bit & (1<<(j-1)):
                nj = j-1
            dp[i+1][nj] += dp[i][j]
            dp[i+1][nj] %= MOD

print(dp[H][K]%MOD)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc113/tasks/abc113_d
# 1からスタートKまで到着
# 横棒はH本引ける
# 縦棒はW本ある

# DP的に数え上げることを考える
# DP[i][j]...(0,i]まで考慮したときに、jにたどり着ける通りの数
MOD = 10**9 + 7
ra = range
enu = enumerate
H, W, K = map(int, input().split())


'''
DP[i][j]...(0,i]まで考慮したときに、jにたどり着ける通りの数

更新則
if iにおいてjとj+1の間に横棒があったら
DP[i+1][j+1] += D[i][j]
DP[i+1][j] += D[i][j+1]
else
DP[i+1][j] = D[i][j]

for all paterns at i

初期条件
DP[i][j]=0
DP[0][0]=1
'''

if W == 1:  # コーナーケース処理
    print(1)
    exit()


def iter_p_adic(p, length):
    '''
    連続して増加するp進数をリストとして返す。lengthはリストの長さ
    return
    ----------
    所望のp進数リストを次々返してくれるiterator
    '''
    from itertools import product
    tmp = [range(p)] * length
    return product(*tmp)


def ret_valid_swapset():
    # (j,j+1)でswapできるならjで1に成るようにする
    for p in iter_p_adic(2, W - 1):
        pre = 0
        skip = False
        for pp in p:
            if pre == 1 and pp == 1:
                skip = True
                break  # 条件を満たさない
            pre = pp
        if skip:
            continue
        yield p


pattern = list(ret_valid_swapset())

DP = [[0] * W for _ in ra(H + 1)]
DP[0][0] = 1
for i in ra(H):
    for p in pattern:
        pre_is_swap = False
        for j, is_swap in enu(p):
            if is_swap:
                DP[i + 1][j + 1] += DP[i][j]
                DP[i + 1][j] += DP[i][j + 1]
            # swapしない線の処理
            elif not pre_is_swap:
                DP[i + 1][j] += DP[i][j]
            pre_is_swap = is_swap
        if not pre_is_swap:  # 一番端の処理も忘れずに
            DP[i + 1][j + 1] += DP[i][j + 1]

            # あまりの処理
    for j in ra(W):
        DP[i + 1][j] %= MOD
print(DP[H][K - 1])
# from pprint import pprint
# print(*DP, sep='\n')

Submission status: Accepted

Solution code:
from collections import defaultdict

H, W, K = map(int, input().split())
dp1 = [[0]*2 for _ in range(W)]
dp1[0][0] = 1

for i in range(W-1):
    dp1[i+1][0] = dp1[i][0]+dp1[i][1]
    dp1[i+1][1] = dp1[i][0]

d = defaultdict(int)
d[-1] = 1

for i in range(W):
    d[i] = dp1[i][0]+dp1[i][1]

dp2 = [[0]*W for _ in range(H+1)]
dp2[0][0] = 1
MOD = 10**9+7

for i in range(H):
    for j in range(W):
        for k in range(max(0, j-1), min(W-1, j+1)+1):
            l = min(j, k)-1
            r = W-2-max(j, k)
            dp2[i+1][k] += dp2[i][j]*d[l]*d[r]
            dp2[i+1][k] %= MOD

print(dp2[H][K-1])

Submission status: Accepted

Solution code:
def isok(n):
    for i in range(w-2):
        if n >> i & 1 and n >> (i+1) & 1:
            return False
    return True
mod = 10**9+7
h,w,k = map(int,input().split())
h += 1
if w == 1:
    print(1)
    exit()
if w == 2:
    print(2**(h-2))
    exit()
dp = [[0]*(w+1) for _ in range(h+1)]
dp[1][1] = 1
amida = [0]*(w)
noamida = [0]*(w+1)
for i in range(2**(w-1)):
    if isok(i):
        for j in range(w-1):
            amida[j+1] += i >> j & 1
        for j in range(w-2):
            if i >> j & 1 == 0 and i >> (j+1) & 1 == 0:
                noamida[j+2] += 1
        if i >> 0 & 1 == 0:
                noamida[1] += 1
        if i >> (w-2) & 1 == 0:
                noamida[w] += 1
for i in range(1,h):
    dp[i+1][1] = (dp[i][2]*amida[1]+dp[i][1]*noamida[1]) % mod
    dp[i+1][w] = (dp[i][w-1]*amida[w-1]+dp[i][w]*noamida[w]) % mod
    for j in range(2,w):
        dp[i+1][j] = (dp[i][j-1]*amida[j-1]+dp[i][j+1]*amida[j]+dp[i][j]*noamida[j]) % mod
print(dp[h][k])

Submission status: Accepted

Solution code:
h,w,k = map(int,input().split())
k -= 1
dp = [[0]*w for _ in range(h+1)]
dp[0][0] = 1
mod = 10**9 + 7
def cnt(n):
    if n < 0:
        return 1
    if 0 <= n < 3:
        return n + 1
    return n + (n-1)*(n-2)//2 + (n-2)*(n-3)*(n-4)//6 + 1

for i in range(h):
    for j in range(w):
        if j > 0:
            dp[i+1][j-1] += dp[i][j] * cnt(j-2) * cnt(w-2-j)
            dp[i+1][j-1] %= mod
        if j < w-1:
            dp[i+1][j+1] += dp[i][j] * cnt(j-1) * cnt(w-3-j)
            dp[i+1][j+1] %= mod
        dp[i+1][j] += dp[i][j] * cnt(j-1) * cnt(w-2-j)
        dp[i+1][j] %= mod
print(dp[h][k])

Submission status: Accepted

Solution code:
MOD = 1000000007
h, w, k = map(int, input().split())

dp = [[0 for _ in range(w)] for _ in range(h+1)]
dp[0][0] = 1

if w == 1:
    print(1)
    exit()

for y in range(1, h+1):
    for i in range(2 ** (w-1)):
        bin_i = str(bin(i))[2:]
        if '11' in bin_i:
            continue
        for j in range(1, w):
            if ((i >> j-1) & 1):
                dp[y][j] += dp[y-1][j-1]
        for j in range(w-1):
            if ((i >> j) & 1):
                dp[y][j] += dp[y-1][j+1]
        for j in range(w):
            f = True
            if j < w:
                f = f and not ((i >> j) & 1)
            if j - 1 >= 0:
                f = f and not ((i >> j-1) & 1)
            if f:
                dp[y][j] += dp[y-1][j]
        for i in range(w):
            dp[y][i] %= MOD
# print(dp)
print(dp[h][k-1])

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    h,w,k = map(int, input().split())
    dp = [[0]*w for _ in range(h+1)]
    dp[0][0] = 1

    pattern = []
    if w > 2:
        for i in range(1<<(w-1)):
            for j in range(w-2):
                if ((i>>j) & 1) and ((i>>(j+1)) & 1):
                    break
            else:
                pattern.append(i)
    else:
        print(1)
        exit()
    
    for i in range(h):
        for j in range(w):
            for p in pattern:
                if j == 0:
                    if p & 1:
                        dp[i+1][j+1] += dp[i][j] % mod
                    else:
                        dp[i+1][j] += dp[i][j] % mod
                elif j == w-1:
                    if (p>>(w-2)) & 1:
                        dp[i+1][j-1] += dp[i][j] % mod
                    else:
                        dp[i+1][j] += dp[i][j] % mod
                else:
                    if (p>>(j-1)) & 1:
                        dp[i+1][j-1] += dp[i][j] % mod
                    elif (p>>j) & 1:
                        dp[i+1][j+1] += dp[i][j] % mod
                    else:
                        dp[i+1][j] += dp[i][j] % mod
    print(dp[-1][k-1] % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from copy import deepcopy
MOD = 10 ** 9 + 7

def main():
    h, w, k = map(int, input().split())
    ways = []
    for way in range(2 ** (w - 1)):
        if isvalid(way):
            ways.append(way)
    dp = [0 for i in range(w)]
    dp[0] = 1
    for i in range(h):
        dppre = deepcopy(dp)
        dp = [0 for i in range(w)]
        for j in range(w):
            for way in ways:
                dp[j] += dppre[apply(way, j)]
                dp[j] %= MOD
    print(dp[k - 1])

def apply(way, fr):
    logic = [False]
    for d in range(8):
        logic.append(bool(way & 1))
        way >>= 1
    if logic[fr]:
        to = fr - 1
    elif logic[fr + 1]:
        to = fr + 1
    else:
        to = fr
    return to

def isvalid(way):
    logic = []
    for d in range(7):
        logic.append(bool(way & 1))
        way >>= 1
    for d in range(6):
        if logic[d] & logic[d + 1]:
            return False
    return True

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

H, W, K = map(int, input().split())

if W == 1:
    print(1)
    exit(0)


pattern = [0] * (W - 1)
total = 0
for i in range(2 ** (W - 1)):
    bit = format(i, 'b').zfill(W - 1)
    flag = True
    for j in range(W - 1):
        if j >= 1 and bit[j] == bit[j - 1] == "1":
            flag = False
    if flag:
        total += 1
        for j in range(W - 1):
            if bit[j] == "1":
                pattern[j] += 1

dp = [[0] * W for _ in range(H + 1)]
dp[0][0] = 1

for i in range(1, H + 1):
    for j in range(W):
        if j == 0:
            dp[i][j] += dp[i - 1][j + 1] * pattern[j]
            dp[i][j] %= 1000000007
            dp[i][j] += dp[i - 1][j] * (total - pattern[j])
            dp[i][j] %= 1000000007
        elif j == W - 1:
            dp[i][j] += dp[i - 1][j - 1] * pattern[j - 1]
            dp[i][j] %= 1000000007
            dp[i][j] += dp[i - 1][j] * (total - pattern[j - 1])
            dp[i][j] %= 1000000007
        else:
            dp[i][j] += dp[i - 1][j + 1] * pattern[j]
            dp[i][j] %= 1000000007
            dp[i][j] += dp[i - 1][j - 1] * pattern[j - 1]
            dp[i][j] %= 1000000007
            dp[i][j] += dp[i - 1][j] * (total - pattern[j - 1] - pattern[j])
            dp[i][j] %= 1000000007

print(dp[H][K - 1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#number of amidakuji

h,w,k=map(int,input().split())

dp=[[0 for i in range(w+1)] for i in range(h+1)]

#dp[i][j]=i段目まででjというところにいくために必要な方法の総和
#求めるのはdp[h][k]である
dp[0][1] = 1
mod=10**9+7
if w>1:
    for i in range(1,h+1):
        for j in range(2**(w-1)):
            st=format(j,"0%ib"%(w-1))
            flag=True
            for _ in range(w-2):
                if st[_]=="1" and st[_+1]=="1":
                    flag=False
            connect=[False for kyopuro in range(w+1)]
            if flag:
                li=[]
                for ss in range(w-1):
                    if st[ss]=="1":
                        li.append(ss+1)
                        connect[ss+1]=True
                        connect[ss+2]=True

                for some in li:
                    dp[i][some+1]+=dp[i-1][some]
                    dp[i][some+1]%=mod
                    dp[i][some]+=dp[i-1][some+1]
                    dp[i][some]%=mod
                for number in range(1,w+1):
                    if connect[number]==False:
                        dp[i][number]+=dp[i-1][number]
                        dp[i][number]%=mod
    print(dp[h][k]%mod)
                
        
else:
    print(1)

Submission status: Accepted

Solution code:
h,w,k=map(int,input().split())
mod=10**9+7
dp=[0]*w
dp[0]=1
fib=[1,1,2,3,5,8,13,21,34,55,89]
for _ in range(h):
  t=[0]*w
  for i in range(w):
    if i>0:
      t[i]+=dp[i-1]*fib[i-1]*fib[w-i-1]
    t[i]+=dp[i]*fib[i]*fib[w-i-1]
    if i<w-1:
      t[i]+=dp[i+1]*fib[i]*fib[w-i-2]
    t[i]%=mod
  dp=t
print(dp[k-1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
mod = 10**9 + 7

h, w, k = [int(x) for x in input().split()]

dp = [[0]*w for _ in range(h + 1)]
dp[0][0] = 1

for i in range(h):
    for j in range(w):
        x = 0 # right
        y = 0 # left
        z = 0 # remain
        for l in range(2**(w - 1)):
            l = bin(l)[2:].zfill(w - 1)
            if "11" in l:
                continue
            if j - 1 >= 0 and l[j - 1] == "1":
                y += 1
            elif j + 1 < w and l[j] == "1":
                x += 1
            else:
                z += 1
        if j + 1 < w:
            dp[i + 1][j + 1] += x * dp[i][j]
            dp[i + 1][j + 1] %= mod
        if j - 1 >= 0:
            dp[i + 1][j - 1] += y * dp[i][j]
            dp[i + 1][j - 1] %= mod
        dp[i + 1][j] += z * dp[i][j]
        dp[i + 1][j] %= mod

print(dp[-1][k - 1])

Submission status: Accepted

Solution code:
H,W,K=map(int,input().split())

fibonacchi=[1 for i in range(0,W+1)]
for i in range(0,W+1):
    if i>=2:
        fibonacchi[i]=fibonacchi[i-1]+fibonacchi[i-2]

dp=[[[0 for i in range(0,W+1)] for j in range(0,W+1)] for k in range(0,H+1)]
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in range(1,W+1):
            if i==1:
                if j==k:
                    dp[i][j][k]=fibonacchi[j-1]*fibonacchi[W-k]
                elif j==k-1:
                    dp[i][j][k]=fibonacchi[j-1]*fibonacchi[W-k]
                elif j==k+1:
                    dp[i][j][k]=fibonacchi[k-1]*fibonacchi[W-j]
            else:
                ans=0
                if k>=2:
                    ans+=dp[i-1][j][k-1]*dp[1][k-1][k]
                if W-1>=k:
                    ans+=dp[i-1][j][k+1]*dp[1][k+1][k]
                ans+=dp[i-1][j][k]*dp[1][k][k]
                dp[i][j][k]=ans

print(dp[H][1][K]%(10**9+7))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

p = 10**9+7
h, w, k = map(int, input().split())

if w == 1:
    print(1)
    exit()

d = [[0]*(w+1) for _ in range(h+1)]

d[0][0] = 1
# [i,j]を(i*0.5)行 (j+1)列 の地点とする
# dp[i][j]: [0][0]をスタートとした時の[i][j]に到達しうるあみだくじの総数

case = [1, 2, 3, 5, 8, 13, 21]

ans = 0
for i in range(1,h+1):
    for j in range(w):
        if i+1 <= j: continue
        # center to center
        d[i][j] += d[i-1][j]*case[max(0,w-j-2)] * case[max(0,j-1)]%p
        # left to right
        if j-1>=0:
            d[i][j] += d[i-1][j-1]* case[max(0,w-j-2)] * case[max(0,j-2)]%p
        # right to left
        if j+1<=w:
            d[i][j] += d[i-1][j+1]* case[max(0,w-j-3)] * case[max(0,j-1)]%p

print(d[h][k-1]%p)

Submission status: Accepted

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

H,W,K=map(int,input().split())
mod=10**9+7
if W==1:
    print(1)
    exit()
dp=[[0 for i in range(W)] for h in range(H+1)]
dp[0][0]=1

for h in range(1,H+1):
    for i in range(2**(W-1)):
        #print(h,i)
        lst=[i>>m&1 for m in range(W-1)]
        for w in range(len(lst)-1):
            if lst[w]==lst[w+1]==1:
                break
        else:
            #print(h,lst)
            for w in range(W):
                if w>0 and lst[w-1]:
                    dp[h][w]+=dp[h-1][w-1]
                elif w<len(lst) and lst[w]:
                    dp[h][w]+=dp[h-1][w+1]
                else:
                    dp[h][w]+=dp[h-1][w]
        for _ in range(len(dp[0])):
            dp[h][_]%=mod
#print(sum(dp[-1]))
print(dp[-1][K-1]%mod)

Submission status: Accepted

Solution code:
H, W, K = map(int, input().split())

# 連続するn区間に橋をかける方法は何通りあるか
# kakekata[b][i] : bが0の時はi区間目に橋をかけない方法 bが1の時はi区間目に橋をかける方法 が何通りあるか
kakekata = [[0] * 10] + [[0] * 10]
kakekata[0][0] = 1
for i in range(8):
    kakekata[0][i + 1] = kakekata[0][i] + kakekata[1][i]
    kakekata[1][i + 1] = kakekata[0][i]


MOD = 10 ** 9 + 7
class modint:
    def __init__(self, x):
        self.x = x.x if isinstance(x, modint) else x % MOD
    def __str__(self): return str(self.x)
    __repr__ = __str__
    def __int__(self): return self.x
    __index__ = __int__
    def __add__(self, other): return modint(self.x + modint(other).x)
    def __sub__(self, other): return modint(self.x - modint(other).x)
    def __mul__(self, other): return modint(self.x * modint(other).x)
    def __pow__(self, other): return modint(pow(self.x, modint(other).x, MOD))
    def __floordiv__(self, other): return modint(self.x * pow(modint(other).x, MOD - 2, MOD))
    def __eq__(self, other): return self.x == modint(other).x
    def __ne__(self, other): return self.x != modint(other).x
    def __inv__(self): return pow(self.x, MOD - 2, MOD)


dp = [[modint(0)] * (W + 2) for h in range(H + 1)]
dp[0][1] += 1
for h in range(H):
    for w in range(1, W + 1):
        # 左に何区間あるか 右に何区間あるか
        l, r = w - 1, W - w

        # そのまま渡す
        dp[h + 1][w] += dp[h][w] * kakekata[0][l] * kakekata[0][r]
        # 右に渡す
        dp[h + 1][w + 1] += dp[h][w] * kakekata[0][l] * kakekata[1][r]
        # 左に渡す
        dp[h + 1][w - 1] += dp[h][w] * kakekata[1][l] * kakekata[0][r]

print(dp[H][K])

Submission status: Accepted

Solution code:
H, W, K = map(int, input().split())
MOD = 10**9 + 7

def check(bit):
  pre_b = '0'
  for b in bit:
    if b == '1' and pre_b == '1':
      return False
    pre_b = b
  return True

# dp[h][w]:上からh段目での操作を終了した時点でw番目のあみだにいる場合の数
dp = [[0]*(W+2) for h in range(H+1)]
dp[0][1] = 1

# ある段における横線の配置を全探索してw-1 <-> wに横線がある数
lines = [0] * (W+1)
c = 0
for i in range(2**(W-1)):
  if not check(bin(i)[2:]):
    continue
  c += 1
  for w in range(W-1):
    lines[w+1] += (i & 1<<w) > 0


# 配るDP
for h in range(H):
  for w in range(1, W+1):
    x, z = lines[w-1], lines[w]
    y = c - x - z
    dp[h+1][w-1] = (dp[h+1][w-1] + dp[h][w]*x % MOD) % MOD
    dp[h+1][w] = (dp[h+1][w] + dp[h][w]*y % MOD) % MOD
    dp[h+1][w+1] = (dp[h+1][w+1] + dp[h][w]*z % MOD) % MOD

print(dp[-1][K])

Submission status: Accepted

Solution code:
H, W, K = [int(x) for x in input().split()]
p = 10 ** 9 + 7

# H = 1, 縦棒W本のあみだくじの数　→フィボナッチ数列
h1amida = [1, 1, 2] + [None] * 6
for i in range(3, 9):
    h1amida[i] = h1amida[i - 2] + h1amida[i - 1]

# dp[h][k] = 縦棒の長さh+1、縦棒の本数W、ゴールの棒kとなるあみだの数
# dp[0][*]とdp[*][0]はダミー
dp = [[0 for _ in range(W + 1)] for _ in range(H + 1)]

for k in range(1, W + 1):
    if k == 1:
        dp[1][k] = h1amida[W - 1]
    elif k == 2:
        dp[1][k] = h1amida[W - 2]
    else:
        dp[1][k] = 0

for h in range(2, H + 1):
    for k in range(1, W + 1):
        if k > 1:
            dp[h][k] += (dp[h - 1][k - 1] * h1amida[k - 2] % p) * h1amida[W - k] % p
        dp[h][k] += (dp[h - 1][k] * h1amida[k - 1] % p) * h1amida[W - k] % p
        if k < W:
            dp[h][k] += (dp[h - 1][k + 1] * h1amida[k - 1] % p) * h1amida[W - k - 1] % p
        dp[h][k] %= p

print(dp[H][K])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

def main():
    H,W,K = map(int,input().split())
    fib = [1,1,2,3,5,8,13,21,34]
    MOD = 10**9+7
    
    if W == 1:
        print(1)
    else:
        ans = [0 for _ in range(W)]
        ans[0] = 1
        for _ in range(H):
            use = [0 for _ in range(W)]
            for i in range(W):
                if i == 0:
                    use[i] = ans[i]*fib[W-1]+ans[i+1]*fib[W-2]
                elif i == W-1:
                    use[i] = ans[i]*fib[W-1]+ans[i-1]*fib[W-2]
                else:
                    use[i] = ans[i]*fib[i]*fib[W-i-1]+ans[i-1]*fib[i-1]*fib[W-i-1]+ans[i+1]*fib[i]*fib[W-i-2]
                use[i] %= MOD
            ans = use
        print(ans[K-1])
    
if __name__ == "__main__":
    main()