Problem:


Score : 900 points

Problem Statement

Snuke loves permutations. He is making a permutation of length N.

Since he hates the integer K, his permutation will satisfy the following:

Let the permutation be a_1, a_2, ..., a_N. For each i = 1,2,...,N, |a_i - i| \neq K.

Among the N! permutations of length N, how many satisfies this condition?

Since the answer may be extremely large, find the answer modulo 924844033(prime).

Constraints

2 ≦ N ≦ 2000

1 ≦ K ≦ N-1

Input

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

N K

Output

Print the answer modulo 924844033.

Sample Input 1

3 1

Sample Output 1

2

2 permutations (1, 2, 3), (3, 2, 1) satisfy the condition.

Sample Input 2

4 1

Sample Output 2

5

5 permutations (1, 2, 3, 4), (1, 4, 3, 2), (3, 2, 1, 4), (3, 4, 1, 2), (4, 2, 3, 1) satisfy the condition.

Sample Input 3

4 2

Sample Output 3

9

Sample Input 4

4 3

Sample Output 4

14

Sample Input 5

425 48

Sample Output 5

756765083



Sample input:
3 1


Sample output:
2

Submission status: Wrong Answer

Solution code:
def sort_list(route): #引数でリストを受け取る。
    route_ = route[:]
    row = []
    box = route_
    row.append(box) #リストの0番目（元の値）を格納
    for node in range(len(route)-1): #リストを一回転する。
       row.append(box) #リストのnode+1番目の初期化
       row[node+1] = row[node][:] #初期化したものを、前のリストのコピーに置き換える。
       row[node+1].append(row[node][0])
       row[node+1].pop(0)

    return row

n,k = input().split()
o = []
lis = sort_list([_n+1 for _n in range(int(n))])
for li in lis:
    flag = False
    for i in range(int(n)):
        if li[i] - i == int(k):
            flag = True
    if flag is False: o.append(li)
print(len(o)%924844033)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n,k = map(int,input().split())
mod = 924844033
rng = 2010
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod
if (n-k)*2 <= n:
  x = (n-k)*2
  ans = 0
  for i in range(x+1):
    if i%2 == 0:
      ans += cmb(x,i)*fctr[n-i]
    else:
      ans -= cmb(x,i)*fctr[n-i]
    ans %= mod
  print(ans)
  exit()
dc = defaultdict(int)
for j in range(1,k+1):
  a = j
  b = k+j
  cnt = 0
  while a<=n and b<=n:
    if a>b:
      b += 2*k
    else:
      a += 2*k
    cnt += 1
  dc[cnt] += 2
nn = (n-k)*2
cp = set()
cpp = 1
for i,x in dc.items():
  for j in range(x):
    cpp += i
    cp.add(cpp)
cp.add(1)
dp = [[0 for j in range(n+1)] for i in range(nn+1)]
dp[0][0] = 1
for i in range(1,nn+1):
  dp[i] = dp[i-1][:]
  if i not in cp:
    for j in range(1,min(i,n)+1):
      dp[i][j] += dp[i-2][j-1]
  else:
    for j in range(1,min(i,n)+1):
      dp[i][j] += dp[i-1][j-1]
  dp[i][j] %= mod
ans = 0
for i in range(n+1):
  if i%2 == 0:
    ans += fctr[n-i]*dp[nn][i]
  else:
    ans -= fctr[n-i]*dp[nn][i]
  ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n,k = map(int,input().split())
mod = 924844033
rng = 2010
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod
if (n-k)*2 <= n:
  x = (n-k)*2
  ans = 0
  for i in range(x+1):
    if i%2 == 0:
      ans += cmb(x,i)*fctr[n-i]
    else:
      ans -= cmb(x,i)*fctr[n-i]
    ans %= mod
  print(ans)
  exit()
dc = defaultdict(int)
for j in range(1,k+1):
  a = j
  b = k+j
  cnt = 0
  while a<=n and b<=n:
    if a>b:
      b += 2*k
    else:
      a += 2*k
    cnt += 1
  dc[cnt] += 2
cp = set()
cpp = 1
for i,x in dc.items():
  for j in range(x):
    cpp += i
    cp.add(cpp)
cp.add(1)
nn = (n-k)*2
dp = [[0 for j in range(n+1)] for i in range(nn+1)]
dp[0][0] = 1
for i in range(nn+1):
  dp[i][0] = 1
  if i not in cp:
    for j in range(1,min(i,n)+1):
      dp[i][j] = dp[i-1][j]+dp[i-2][j-1]
  else:
    for j in range(1,min(i,n)+1):
      dp[i][j] = dp[i-1][j]+dp[i-1][j-1]
    dp[i][j] %= mod
ans = 0
for i in range(n+1):
  if i%2 == 0:
    ans += fctr[n-i]*dp[nn][i]
  else:
    ans -= fctr[n-i]*dp[nn][i]
  ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
from collections import defaultdict

MAX = 924844033
n, k = [int(x) for x in input().split()]
k_perm = permutations(range(1, n+1))
ans = 0
dp = defaultdict(int)
# ans_list = []
for perm in k_perm:
    like = True
    for i in range(n):
        if dp[(perm[i], i)] == 1:
            like = False
            break
        if abs(perm[i] - (i+1)) == k:
            dp[(perm[i], i)] = 1
            like = False
            break
    if like:
        # ans_list.append(perm)
        ans +=1
print(ans % MAX)
# print(ans_list)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy

a = input().split(" ")
N = int(a[0])
K = int(a[1])
l = list([i+1 for i in range(N)])
it = itertools.permutations(l,N)

itNum = numpy.array(list(it))
lNum = numpy.array(l)
isK = itNum - lNum

ans = 0
for i in isK:
    if not(K in i or -K in i):
        ans += 1
  
print(ans%924844033)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

MAX = 924844033
n, k = [int(x) for x in input().split()]
k_perm = permutations(range(1, n+1))
ans = 0
# ans_list = []
for perm in k_perm:
    like = True
    for i in range(n):
        if abs(perm[i] - (i+1)) == k:
            like = False
    if like:
        # ans_list.append(perm)
        ans +=1
print(ans % MAX)
# print(ans_list)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def isContainedValueK(tup, k):
    for i, t in enumerate(tup):
        if t - (i + 1) == k or t - (i + 1) == -k:
            return True
    return False
        
n, k = map(int, input().split())
r = [i for i in range(1, n + 1)]
cnt = 0
for tup in itertools.permutations(r, n):
    if not isContainedValueK(tup, k):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod=924844033

n,k=LI()
fac = [1] * (n + 1)
for j in range(1, n + 1):
    fac[j] = fac[j-1] * j % mod

dp=[[0]*2 for _ in range(n+1)]
dp[0][0]=1
last=0
for i in range(min(n,2*k)):
    idx=i
    while idx<n:
        ndp = [[0] * 2 for _ in range(n+1)]
        if idx==i:
            for ll in range(n+1):
                dp[ll][0]+=dp[ll][1]
                dp[ll][1]=0
        for l in range(1,n+1):
            ndp[l][0]=sum(dp[l])
            if idx-k>=0:
                ndp[l][0]+=dp[l-1][0]
            if idx+k<n:
                ndp[l][1]=sum(dp[l-1])
            ndp[l][0]%=mod
        ndp[0][0]=1
        dp=ndp
        idx+=2*k

ans=fac[n]
for m in range(1,n+1):
    if m%2:
        ans-=sum(dp[m])*fac[n-m]%mod
    else:
        ans+=sum(dp[m])*fac[n-m]%mod
    ans%=mod

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n,k = map(int,input().split())
mod = 924844033
rng = 2010
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod
if (n-k)*2 <= n:
  x = (n-k)*2
  ans = 0
  for i in range(x+1):
    if i%2 == 0:
      ans += cmb(x,i)*fctr[n-i]
    else:
      ans -= cmb(x,i)*fctr[n-i]
    ans %= mod
  print(ans)
  exit()
dc = defaultdict(int)
for j in range(1,k+1):
  a = j
  b = k+j
  cnt = 0
  while a<=n and b<=n:
    if a>b:
      b += 2*k
    else:
      a += 2*k
    cnt += 1
  dc[cnt] += 2
nn = (n-k)*2
cp = set()
cpp = 1
for i,x in dc.items():
  for j in range(x):
    cpp += i
    cp.add(cpp)
cp.add(1)
dp = [[0 for j in range(n+1)] for i in range(nn+1)]
dp[0][0] = 1
for i in range(1,nn+1):
  dp[i] = dp[i-1][:]
  if i not in cp:
    for j in range(1,min(i,n)+1):
      dp[i][j] += dp[i-2][j-1]
      dp[i][j] %= mod
  else:
    for j in range(1,min(i,n)+1):
      dp[i][j] += dp[i-1][j-1]
      dp[i][j] %= mod
ans = 0
for i in range(n+1):
  if i%2 == 0:
    ans += fctr[n-i]*dp[nn][i]
  else:
    ans -= fctr[n-i]*dp[nn][i]
  ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import numpy as np

MOD = 924844033

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv

# mod (2K) ごとに、「反例として決めうつ個数」 -> 「合法な決め打ち方」を求める。
# 項数・両端の選択肢の有無でdp

N,K = map(int,readline().split())

dp_0 = np.zeros((N+1,N+1),np.int64) # 両端がともに使えない
dp_1 = np.zeros((N+1,N+1),np.int64) # 両端が片方だけ使える → 右側だけ使えるとして
dp_2 = np.zeros((N+1,N+1),np.int64) # 両端がともに使える
dp_0[0,0] = 1
dp_1[0,0] = 1
dp_2[0,0] = 1
for n in range(1,N+1):
    # 最後の項を右側でとる
    dp_1[n,1:] += dp_1[n-1,:-1]
    dp_2[n,1:] += dp_2[n-1,:-1]
    # 最後の項を左側でとる
    if n >= 2:
        dp_0[n,1:] += dp_0[n-1,:-1]
        dp_1[n,1:] += dp_0[n-1,:-1]
    dp_2[n,1:] += dp_1[n-1,:-1]
    # 最後の項を放置する
    dp_0[n] += dp_1[n-1]
    dp_1[n] += dp_1[n-1]
    dp_2[n] += dp_2[n-1]
    dp_0[n] %= MOD
    dp_1[n] %= MOD
    dp_2[n] %= MOD

def F(N,K,n):
    # mod 2Kでnのものたち
    items = (N-n)//(2*K)+1
    last = n + (items-1)*(2*K)
    x = (n-K>0) + (last+K<=N)
    arr = dp_0 if x == 0 else (dp_1 if x == 1 else dp_2)
    return arr[items][:items+1]

def convolve(x,y):
    Lx = len(x)
    Ly = len(y)
    if Lx < Ly:
        x,y = y,x
        Lx,Ly = Ly,Lx
    arr = np.zeros(Lx+Ly-1,np.int64)
    for n in range(Ly):
        arr[n:n+Lx] += y[n] * x % MOD
    arr %= MOD
    return arr

x = np.array([1],np.int64)
for n in range(1,2*K+1):
    if n > N:
        break
    x = convolve(x,F(N,K,n))

fact, _ = make_fact(N+100,MOD)

L = len(x)
x[1::2] *= (-1)
answer = (x[::-1] * fact[N-L+1:N+1] % MOD).sum() % MOD
print(answer)

Submission status: Accepted

Solution code:
from functools import reduce

mod = 924844033
frac = [1]*5555
for i in range(2,5555):
    frac[i] = i * frac[i-1]%mod
fraci = [None]*5555
fraci[-1] = pow(frac[-1], mod -2, mod)
for i in range(-2, -5555, -1):
    fraci[i] = fraci[i+1] * (5555 + i + 1) % mod

N, K = map(int, input().split())

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

def merge(A, B):
    C = [0]*(len(A) + len(B) - 1)
    for i in range(len(A)):
        for j in range(len(B)):
            C[i+j] = (C[i+j] + A[i]*B[j]) % mod
    return C

for j in range(min(N, 2*K)):
    dp[0][j][2] = 1
    if j + K <= N-1:
        dp[1][j][0] = 1
    if j - K >= 0:
        dp[1][j][1] = 1

for i in range(-(-N//(2*K)) + 1):
    for j in range(N):
        if j + 2*K > N - 1:
            break
        r = dp[i][j][0]
        l = dp[i][j][1]
        n = dp[i][j][2]
        if i == -(-N//(2*K)):
            dp[i][j+2*K][2] = (dp[i][j+2*K][2] + r + l + n) % mod
            continue
        if j + 3*K <= N - 1:
            dp[i+1][j+2*K][0] = (dp[i+1][j+2*K][0] + r + l + n) % mod
        dp[i][j+2*K][2] = (dp[i][j+2*K][2] + r + l + n) % mod
        dp[i+1][j+2*K][1] = (dp[i+1][j+2*K][1] + l + n) % mod
Ans = []
for j in range(min(2*K, N)):
    j = - 1 - j
    Ans.append([sum(dp[i][j]) for i in range(-(-N//(2*K)) + 1)])

A = reduce(merge, Ans)
A = [((-1)**i * frac[N - i] * a)%mod for i, a in enumerate(A)]
print(sum(A)%mod)