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,求取法的方案数 ...
随机推荐
- nodejs+koa在header里面添加header信息
参考:https://koa.bootcss.com/ ctx.append('resultCode', '0000'); ctx.append('resultMessage', 'success') ...
- S0.3 直方图
目录 直方图(一) 直方图概念 直方图分类 直方图优点 直方图应用 opencv3直方图产生函数cvCalcHist() 画图函数 完整示例 直方图(一) 直方图概念 直方图是图像中像素强度分布的图形 ...
- react-native添加react-native-vector-icons插件android遇到的问题
问题 yarn add react-native-vector-icons后图省事使用react-native link来添加native配置,结果run时报错. ps:安装的需要native的插件不 ...
- Java 读取 Json格式的 内容
一.Json 报文格式如下: 二.获取 Json 报文中字段的内容 import java.io.IOException; import com.fasterxml.jackson.core.Json ...
- hdfs 路径不支持‘:’
hdfs 路径不支持‘:’,但Linux支持. -put: Pathname /public/platform/control_center/file_center/weiflow/core-user ...
- flexible.js 移动端自适应方案
一,flexible.js 的使用方式: github地址:https://github.com/amfe/lib-flexible 官方文档地址:https://github.com/amfe/ar ...
- float和double的最大值和最小值
- 好大一个坑: EF Core 异步读取大字符串字段比同步慢100多倍
这两天遇到一个奇怪的问题,通过 EF/EF Core 查询数据库速度奇慢,先是在传统的 ASP.NET 项目中遇到(用的是EF6.0),后来将该项目迁移至 ASP.NET Core 也是同样的问题(用 ...
- oracle04 约束,索引
约束(constraint)一.定义 为表的列添加一些约束,以满足用户的需求.在之后的DML(select,insert,update) 数据时,若不满足约束条件,则语句不能执行 注:可以为约束起别名 ...
- Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
安装了mysql5.7,用group by 查询时抛出如下异常: Expression # of SELECT list is not in GROUP BY clause and contains ...