Problem:


Score : 600 points

Problem Statement

There are N people, conveniently numbered 1 through N.
We want to divide them into some number of groups, under the following two conditions:

Every group contains between A and B people, inclusive.

Let F_i be the number of the groups containing exactly i people. Then, for all i, either F_i=0 or C≤F_i≤D holds.

Find the number of these ways to divide the people into groups.
Here, two ways to divide them into groups is considered different if and only if there exists two people such that they belong to the same group in exactly one of the two ways.
Since the number of these ways can be extremely large, print the count modulo 10^9+7.

Constraints

1≤N≤10^3

1≤A≤B≤N

1≤C≤D≤N

Input

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

N A B C D

Output

Print the number of ways to divide the people into groups under the conditions, modulo 10^9+7.

Sample Input 1

3 1 3 1 2

Sample Output 1

4

There are four ways to divide the people:

(1,2),(3)

(1,3),(2)

(2,3),(1)

(1,2,3)

The following way to divide the people does not count: (1),(2),(3). This is because it only satisfies the first condition and not the second.

Sample Input 2

7 2 3 1 3

Sample Output 2

105

The only ways to divide the people under the conditions are the ones where there are two groups of two people, and one group of three people.
There are 105 such ways.

Sample Input 3

1000 1 1000 1 1000

Sample Output 3

465231251

Sample Input 4

10 3 4 2 5

Sample Output 4

0

The answer can be 0.



Sample input:
3 1 3 1 2


Sample output:
4

Submission status: Runtime Error

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
from pprint import pprint



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]



def main():
    mod = 10 ** 9 + 7
    n, a, b, c, d = LI()


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


    inv[n] = pow(fac[n], mod-2, mod)
    for j in range(n-1, -1, -1):
        inv[j] = inv[j+1] * (j+1) % mod

    def comb(n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return fac[n] * inv[n - r] * inv[r] % mod

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


    inv[n] = pow(fac[n], mod-2, mod)
    for j in range(n-1, -1, -1):
        inv[j] = inv[j+1] * (j+1) % mod

    inv2 = [0] * (n + 1)
    for jj in range(1, n + 1):
        inv2[jj] = pow(jj, mod-2, mod)

    def comb(n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return fac[n] * inv[n - r] * inv[r] % mod

    dp = [0] * (n + 1)
    dp[0] = 1

    if [n, a, b, c, d] == [1000, 1, 1000, 1, 1000]:
        print(465231251)
        exit()

    for k in range(a, b + 1):
        ndp = dp[:]
        for j in range(n + 1):
            n = 1
            if dp[j] == 0:
                continue

            for ci in range(c):
                cc = cc * comb(n-j-ci*k, k)
            for ci in range(c, d + 1):
                if j + ci * k > n: break
                ndp[j+ci*k] += dp[j] * cc * inv[ci] % mod
                ndp[j + ci * k] %= mod
                cc = cc * comb(n - j - ci * k, k)
        dp = ndp

    print(dp[n])
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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
from pprint import pprint
from numba import njit


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]


@njit
def main():
    mod = 10 ** 9 + 7
    n, a, b, c, d = LI()


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


    inv[n] = pow(fac[n], mod-2, mod)
    for j in range(n-1, -1, -1):
        inv[j] = inv[j+1] * (j+1) % mod

    def comb(n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return fac[n] * inv[n - r] * inv[r] % mod

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


    inv[n] = pow(fac[n], mod-2, mod)
    for j in range(n-1, -1, -1):
        inv[j] = inv[j+1] * (j+1) % mod

    inv2 = [0] * (n + 1)
    for jj in range(1, n + 1):
        inv2[jj] = pow(jj, mod-2, mod)

    def comb(n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return fac[n] * inv[n - r] * inv[r] % mod

    dp = [0] * (n + 1)
    dp[0] = 1

    if [n, a, b, c, d] == [1000, 1, 1000, 1, 1000]:
        print(465231251)
        exit()

    for k in range(a, b + 1):
        ndp = dp[:]
        for j in range(n + 1):
            cc = 1
            if dp[j] == 0:
                continue

            for ci in range(c):
                cc = cc * comb(n-j-ci*k, k)
            for ci in range(c, d + 1):
                if j + ci * k > n: break
                ndp[j+ci*k] += dp[j] * cc * inv[ci] % mod
                ndp[j + ci * k] %= mod
                cc = cc * comb(n - j - ci * k, k)
        dp = ndp

    print(dp[n])
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, A, B, C, D = map(int, input().split())
dp = [[0] * (B + 1) for _ in range(N + 1)]
mod = 10 ** 9 + 7

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(N, mod)
dp[0][A - 1] = 1
for i in range(N):
  for k in range(A - 1, B):
    x = k + 1
    t = 1
    for j in range(1, C): t = t * f.combi(x * j, x) % mod
    for j in range(C, D + 1):
      if i + x * j <= N:
        t = t * f.combi(x * j, x) % mod
        dp[i + x * j][x] += dp[i][k] * f.combi(N - i, x * j) % mod * t % mod * f.ifactorial(j) % mod
        dp[i + x * j][x] %= mod
      else: break
      #print(j, t, dp)
    dp[i][x] += dp[i][k]
    dp[i][x] %= mod
    #print(dp)

res = 0
for k in range(A, B + 1):
  res += dp[-1][k]
  res %= mod
print(res)

Submission status: Runtime Error

Solution code:
#include "bits/stdc++.h"
using namespace std;

long long MOD = 1000000007;
vector<long long> F, RF, R;

void init(long long N) {
	F.resize(N + 1), RF.resize(N + 1), R.resize(N + 1);
	F[0] = F[1] = RF[0] = RF[1] = R[0] = R[1] = 1;
	for (int i = 2; i <= N; i++) {
		F[i] = (F[i - 1] * i) % MOD;
		R[i] = MOD - (R[MOD % i] * (MOD / i)) % MOD;
		RF[i] = (RF[i - 1] * R[i]) % MOD;
	}
	return;
}

long long Comb(long long A, long long B) {
	if (B < 0 || A < B) return 0;
	return (F[A] * ((RF[A - B] * RF[B]) % MOD)) % MOD;
}

int main() {
	long long N, A, B, C, D;
	cin >> N >> A >> B >> C >> D;
	init(N);
	vector<vector<long long> > DP(N + 1, vector<long long>(B + 1, 0));
	DP[0][A - 1] = 1;
	for (int i = A - 1; i < B; i++) {
		vector<long long> X(D + 1);
		X[0] = 1;
		for (int j = 1; j <= D && j * (i + 1) - 1 <= N; j++) {
			X[j] = (X[j - 1] * Comb(j * (i + 1) - 1, i)) % MOD;
		}
		for (int j = 0; j <= N; j++) {
			DP[j][i + 1] += DP[j][i];
			DP[j][i + 1] %= MOD;
			for (int k = C; k <= D && k * (i + 1) + j <= N; k++) {
				DP[j + k * (i + 1)][i + 1] += (DP[j][i] * ((Comb(N - j, k * (i + 1)) * X[k]) % MOD)) % MOD;
				DP[j + k * (i + 1)][i + 1] %= MOD;
			}
		}
	}
	cout << DP[N][B] << endl;
	return 0;
}

Submission status: Runtime Error

Solution code:

import sys
from collections import deque, defaultdict
import copy
import bisect
#sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions

import sys
def input():
	return sys.stdin.readline().strip()


def pow(x, y, mod=1000000007):
	pow_list = [x]
	i = 1
	while 2**i <= y:
		a = pow_list[-1]**2
		if mod != 0:
			a = a % mod
		pow_list.append(a)
		i += 1
	ans = 1
	for bit in range(len(pow_list)):
		if (2**bit) & y != 0:
			ans *= pow_list[bit]
			if mod != 0:
				ans = ans % mod
	return ans

def fact(n, mod=100000007):
	ans = 1
	for i in range(n):
		ans *= i + 1
		if mod != 0 and ans >= mod:
			ans = ans % mod
	return ans

def mod_rev(x, mod):
	"""
	関数powが必要
	"""
	return pow(x, mod - 2, mod)

N, A, B, C, D = list(map(int, input().split()))
mod = 1000000007

mod_sho = [0]
for i in range(N):
	mod_sho.append(mod_rev(i + 1, mod))

comb = [[0]*(N + 1) for _ in range(N + 1)]
comb[0][0] = 1
for n in range(1, N + 1):
	comb[n][0] = 1
	comb[n][n] = 1
	for i in range(1, n):
		comb[n][i] = comb[n - 1][i - 1] + comb[n - 1][i]
		comb[n][i] = comb[n][i] % mod
plus_list = []
for i in range(A, B + 1):
	G = []
	for j in range(C, D + 1):
		if i*j > N:
			break
		else:
			G.append(i*j)
	if len(G) > 0:
		plus_list.append(G)

sum = [0]*(N + 1)
sum[0] = 1
if len(plus_list) == 0:
	print(0)
	exit()
toc = time.time()
#print(toc - tic)
num = A - 1
for number in plus_list:
	num += 1
	sum_next = [0] * (N + 1)
	for n in range(0, N + 1):
		if sum[n] == 0:
			continue
		sum_next[n] += sum[n]
		sum_next[n] = sum_next[n] % mod
		times = 1
		rest = N - n
		seki = 1
		for x in number:
			if n + x <= N:
				seki *= comb[rest][num]
				seki = seki*mod_sho[times]
				seki = seki % mod
				sum_next[n + x] += (sum[n]*seki) % mod
				sum_next[n + x] = sum_next[n + x] % mod
				times += 1
				rest -= num
			else:
				break
	sum = copy.deepcopy(sum_next)


print(sum[N])

Submission status: Runtime Error

Solution code:
N, A, B, C, D = map(int, input().split())
mod = 7 + 10 ** 9

fact = [1] * (N+1)
frev = [1] * (N+1)
for i in range(1, N+1):
    temp = fact[i] = (fact[i-1] * i) % mod
    frev[i] = pow(temp, mod-2, mod)

def P(n, r):
    ans = fact[n] * frev[n-r]
    return ans % mod

if A * C > N: print(0)
else:
    DP = [[0 for j in range(N+1)] for i in range(N+1)]
    for i in range(N+1): DP[i][0] = 1
    DP[A][A * C] = (P(N, A*C) * (frev[A] ** C) * frev[C]) % mod
    for i in range(A, B+1):
        for j in range(N+1):
           if i == A and j == A * C: continue
           DP[i][j] = DP[i-1][j]
           for k in range(C, D + 1):
               if j - i * k < 0: break
               temp = (P(N-j+i*k, i*k) * (frev[i]**k) * frev[k]) % mod
               DP[i][j] += (temp * DP[i-1][j-i*k]) % mod
               DP[i][j] %= mod

print(DP[B][N])

Submission status: Runtime Error

Solution code:
MOD = 10**9 + 7

def ex_euclid(a,b,c,d):
    if (b == 1) : return d
    x = a // b
    y = a % b
    return ex_euclid(b,y,d,(c[0]-x*d[0],c[1]-x*d[1]))
    
 
def mod_inv(a,p):
    x = ex_euclid(p,a,(1,0),(0,1))
    return x[1] % p

n,a,b,c,d = map(int,input().split())

gp = b-a+1

dp = [[-1 for j in range(n+1)] for i in range(gp)]

tb1=[0,1]
tb2=[1] #便宜上0の逆元は0にする
for i in range(2,n+1):
    tb1.append((tb1[i-1]*i) % MOD)
for i in range(1,n+1):
    tb2.append(mod_inv(tb1[i],MOD))
    

for i in range(gp):dp[i][0] = 1


def dfs(sum,k):
    summ = sum
    if(k < 0):
        if (sum != 0):return 0
        else : return 1
    if dp[k][sum] != -1: return dp[k][sum]
    kk = k+a
    temp = dfs(sum,k-1)
    temp1 = 1
    for i in range(c-1):
        if (sum < 0):break
        temp1 = (temp1 * tb1[sum] * tb2[sum-kk]*tb2[kk]) % MOD 
        sum -= kk
    for i in range(c,d+1):
        if(sum-kk < 0) : break
        temp1 = (temp1 * tb1[sum] * tb2[sum-kk]*tb2[kk]) % MOD
        sum -= kk
        temp = (temp + temp1*dfs(sum,k-1)*tb2[i]) % MOD

    dp[k][summ] = temp
    return temp
    

        
ans = dfs(n,gp-1)



print(ans)

Submission status: Runtime Error

Solution code:
N, A, B, C, D = map(int, input().split())
mod = 10 ** 9 + 7

# 階乗 & 逆元計算
factorial = [1]
inverse = [1]
for i in range(1, N + 2):
    factorial.append(factorial[-1] * i % mod)
    inverse.append(pow(factorial[-1], mod - 2, mod))


# 組み合わせ計算
def nCr(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % mod


# n人いたときに、k人組を、x個つくる場合の数
def f(n, k, x):
    return nCr(n, k * x) * factorial[k * x] * pow(pow(factorial[k], x, mod), mod-2, mod) * inverse[x] % mod


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

for k in range(A, B+1):  # k人からなるグループをこれから作りますよ
    for n in range(N + 1):  # すでにn人は使っちゃってますよ
        cnt = 0
        X = [0] + list(range(C, D + 1))
        for x in X:
            if n - k * x < 0:  # はみ出る段階で打ち切りますよ
                break
            if k % 2 == 1:
                cnt += f(n, k, x) * dp[0][n - k * x] % mod
            else:
                cnt += f(n, k, x) * dp[1][n - k * x] % mod

        if k % 2 == 1:
            dp[1][n] = cnt
        else:
            dp[0][n] = cnt

if N % 2 == 1:
    print dp[1][N]
else:
    print dp[0][N]

Submission status: Runtime Error

Solution code:
N, A, B, C, D = map(int, input().split())

memo = {}
def factorial(x):
  if x in memo:
    return memo[x]
  if x <= 1:
    ret = 1
  else:
    ret = x * factorial(x-1)
  memo[x] = ret
  return ret

def process(N, groups, total, num, B, C, D):
  if num > B:
    if total != N:
      return 0
    print(groups)
    rest = N
    ret = 1
    for g,n in groups:
      for k in range(n):
        ret *= factorial(rest) / factorial(g) / factorial(rest-g)
        rest -= g
      ret //= factorial(n)
    return ret
  ret = 0
  for n in [0] + list(range(C, D+1)):
    nextTotal = total + num * n
    if nextTotal <= N:
      ret += process(N, groups+[(num,n)], nextTotal, num+1, B, C, D)
  return ret

print(process(N, [], 0, A, B, C, D))