Cod sursa(job #1710276)

Utilizator UPB_Darius_Rares_SilviuPeace my pants UPB_Darius_Rares_Silviu Data 28 mai 2016 18:53:00
Problema Padure2 Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.51 kb
#include <cstdio>
#include <algorithm>
using namespace std;

struct asd{
    int x, y;
}a[1005];

const int mod = 2000003;
int N, M, K;
long long dp[1005], fact[2000005], inv[2000005];

int comb(int x, int y) {
    return (1LL * fact[x + y - 2] * inv[x - 1] * inv[y - 1] ) % mod;
}

long long logPow(long long base, long long exp) {
    long long res = 1;
    while (exp) {
        if (exp & 1) {
            res = (res * base) % mod;
        }

        base = (base * base) % mod;
        exp >>= 1;
    }
    return res;
}

int cmp(asd A, asd B) {
    return ((A.x < B.x) || (A.x == B.x && A.y < B.y));
}

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

    scanf("%d%d%d", &N, &M, &K); fact[0] = inv[0] = 1;
    int limit = N + M + 3;
    for (int i = 1; i <= limit; ++i) {
        fact[i] = (fact[i - 1] * i) % mod;
        inv[i] = logPow(fact[i], mod - 2);
    }

    for (int i = 1; i <= K; ++i) {
        scanf("%d%d", &a[i].x, &a[i].y);
    }

    a[++K].x = N; a[K].y = M;
    sort(a+1, a+K+1, cmp);
    for (int i = 1; i <= K; ++i) {
        dp[i] = comb(a[i].x, a[i].y);
        for (int j = 1; j < i; ++j) {
            if (a[j].x <= a[i].x && a[j].y <= a[i].y) {
                dp[i] = (dp[i] - (dp[j] * comb(a[i].x - a[j].x + 1, a[i].y - a[j].y + 1))) % mod;

                if (dp[i] < 0) {
                    dp[i] += mod;
                }
            }
        }
    }

    printf("%d\n", dp[K]);
    return 0;
}