Cod sursa(job #1710080)

Utilizator UBB_Craciun_Griza_PuscasUBB ATeamHasNoName UBB_Craciun_Griza_Puscas Data 28 mai 2016 14:58:19
Problema Padure2 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.62 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll nmax = 1000006;
const ll cmax = 1001;
const ll mod = 2000003LL;

ll n, m, c, x, y, t;
ll inv[mod], calc[cmax];
ll fact[mod];
vector <pair <ll, ll>> v;

ll raise(ll a, ll b) {
    if(b == 0)
        return 1;

    ll half = raise(a, b/2);

    if(b % 2 == 0)
        return (half * half) % mod;
    return (((half*half) % mod) * a) % mod;
}

bool cmp(pair <ll, ll> a, pair <ll, ll> b) {
    return (a.first + a.second) < (b.first + b.second);
}

ll comb(ll n, ll k) {
    return (((fact[n] * inv[fact[k]]) % mod) * inv[fact[n-k]]) % mod;
}

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

    f>>n>>m;

    inv[1] = 1;
    fact[0] = 1;
    fact[1] = 1;
    for(int i=2; i<=mod-1; i++) {
        inv[i] = raise(i, mod-2);
        fact[i] = (i * fact[i-1]) % mod;
    }

    t = comb(n+m-2, n-1);
    f>>c;
    for(int i=1; i<=c; i++) {
        f>>x>>y;
        v.push_back(make_pair(x, y));
    }

    sort(v.begin(), v.end(), cmp);
    for(int i=0; i<v.size(); i++) {

        ll cost = comb(v[i].first + v[i].second - 2, v[i].first-1);

        for(int j=0; j<i; j++) {

            if(v[i].first >= v[j].first && v[i].second > v[j].second) {

                cost = cost - (comb(v[i].first + v[i].second - v[j].first - v[j].second, v[i].first - v[j].first) * calc[j]) % mod;
                if(cost < 0)
                    cost += mod;
            }
        }

        calc[i] = cost;
        t =  t - (cost * comb(n + m - v[i].first - v[i].second, n - v[i].first)) % mod;
        if(t < 0)
            t += mod;
    }

    g<<t<<"\n";


    return 0;
}