Problem:


Score : 600 points

Problem Statement

Let us define the oddness of a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n} as \sum_{i = 1}^n |i - p_i|.

Find the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Constraints

All values in input are integers.

1 \leq n \leq 50

0 \leq k \leq n^2

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

2

There are six permutations of {1,\ 2,\ 3}. Among them, two have oddness of 2:  {2,\ 1,\ 3} and {1,\ 3,\ 2}.

Sample Input 2

39 14

Sample Output 2

74764168



Sample input:
3 2


Sample output:
2

Submission status: Accepted

Solution code:
N, K = map(int, open(0).read().split())

MOD = 10 ** 9 + 7
dp = [[[0] * (K + 1) for _ in range(N + 1)] for _ in range(N + 1)]
dp[0][0][0] = 1

for i in range(1, N + 1):
    for j in range(N + 1):
        for k in range(K + 1):
            if k - 2 * j >= 0:
                dp[i][j][k] = (2 * j + 1) * dp[i - 1][j][k - 2 * j]
                
                if j < N:
                    dp[i][j][k] += (j + 1) ** 2 * dp[i - 1][j + 1][k - 2 * j]
                if j > 0:
                    dp[i][j][k] += dp[i - 1][j - 1][k - 2 * j]
            dp[i][j][k] %= MOD
                    
print(dp[N][0][K])

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
m = 1000000007

dp = [[[0] * (K+2*N+1) for _ in range(N+1)] for _ in range(N+1)]

dp[0][0][0] = 1
for i in range(0, N):
  for j in range(0, i+1):
    for k in range(0, K+1):
      dp[i+1][j+1][k+2*(j+1)] += dp[i][j][k]
      dp[i+1][j+1][k+2*(j+1)] %= m
      dp[i+1][j][k+2*j] += dp[i][j][k]
      dp[i+1][j][k+2*j] %= m
      dp[i+1][j][k+2*j] += 2*j * dp[i][j][k]
      dp[i+1][j][k+2*j] %= m
      if j != 0:
        dp[i+1][j-1][k+2*(j-1)] += j*j * dp[i][j][k]
        dp[i+1][j-1][k+2*(j-1)] %= m
print(dp[N][0][K])

Submission status: Accepted

Solution code:
n,score=map(int,input().split())
dp=[[[0]*(3000) for j in range(n+1)] for i in range(n+1)]
dp[0][0][0]=1
MOD=10**9+7
for i in range(n):
    for j in range(i+1):
        for k in range(score+1):
            #yoko
            nj=j
            dp[i+1][nj][k+2*nj]+=dp[i][j][k]
            dp[i+1][nj][k+2*nj]%=MOD
            #erabanai
            nj=j+1
            dp[i+1][nj][k+2*nj]+=dp[i][j][k]
            dp[i+1][nj][k+2*nj]%=MOD
            #katahou
            nj=j
            x=2*j
            dp[i+1][nj][k+2*nj]+=dp[i][j][k]*x
            dp[i+1][nj][k+2*nj]%=MOD

            if j==0:
                continue
            nj=j-1
            x=j*j
            dp[i+1][nj][k+2*nj]+=dp[i][j][k]*x
            dp[i+1][nj][k+2*nj]%=MOD
print(dp[n][0][score])

Submission status: Accepted

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

# dp[i][j][k] iまで見た, j個保留, 奇妙さk
dp = [[[0]*(K+N*2+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 1

for i in range(N) :
    for j in range(i+1) :
        for k in range(K+1) :
            dp[i+1][j][k+j*2] += dp[i][j][k]
            dp[i+1][j][k+j*2] %= MOD
            dp[i+1][j+1][k+(j+1)*2] += dp[i][j][k]
            dp[i+1][j+1][k+(j+1)*2] %= MOD
            dp[i+1][j][k+j*2] += dp[i][j][k] * j * 2
            dp[i+1][j][k+j*2] %= MOD
            if j >= 1 :
                dp[i+1][j-1][k+(j-1)*2] += dp[i][j][k] * j * j
                dp[i+1][j-1][k+(j-1)*2] %= MOD
                
print(dp[N][0][K])

Submission status: Accepted

Solution code:
from functools import lru_cache


def mod(x):
    return x % (10 ** 9 + 7)


@lru_cache(maxsize=None)
def dp(i, j, k):
    if i == 0:
        if k == 0 and j == 0:
            return 1
        else:
            return 0
    elif k < 0 or j < 0 or i < j:
        return 0
    else:
        return mod(
            (2 * j + 1) * dp(i - 1, j, k - 2 * j)
            + (j + 1) * (j + 1) * dp(i - 1, j + 1, k - 2 * j)
            + dp(i - 1, j - 1, k - 2 * j)
        )


n, k = map(int, input().split())
print(dp(n, 0, k))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
dp = [[[0]*N**2*3 for a in range(N+1)] for b in range(N+1)]

dp[0][0][0] = 1
for i in range(N):
    for j in range(N):
        for k in range(N**2):
            dp[i+1][j+1][k+2*j+2] += dp[i][j][k]
            dp[i+1][j][k+2*j] += dp[i][j][k] * (2*j+1)
            if j: dp[i+1][j-1][k+2*j-2] += dp[i][j][k] * j**2

print(dp[N][0][K] % (10**9+7))

Submission status: Accepted

Solution code:
mod=1000000007
n,k=map(int,input().split())
DP=[[[0]*(k+1) for _ in range(n+1)] for _ in range(n+1)]
DP[0][0][0]=1
for i in range(1,n+1):
    for j in range(n+1):
        for l in range(k+1):
            if l-2*j>=0:
                DP[i][j][l]=(2*j+1)*DP[i-1][j][l-2*j]
                if j!=n:
                    DP[i][j][l]+=(j+1)**2*DP[i-1][j+1][l-2*j]
                if j!=0:
                    DP[i][j][l]+=DP[i-1][j-1][l-2*j]
            DP[i][j][l]%=mod
print(DP[-1][0][-1])