Cod sursa(job #1709354)

Utilizator TeamFIIAUAIC FIIHumvee TeamFIIA Data 28 mai 2016 11:55:09
Problema Padure2 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.21 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;

int N,M,C;

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

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

#define MODNR 2000003

LL mfpow(int x, int k) {
    if(k==-1)
        int 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(int i=0; i<pnr && (LL)primes[i] <= n; i++) {
        LL p = primes[i];
        LL pp = p;
        int 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(int k)
{
    prec[k] = comb(x[k]+y[k]-2, x[k]-1);
    for(int 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(int i=1; i<=C; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }
    for(int i = 1; i <= C; i++)
    {
        if(!viz[i])
        {
            viz[i] = 1;
            DFS(i);
        }
    }
    for(int 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);
}