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

  思路:若是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. 版本管理_git

    git 世界上最好的版本管理工具,分布式版本控制系统. 林纳斯-托瓦斯,自由主义教皇(git.linux) git 不管理空文件夹 对比于 SVN mkdir XX        创建一个空目录 XX ...

  2. Nginx+Https自己敲命令生成证书

    nginx配置https访问 一.准备 环境:centos6.8 nginx:1.13.6 二.开始       首先安装依赖包: yum install -y gcc gcc-c++ autocon ...

  3. python语法_元组

    tuple 元组 被称为只读列表 tup = (1,3,4,'234') 只能读,不能进行修改操作. 与列表的区分就是 () [] 中括号和小括号的区别,

  4. Linux filesystem

    文件系统的运作与操作系统的文件数据有关.较新的操作系统的文件数据除了文件实际内容外,通常含有非常多的属性,例如Linux操作系统的文件权限(rwx)与文件属性(属主.属组.时间参数等).文件系统通常会 ...

  5. 阿里云服务器Linux CentOS安装配置(11)安装Wordpress

    下载wordpress安装包 wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.zip unzip wordpress-4.8.1-zh_CN.z ...

  6. 关于redis分布式锁实现原理

    具体详情 http://www.cnblogs.com/SUNSHINEC/p/8302540.html

  7. 如何量化考核技术人的KPI?

    对技术人来说,技术是成长的“核心”.然而,在实际工作协作中,技术的重要性常常被业务所掩盖,造成先业务后技术的现象. 针对这个痛点,阿里高级技术专家张建飞提出了自己的解决思路,希望能与大家一起探讨交流. ...

  8. maven jdk版本

    http://maven.apache.org/docs/history.html Maven Releases History Date format is: YYYY-MM-DD Maven 3 ...

  9. ItunesConnect:"Missing Push Notification Entitlement"警告-----以及解决方法

    最近开发的cordova应用,要做ios的适配,并且发布版本,但是有一次在发测试版本的时候,突然收到一封邮件警告,原文如下: Missing Push Notification Entitlement ...

  10. promise 的学习

    promise 是为了解决异步操作的顺序问题而产生的 特性 promise 的实例一旦创建就会执行里面的异步操作 promise 的实例状态一旦改变就变成凝固的了, 无法再对其作出修改,  (不明白为 ...