A Simple Chess (Lucas组合数 + 容斥)
题意:走马步,要求向右向下,不能走进禁止的点。求方案数。
思路:若是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组合数 + 容斥)的更多相关文章
- Codeforces 100548F - Color (组合数+容斥)
题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...
- hdu_5794_A Simple Chess(lucas+dp)
题目链接:hdu_5794_A Simple Chess 题意: 给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案 题解: ...
- bzoj3782上学路线(Lucas+CRT+容斥DP+组合计数)
传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3782 有部分分的传送门:https://www.luogu.org/problemnew/ ...
- BZOJ5306 [HAOI2018]染色 【组合数 + 容斥 + NTT】
题目 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 \(M\) 种颜色中的某一种. 然而小 C 只 ...
- 【BZOJ4710】[Jsoi2011]分特产 组合数+容斥
[BZOJ4710][Jsoi2011]分特产 Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同 ...
- cf997C. Sky Full of Stars(组合数 容斥)
题意 题目链接 \(n \times n\)的网格,用三种颜色染色,问最后有一行/一列全都为同一种颜色的方案数 Sol Orz fjzzq 最后答案是这个 \[3^{n^2} - (3^n - 3)^ ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive in ...
- 【BZOJ2839】集合计数 组合数+容斥
[BZOJ2839]集合计数 Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数 ...
随机推荐
- XIV Open Cup named after E.V. Pankratiev. GP of America
A. Ancient Diplomacy 建图,同色点间边权为$0$,异色点间边权为$1$,则等价于找一个点使得到它最短路最长的点的最短路最小,Floyd即可. 时间复杂度$O(n^3)$. #inc ...
- 做rl_abs过程中遇到的问题
问题一 运行 train_abstractor.py就出现这个问题 nohup: ignoring input start training with the following hyper-para ...
- TestNG 中DataProvider 的用法
使用DataProvider提供数据有两种形式: 第一种:一种是在测试代码和测试数据放在同一个类中: 第二种:把所有的数据提供都单独写在一个类里面,当测试数据比较多时,这种方法利于维护. DataPr ...
- 那些年我们跳过的 IE坑
一, IE input X 去掉文本框的叉叉和密码输入框的眼睛图标 解: 从IE 10开始,type=”text” 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清 ...
- angular.isDate()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Oracle 用户权限 Grant
用户的权限来自系统权限和对象权限 一.系统权限 3个索引权限 Grant CREATE ANY INDEX to User_Name://创建索引 Grant ALTER ANY INDEX to U ...
- javascript的数组之map()
map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的回调函数后返回的结果.新数组 // ES6 let numbers = [1, 5, 10, 15]; let doubles ...
- 使用Dubbo的SPI扩展机制实现自定义LoadBalance——方法二 不改源码添加META-INF/dubbo元数据
一.官网提供的方法 参考官网 http://dubbo.apache.org/zh-cn/docs/dev/impls/load-balance.html 二.方法总结 在工程中创建类并实现LoadB ...
- Linux下查找某一文件常用的方式
当我们需要在ubuntu中找到之前的某一个文件,该用什么方式呢?用以下命令你就可以快速定位: find / -name "pycharm.sh" 用find查找这个命令,确定查找范 ...
- 2. Scala变量
2.1 变量是程序的基本组成单位 举一个简单的例子 object boke_demo01 { def main(args: Array[String]): Unit = { var a: Int = ...