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

  思路:若是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. Ackerman

    Ackerman 递归算法 一 . 问题描述及分析 图1 二 . 代码实现 package other; import java.io.BufferedWriter; import java.io.F ...

  2. Fragment概述

    1 Fragment Fragment是什么? Fragment允许将Activity拆分成多个完全独立封装的可重用的组件,每个组件有它自己的生命周期和UI布局. 每个Fragment都是独立的模块, ...

  3. 线段树 区间开方区间求和 & 区间赋值、加、查询

    本文同步发表于 https://www.zybuluo.com/Gary-Ying/note/1288518 线段树的小应用 -- 维护区间开方区间求和 题目传送门 约定: sum(i,j) 表示区间 ...

  4. 做rl_abs过程中遇到的问题

    问题一 运行 train_abstractor.py就出现这个问题 nohup: ignoring input start training with the following hyper-para ...

  5. 201771010118马昕璐《面向对象程序设计java》第八周学习总结

    第一部分:理论知识学习部分 1.接口 在Java程序设计语言中,接口不是类,而是对类的一组需求描述,由常量和一组抽象方法组成.Java为了克服单继承的缺点,Java使用了接口,一个类可以实现一个或多个 ...

  6. error MSB8020 问题解决

    产生原因: 1.vs 版本过低 2.项目平台工具选择不正确 解决方案: 1.安装VS2015以上的版本 2.选择项目属性,修改平台工具,选择当前版本可用的工具. 具体步骤:右键点击你的项目,选择 Pr ...

  7. editplus注册码生成

    http://www.jb51.net/tools/editplus/ 主要JS代码: function generate_editplus_regcode() { var list = [0,493 ...

  8. python语法_元组

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

  9. 为什么局部变量必须以final修饰(或者有final实效:java8)才可以在内部类中使用?

    为什么局部变量必须以final修饰(或者有final实效:java8)才可以在内部类中使用? public class Ace { public static void main(String[] a ...

  10. SSM 记录

    前言:本过程从0开始,先是导入最核心的jar包,然后随着ssm中的功能实现,打包===>启动===>报错,一步步解决问题,增加额外的必须的jar包来熟悉ssm 1.导包(核心包) myba ...