Cod sursa(job #2072373)

Utilizator DruffbaumPopescu Vlad Druffbaum Data 21 noiembrie 2017 19:48:22
Problema Padure2 Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.35 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define x first
#define y second
#define MOD 2000003
const int MAXN = 1e6;
const int MAXM = 1e3;

int fact[2 * MAXN + 1], d[MAXM + 1];
std::vector <std::pair <int, int> > v;

int mpow(int a, int n) {
  int p = 1;
  while (n > 0) {
    if (n & 1) {
      p = (1LL * p * a) % MOD;
    }
    a = (1LL * a * a) % MOD;
    n >>= 1;
  }
  return p;
}

inline int comb(int n, int k) {
  return 1LL * fact[n] * mpow(fact[k], MOD - 2) % MOD * mpow(fact[n - k], MOD - 2) % MOD;
}                 

int main() {
  int n, m, c, x, y;
  FILE *f = fopen("padure2.in", "r");
  fscanf(f, "%d%d%d", &n, &m, &c);
  for (int i = 0; i < c; ++i) {
    fscanf(f, "%d%d", &x, &y);
    v.push_back({x, y});
  }
  fclose(f);
  v.push_back({n, m});
  std::sort(v.begin(), v.end());
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= 2 * MAXN; ++i) {
    fact[i] = (1LL * fact[i - 1] * i) % MOD;
  }
  for (int i = 0; i <= c; ++i) {
    d[i] = comb(v[i].y + v[i].x - 2, v[i].x - 1) % MOD;
    for (int j = 0; j < i; ++j) {
      if (v[j].x <= v[i].x && v[j].y <= v[i].y) {
        d[i] = (d[i] - 1LL * d[j] * comb(v[i].x - v[j].x + v[i].y - v[j].y, v[i].x - v[j].x) % MOD + MOD) % MOD;
      }
    }
  }
  f = fopen("padure2.out", "w");
  fprintf(f, "%d\n", d[c]);
  fclose(f);
  return 0;
}