Submission #660529

#TimeUsernameProblemLanguageResultExecution timeMemory
660529Alex_tz307Kangaroo (CEOI16_kangaroo)C++17
100 / 100
11 ms328 KiB
#include <bits/stdc++.h>

using namespace std;

const int kN = 2e3;
const int mod = 1e9 + 7;
int dp[2][1 + kN];

void addSelf(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int add(int x, int y) {
  addSelf(x, y);
  return x;
}

void multSelf(int &x, int y) {
  x = (int64_t)x * y % mod;
}

int mult(int x, int y) {
  multSelf(x, y);
  return x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, first, last;
  cin >> n >> first >> last;
  dp[1][1] = 1;
  for (int i = 2, t = 0; i <= n; ++i, t ^= 1) {
    for (int j = 1; j <= i; ++j) {
      if (i == first || i == last) {
        dp[t][j] = add(dp[t ^ 1][j - 1], dp[t ^ 1][j]);
      } else {
        dp[t][j] = add(mult(dp[t ^ 1][j + 1], j), mult(dp[t ^ 1][j - 1], j - (i > first) - (i > last)));
      }
    }
  }
  cout << dp[n & 1][1] << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...