Cod sursa(job #1710256)

Utilizator UPB_Darius_Rares_SilviuPeace my pants UPB_Darius_Rares_Silviu Data 28 mai 2016 18:34:23
Problema Padure2 Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.48 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("padure2.in");
ofstream g("padure2.out");

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

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

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

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

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

int cmp(asd A, asd B) {
    if (A.x < B.x) return 1;
        else if (A.x == B.x) return (A.y < B.y);
    return 0;
}

int main() {
    f >> N >> M >> K; fact[0] = inv[0] = 1;
    int limit = N + M + 3;
    for (int i = 1; i <= limit; ++i) {
        fact[i] = (1LL * fact[i - 1] * i) % mod;
        inv[i] = logPow(fact[i], mod - 2);
    }

    for (int i = 1; i <= K; ++i) {
        f >> 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] = 1LL * (dp[i] - (1LL * 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;
                }
            }
        }
    }

    g << dp[K] << '\n';
    return 0;
}