题意:走马步,要求向右向下,不能走进禁止的点。求方案数。

  思路:若是n*m比较小的话,那么可以直接DP。但是这道题目不行。不过我们仔细分析可以知道从某个点到某个点是一个组合数,但是数据太大,mod值很小,所以只能用Lucas定理。然后DP一下到某个点不经过之前的点的方案数一直推下去就可以得到最终答案了。

#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int maxn = 1e3 + ;
const int maxm = 2e5 + ;
const int mod = ; ll fac[maxm], refac[maxm], dp[maxm]; ll mypow(ll a, ll p, ll mo){
ll ret = ;
while(p){
if(p & ) ret = ret * a % mo;
a = a * a % mo;
p >>= ;
}
return ret;
} void init(){
refac[] = refac[] = fac[] = fac[] = 1LL;
for(int i = ; i < mod; i ++) fac[i] = 1LL * fac[i - ] * i % mod;
refac[mod - ] = mypow(fac[mod - ], mod - , mod);
for(int i = mod - ; i > ; i --) refac[i] = 1LL * refac[i + ] * (i + ) % mod;
} ll comb(int a, int b){
if(a < b) return ;
return fac[a] * refac[b] % mod * refac[a - b] % mod;
} ll lucas(ll n, ll m){
if(!m) return ;
return comb(n % mod, m % mod) * lucas(n/mod, m/mod) % mod;
} struct P{
ll x, y;
P(){}
P(ll a, ll b):x(a), y(b){}
bool operator < (const P & t) const{
return x + y < t.x + t.y;
}
bool check(const P & t){
if(x <= t.x || y <= t.y) return false;
ll a = x - t.x, b = y - t.y ;
if((a + b) % != || a > * b || * a < b) return false;
return true;
}
ll cnt(const P & t){
ll dx = x - t.x, dy = y - t.y;
ll step = (dx + dy) / ;
return lucas(step, dx - step);
}
};
P in[maxn]; int main(){
init();
int ncase = ;
ll n, m;
int k; while(~scanf("%lld%lld%d", &n, &m, &k)){
memset(dp, , sizeof(dp));
bool flag = true;
for(int i = ; i < k; i ++) {
scanf("%lld%lld", &in[i].x, &in[i].y);
if(in[i].x == n && in[i].y == m) flag = false;
}
if(!flag) {
printf("Case #%d: 0\n", ncase ++);
continue;
}
if(n == && m == ) {
printf("Case #%d: %lld\n", ncase ++, 1LL);
continue;
}
sort(in, in + k);
in[k].x = n, in[k].y = m;
for(int i = ; i <= k; i ++){
if(!in[i].check(P(, ))) continue;
dp[i] = in[i].cnt(P(, ));
for(int j = ; j < i; j ++){
if(!dp[j] || !in[i].check(in[j])) continue;
dp[i] = ((dp[i] - dp[j] * in[i].cnt(in[j])) % mod + mod ) % mod;
}
}
printf("Case #%d: %lld\n", ncase ++, dp[k]);
}
return ;
}

A Simple Chess (Lucas组合数 + 容斥)的更多相关文章

  1. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  2. hdu_5794_A Simple Chess(lucas+dp)

    题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...

  3. bzoj3782上学路线(Lucas+CRT+容斥DP+组合计数)

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3782 有部分分的传送门:https://www.luogu.org/problemnew/ ...

  4. BZOJ5306 [HAOI2018]染色 【组合数 + 容斥 + NTT】

    题目 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 \(M\) 种颜色中的某一种. 然而小 C 只 ...

  5. 【BZOJ4710】[Jsoi2011]分特产 组合数+容斥

    [BZOJ4710][Jsoi2011]分特产 Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同 ...

  6. cf997C. Sky Full of Stars(组合数 容斥)

    题意 题目链接 \(n \times n\)的网格,用三种颜色染色,问最后有一行/一列全都为同一种颜色的方案数 Sol Orz fjzzq 最后答案是这个 \[3^{n^2} - (3^n - 3)^ ...

  7. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  8. CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)

    Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive in ...

  9. 【BZOJ2839】集合计数 组合数+容斥

    [BZOJ2839]集合计数 Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数 ...

随机推荐

  1. [是男人就过8题——Pony.ai]Perfect N-P Arrays

    [是男人就过8题--Pony.ai]Perfect N-P Arrays 题目大意: 一棵\(n(\sum n\le5\times10^6)\)个结点的树,每个结点都有一个括号.求树上一个合法的括号序 ...

  2. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习7

    #include <iostream>#include <cstring>const MAXSIZE=100;using namespace std;int main (){  ...

  3. Go语言基础(一)

    Go语言基础(一) 国庆体验一下大名鼎鼎的Go语言,IDE使用IEDA+Go插件,边敲代码边体会,感觉Go语言好酷 一.Hello World 和Java类似,go文件需要一个package包含,代码 ...

  4. 在vue脚手架中使用npm的方式使用swiper

    打开项目的根目录,然后打开命令窗口,输入 npm install swiper@4.4.1 @后为指定版本号,也可以不写 在main.js 中,引入 import Swiper from 'swipe ...

  5. MySql数据and高级查询

    1.CREATE TABLE grade(stuID INT AUTO_INCREMENT PRIMARY KEY NOT NULL,stuName VARCHAR(32),stuAge INT ) ...

  6. Oracle Sequence创建与使用

    一.Sequence简介 Sequence是数据库系统按照一定的规则自动增加的数字序列,主要用于生成数据库数据记录.这个序列一般作为代理主键(因为不会重复). Sequence是数据中一个特殊存放等差 ...

  7. Idea使用说明

    快捷键 ctrl + alt + b : 查找接口的实现类 ctrl + h : 查找类或接口的继承关系 double shift : 搜索任何地方 ctrl + shift + n : 查找文件 c ...

  8. PHP 利用CURL(HTTP)实现服务器上传文件至另一服务器

    // 上传端 /** * 向目标地址推送xls文件 * @Date 2019/4/29 */ public function putXls() { // 目标接口 $url = "http: ...

  9. 源码编译vim

    目录 获取最新版 vim 源码 1 git仓库clone 2, 源码包下载,里面有各个版本的vim压缩包 vim 配置选项 配置示例 参考文章 tip 获取最新版 vim 源码 1 git仓库clon ...

  10. [daily] cscope

    手册: http://cscope.sourceforge.net/cscope_vim_tutorial.html 下载 cscope_maps.vim 到 $HOME/.vim/plugin/ 目 ...