Cod sursa(job #1709127)

Utilizator echipa_BoSSilorUNIBUC Harsan Bicsi Baltatu echipa_BoSSilor Data 28 mai 2016 10:59:55
Problema Padure2 Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.68 kb
#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

struct pct {
    int x, y;
};

const int MOD = 2000003;
const int N = 2000003;

int n, m, c, d[N], fact[N], invfact[N];
pct v[N];

int comb(int n, int k) {
    int r = (1LL * fact[n] * invfact[k]) % MOD;
    r = (1LL * r * invfact[n - k]) % MOD;

    return r;
}

int nr(int p1, int p2, int p3, int p4) {
    int sz1 = p3 - p1, sz2 = p4 - p2;

    return comb(sz1 + sz2, sz1);
}
int po(int el, int p) {
    int r = 1;

    while(p) {

        if(p & 1)
            r = (1LL * r * el) % MOD;

        el = (1LL * el * el) % MOD;
        p /= 2;
    }

    return r;
}

bool cmp(pct a, pct b) {
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}

int main()
{int i, j;
    freopen("padure2.in", "r", stdin);
    freopen("padure2.out", "w", stdout);

    cin >> n >> m >> c;

    fact[0] = 1;
    for(i = 1; i < N; ++i)
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    invfact[N - 1] = po(fact[N - 1], MOD - 2);
    for(i = N - 2; i >= 0; --i) {

        invfact[i] = (1LL * invfact[i + 1] * (i+ 1)) % MOD;
    }

    for(i = 1; i <= c; ++i) {

        cin >> v[i].x >> v[i].y;

    }

    sort(v+  1, v + c + 1, cmp);

    //v[0].x = 1; v[0].y = 1;
    v[c + 1].x = n; v[c + 1].y = m;

    for(i = 1; i <= c + 1; ++i) {

        d[i] = nr(1, 1, v[i].x, v[i].y);

        for(j = 1; j < i; ++j) if(v[j].x <= v[i].x && v[j].y <= v[i].y) {

            int aa = (1LL * d[j] * nr(v[j].x, v[j].y, v[i].x, v[i].y)) % MOD;

            d[i] = (d[i] - aa + MOD) % MOD;
        }
    }

    cout << d[c + 1];

    return 0;
}