Submission #1371133


Source Code Expand

#include <iostream>
#include <vector>

int main() {
  const unsigned long long MOD = 1000000007;
  unsigned long N;
  unsigned long long A, B, C, D;
  std::cin >> N >> A >> B >> C >> D;
  std::vector<unsigned long long> inv(N + 1);
  inv[1] = 1;
  for (unsigned i = 2; i <= N; ++i) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; }
  std::vector<unsigned long long> fact(N + 1);
  fact[0] = 1;
  for (unsigned i = 1; i <= N; ++i) { fact[i] = fact[i - 1] * i % MOD; }
  std::vector<unsigned long long> inv_fact(N + 1);
  inv_fact[0] = 1;
  for (unsigned i = 1; i <= N; ++i) { inv_fact[i] = inv_fact[i - 1] * inv[i] % MOD; }
  std::vector<std::vector<unsigned long long>> dp(N + 1, std::vector<unsigned long long>(N + 1, 0));
  for (unsigned i = 0; i <= N; ++i) { dp[0][i] = 1; }
  for (unsigned i = 1; i <= N; ++i) {
    for (auto j = A; j <= B; ++j) {
      dp[i][j] = dp[i][j - 1];
      if (i / j < C) { continue; }
      unsigned long long tmp = 1;
      for (unsigned k = 0; k < C - 1; ++k) {
        tmp *= inv_fact[j];
        tmp %= MOD;
      }
      for (auto k = C; k <= std::min(i / j, D); ++k) {
        tmp *= inv_fact[j];
        tmp %= MOD;
        auto acc = dp[i - k * j][j - 1];
        //choose{i}{k*j} * frac{(k*j)!}{j!**k*k!}
        //i!(k*j)! / (k*j)!(i-k*j)!(j!)**k(k!)
        //i! / (i-k*j)!(j!)**k(k!)
        acc *= fact[i];
        acc %= MOD;
        acc *= inv_fact[i - k * j];
        acc %= MOD;
        acc *= tmp;
        acc %= MOD;
        acc *= inv_fact[k];
        acc %= MOD;
        dp[i][j] += acc;
        dp[i][j] %= MOD;
      }
    }
  }
  std::cout << dp[N][B];
  return 0;
}

Submission Info

Submission Time
Task E - Grouping
User ytsmiling
Language C++14 (Clang 3.8.0)
Score 600
Code Size 1674 Byte
Status AC
Exec Time 72 ms
Memory 8192 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 15
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, subtask_1_many_01.txt, subtask_1_many_02.txt, subtask_1_many_03.txt, subtask_1_many_04.txt, subtask_1_max_01.txt, subtask_1_max_02.txt, subtask_1_min_01.txt, subtask_1_randa_01.txt, subtask_1_randa_02.txt, subtask_1_randb_01.txt, subtask_1_randb_02.txt
Case Name Status Exec Time Memory
sample_01.txt AC 8 ms 888 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 72 ms 8192 KB
sample_04.txt AC 1 ms 256 KB
subtask_1_many_01.txt AC 44 ms 8192 KB
subtask_1_many_02.txt AC 47 ms 8192 KB
subtask_1_many_03.txt AC 44 ms 8192 KB
subtask_1_many_04.txt AC 49 ms 8192 KB
subtask_1_max_01.txt AC 8 ms 8192 KB
subtask_1_max_02.txt AC 7 ms 8192 KB
subtask_1_min_01.txt AC 1 ms 256 KB
subtask_1_randa_01.txt AC 4 ms 2432 KB
subtask_1_randa_02.txt AC 1 ms 256 KB
subtask_1_randb_01.txt AC 6 ms 4992 KB
subtask_1_randb_02.txt AC 2 ms 512 KB