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:
def f_permutation_oddness(N, K, MOD=10**9 + 7):
    # 参考: https://atcoder.jp/contests/abc134/submissions/6487912
    from itertools import product

    # https://www.youtube.com/watch?v=-o4r74eJzV4 の 1:49:00 からの説明のように、
    # 左上→右下へ線を引いて水平線と交わる点の数と、
    # 左下→右上へ線を引いて水平線と交わる点の数は等しい。
    # よって奇妙さは必ず偶数であり、Kが奇数の場合はそれを満たすような置換がない。
    if K % 2 == 1:
        return 0

    # editorial と同じ定義と遷移
    dp = [[[0 for k in range(K + 1)]
           for j in range(N + 2)]
          for i in range(N + 1)]
    dp[0][0][0] = 1
    for i, j, k in product(range(1, N + 1), range(N + 1), range(K + 1)):
        k_previous = k - 2 * j
        if k_previous < 0:
            continue
        dp[i][j][k] += (2 * j + 1) * dp[i - 1][j][k_previous]
        dp[i][j][k] += (j + 1)**2 * dp[i - 1][j + 1][k_previous]
        if j > 0:
            dp[i][j][k] += dp[i - 1][j - 1][k_previous]
        dp[i][j][k] %= MOD
    return dp[N][0][K]

N, K = [int(i) for i in input().split()]
print(f_permutation_oddness(N, K))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import product

MOD = 1000000007  # type: int

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

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    k = int(next(tokens))  # type: int
    solve(n, k)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = list(map(int, sys.stdin.readline().split()))

if K % 2 == 1:
    print(0)
    exit()
if N == 1:
    if K == 0:
        print(1)
    else:
        print(0)
    exit()
if K == 0:
    print(1)
    exit()

# 解説AC
# dp[i][j][k]: i まで決まってて、保留の数が j、確定した奇妙さが k
dp = [[[0] * (K + 1) for _ in range(N)] for _ in range(N)]
dp[0][0][0] = 1
dp[0][1][2] = 1
for i, j, k in itertools.product(range(1, N), range(N - 1), range(K + 1)):
    if k - j * 2 < 0:
        continue
    # i == pi
    # 保留の数はそのまま
    # 奇妙さは保留の数だけ増える
    dp[i][j][k] += dp[i - 1][j][k - j * 2] * 1
    # i == p_j または j == p_i (j < i)
    # 保留の数はそのまま
    # 奇妙さは保留の数だけ増える
    # パターン数はうさぎの保留数j + かめの保留数j
    dp[i][j][k] += dp[i - 1][j][k - j * 2] * (j * 2)
    # i == p_j かつ j == p_j (j < i)
    # 保留数が1減る
    # パターン数はうさぎの保留数j * かめの保留数j
    dp[i][j][k] += dp[i - 1][j + 1][k - j * 2] * (j + 1) ** 2
    if j - 1 >= 0:
        # 両方保留
        # そのまま増やす
        dp[i][j][k] += dp[i - 1][j - 1][k - j * 2]

    dp[i][j][k] %= MOD
print(dp[-1][0][K])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    print(3*n**2)
    return

#B
def B():
    n,d = LI()
    f = [1]*(n+2*d+2)
    ans = 0
    for i in range(n):
        if f[i]:
            for j in range(i,i+2*d+1):
                f[j] = 0
            ans += 1
    print(ans)
    return

#C
def C():
    n = I()
    a = IR(n)
    mi = max(a)
    if a.count(mi) > 1:
        for i in range(n):
            print(mi)
    else:
        b = [a[i] for i in range(n)]
        b.sort()
        b = b[::-1]
        for i in range(n):
            if mi != a[i]:
                print(mi)
            else:
                print(b[1])

    return

#D
def D():
    n = I()
    a = LI()
    b = [0]*(n+1)
    for i in range(1,n+1)[::-1]:
        j = 2*i
        k = 0
        while j <= n:
            k ^= b[j]
            j += i
        if a[i-1]^k:
            b[i] = 1
    ans = []
    for i in range(n+1):
        if b[i]:
            ans.append(i)
    print(len(ans))
    if ans:
        print(*ans)
    return

#E
def E():
    n = I()
    a = IR(n)
    mi = []
    ans = 0
    for i in range(n):
        j = bisect.bisect_right(mi,-a[i])
        if j == len(mi):
            mi.append(-a[i])
        else:
            mi[j] = -a[i]
    print(len(mi))
    return

#F 箱根駅伝DP
def F():
    n,K = LI()
    dp = [[[0]*(K+2500) for j in range(n+1)] for i in range(n+1)] #dp[i][j][k] : i段目、j個保留の時にスコアがkとなるパターン数(保留した点の数だけ、スコアは加算されていく,保留点が段をまたいでスコアが増えていくイメージ)
    dp[0][0][0] = 1
    for i in range(n):
        for j in range(i+1):
            for k in range(K+1):
                #パターン1 : i+1段目で両方結ぶ
                nj = j
                dp[i+1][nj][k+2*nj] += dp[i][j][k]
                dp[i+1][nj][k+2*nj] %= mod
                #パターン2 : i+1段目で両方結ばない
                nj = j+1
                dp[i+1][nj][k+2*nj] += dp[i][j][k]
                dp[i+1][nj][k+2*nj] %= mod
                #パターン3 : i+1段目で片側ともう片方の保留点を結ぶ
                nj = j
                x = j*2 #結ぶ相方の選び方((右or左)*(保留点の数))
                dp[i+1][nj][k+2*nj] += dp[i][j][k]*x
                dp[i+1][nj][k+2*nj] %= mod
                #パターン4 : i+1段目で両側ともう片方の保留点を結ぶ
                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][K])
    return

#Solve
if __name__ == "__main__":
    F()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
MOD = 10**9 + 7
memo = {}
def dfs(i, a, b, k):
    key = (i, a, b, k)
    if key in memo:
        return memo[key]
    if i == N:
        if b > 0:
            memo[key] = 0
            return 0
        return k == K
    r = dfs(i+1, a, b, k+a+b) + dfs(i+1, a+1, b+1, k+a+b+2)
    if a > 0:
        r += dfs(i+1, a, b, k+a+b) * a
    if b > 0:
        r += dfs(i+1, a, b, k+a+b) * b
    if a > 0 < b:
        r += dfs(i+1, a-1, b-1, k+a+b-2) * (a * b)
    memo[key] = r = r % MOD
    return r
print(dfs(0, 0, 0, 0))