Cod sursa(job #2455999)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 13 septembrie 2019 12:03:31
Problema Padure2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 1.63 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("padure2.in");
ofstream g("padure2.out");
int N, M, C;
pair <int, int> X[1005];
const int MOD = 2000003;
int DP[1005];
int Fact[2000005], Inv[2000005];
int powerLog(int n, int p){
    int sol = 1;
    while(p){
        if(p % 2 == 1)
            sol = (1LL * sol * n) % MOD;
        p /= 2;
        n = (1LL * n * n) % MOD;
    }
    return sol;
}
void precalcFact(){
    Fact[0] = 1;
    for(int i = 1; i <= 2000000; i++)
        Fact[i] = (1LL * Fact[i - 1] * i) % MOD;
    Inv[2000000] = powerLog(Fact[2000000], MOD - 2);
    for(int i = 2000000 - 1; i >= 0; i--){
        Inv[i] = (1LL * Inv[i + 1] * (i + 1)) % MOD;
    }
}

void Read(){
    f >> N >> M >> C;
    for(int i = 1; i <= C; i++)
        f >> X[i].first >> X[i].second;
    X[++C] = make_pair(N, M);
    sort(X + 1, X + C + 1);
}
int Comb(int n, int k){
    return (((1LL * Fact[n] * Inv[k]) % MOD) * Inv[n - k]) % MOD;
}
int Roads(int x1, int y1, int x2, int y2){
    return Comb(x2 - x1 + y2 - y1, x2 - x1);
}
void Solve(){
    for(int i = 1; i <= C; i++){
        int x = X[i].first, y = X[i].second;
        int roads = Roads(1, 1, x, y);
        for(int j = 1; j < i; j++){
            if(X[j].first <= x && X[j].second <= y){
                int sub = (1LL * DP[j] * Roads(X[j].first, X[j].second, x, y)) % MOD;
                roads -= sub;
                if(roads < 0)
                    roads += MOD;
            }
        }
        DP[i] = roads;
    }
    g << DP[C] << '\n';
}
int main()
{
    precalcFact();
    Read();
    Solve();
    return 0;
}