Cod sursa(job #1709993)

Utilizator theboysUVT Talpau Craciun Ciobanu theboys Data 28 mai 2016 14:43:16
Problema Padure2 Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.34 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

const int cst = 2000003;

struct QQ{
    int x, y;
};

bool cmp(QQ i, QQ j){
    if(i.x == j.x)
        return i.y < j.y;
    return i.x < j.x;
}

QQ a[1100];
int fact[2000100], inv[1000100];
int d[1105];

int cmb(int x, int y){
    int n = (1LL*inv[x] * inv[y]) % cst;

    return ((1LL*n*fact[x + y]) % cst);
}

int main(){
	
	freopen("padure2.in", "r", stdin);
	freopen("padure2.out", "w", stdout);
	
    fact[0] = 1;
    for(int i = 1; i < 2000010; ++i){
        fact[i] = (1LL * fact[i-1] * i) % cst;
    }

    inv[0] = 1; inv[1] = 1;
    for(int i = 2; i < 1000010; ++i){
        inv[i] = (1LL*inv[cst % i]*(cst - (cst/i))) % cst;
    }
    for(int i = 2; i < 1000010; ++i){
        inv[i] = (1LL * inv[i] * inv[i - 1]) % cst;
    }

    int h, w, n;
	
	scanf("%d %d %d", &h, &w, &n);
    
    for(int i = 0; i < n; ++i){
		scanf("%d %d", &a[i].x, &a[i].y);
    }
    a[n].x = h; a[n].y = w;

    sort(a, a + n + 1, cmp);

    for(int i = 0; i <= n; ++i){
        d[i] = cmb(a[i].x - 1, a[i].y - 1);
        for(int j = 0; j < i; ++j){
            if(a[i].x >= a[j].x && a[i].y >= a[j].y){
                d[i] -= (1LL*d[j]*cmb(a[i].x - a[j].x, a[i].y - a[j].y)) % cst;
                if(d[i] < 0)
                    d[i] += cst;
            }
        }
    }
	
	printf("%d", d[n]);

return 0;
}