Cod sursa(job #1710084)

Utilizator TeamFIIAUAIC FIIHumvee TeamFIIA Data 28 mai 2016 14:58:47
Problema Padure2 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.2 kb
#include <iostream>
#include <map>
#include <cstdio>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;

#define MAXN 1000010
typedef long long LL;

LL N,M,C;

LL primes[MAXN];
char marked[MAXN];
LL x[1010], y[1010],  viz[1010];
LL prec[1010];
LL pnr;

void ciur() {
    primes[pnr++] = 2;
    for(LL i=3; i<MAXN; i+=2) {
        if(marked[i]) continue;
        primes[pnr++] = i;
        for(LL j=i+i; j<MAXN; j+=i)
            marked[j] = 1;
    }
}

#define MODNR 2000003

LL mfpow(LL x, LL k) {
    if(k==-1)
        LL a = 5;
    if(k == 0) return 1;
    if(k == 1) return x;
    LL aux = mfpow(x, k>>1);
    LL tmp = (aux * aux) % MODNR;
    if(k&1)
        return (tmp * x) % MODNR;
    else return tmp;
}

LL comb(LL n, LL k) {
    LL res = 1;
    for(LL i=0; i<pnr && (LL)primes[i] <= n; i++) {
        LL p = primes[i];
        LL pp = p;
        LL c = 0;
        while(pp <= n) {
            c += n/pp;
            c -= k/pp;
            c -= (n-k)/pp;
            pp *= p;
        }
        res *= mfpow(p, c);
        res %= MODNR;
    }
    return res;
}

void DFS(LL k)
{
    prec[k] = comb(x[k]+y[k]-2, x[k]-1);
    for(LL i = 1; i <= C; i++)
    {
        if(i == k)continue;
        if(x[i] <= x[k] && y[i] <= y[k])
        {
            if(!viz[i])
            {
                viz[i] = 1;
                DFS(i);
            }
            prec[k] = (prec[k] - prec[i]) % MODNR;
            if(prec[k] < 0)
                prec[k] += MODNR;
        }
    }
}

int main()
{
    ciur();
    freopen("padure2.in", "r", stdin);
    freopen("padure2.out", "w", stdout);
    scanf("%d%d", &N, &M);
    LL total = comb(N+M-2, N - 1);
    scanf("%d", &C);
    for(LL i=1; i<=C; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }
    for(LL i = 1; i <= C; i++)
    {
        if(!viz[i])
        {
            viz[i] = 1;
            DFS(i);
        }
    }
    for(LL i = 1; i <= C; i++)
    {
        LL toDec = (comb(N+M-x[i]-y[i], N-x[i]) * prec[i]) % MODNR;
        total -= toDec;
        total %= MODNR;
    }
    printf("%lld\n", total);
}