码迷,mamicode.com
首页 > 其他好文 > 详细

CF 559C - Gerald and Giant Chess (组合计数)

时间:2019-09-30 12:30:04      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:oid   eof   open()   sizeof   init   class   gis   set   amp   

\(C_{x+y}^y\)的公式,DP容斥删多余贡献。

#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;
}

CF 559C - Gerald and Giant Chess (组合计数)

标签:oid   eof   open()   sizeof   init   class   gis   set   amp   

原文地址:https://www.cnblogs.com/bingoyes/p/11612081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!