CF 559C - Gerald and Giant Chess (composition count)

\ (C_ {x + y} ^ y \) formula, DP-repellent delete redundant capacity contribution.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
 
//#define ON_DEBUGG
 
#ifdef ON_DEBUGG
 
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else
 
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
 
#endif
 
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;
 
template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}

const int N = 200007;
const int mod = 1000000007;

#define int long long
struct nod{
    int x, y;
    bool operator < (const nod &com) const{
        if(x != com.x) return x < com.x;
        return y < com.y;
    }
}a[N];
int fac[N], inv[N], n, m, K;
inline int Pow(int a, int b){
    int s = 1;
    while(b){
        if(b & 1) s = s * a % mod;
        a = a * a % mod, b >>= 1; 
    }
    return s;
}
inline void Init(){
    fac[0] = fac[1] = inv[0] = 1;
    R(i,2,n + m) fac[i] = fac[i - 1] * i % mod;
    R(i,1,n + m) inv[i] = Pow(fac[i], mod - 2);
}
inline int Calc(int x, int y){
    if(x < 0 || y < 0) return 0;
    return fac[x + y] * inv[x] % mod * inv[y] % mod;
}
int f[N];
#undef int
int main(){
#define int long long
//FileOpen();
//FileSave();
    io >> n >> m >> K;
    Init();
    R(i,1,K){
        io >> a[i].x >> a[i].y;
    }
    sort(a + 1, a + K + 1);
    a[K + 1] = (nod){ n, m};
    R(i,1,K + 1){
        int x = a[i].x - 1, y  = a[i].y - 1;
        f[i] = Calc(x, y);
        R(j,1, i - 1){
            int dx = a[i].x - a[j].x, dy = a[i].y - a[j].y;
            int del = Calc(dx, dy) * f[j] % mod;
            f[i] = (f[i] - del + mod) % mod;
        }
    }
    printf("%lld", f[K + 1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/bingoyes/p/11612081.html