【CF559C】 Gerald and Giant Chess(计数,方案数DP,数论)
题意:给出一个棋盘为h*w,现在要从(1,1)到(h,w),其中有n个黑点不能走,问有多少种可能从左上到右下
(1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000),答案模10^9+7
思路:从(1,1)到(n,m)的方案数是c(n+m-2,n-1)
考虑不能走黑点
设dp[i]为从(1,1)走到(x[i],y[i]),中途没有走过任何一个黑点的方案数
dp[i]=dp[i]-dp[j]*c(x[i]+y[i]-x[j]-y[j],x[i]-x[j]) (x[j]<=x[i]且y[j]<=y[i])
除以阶乘取模用线性求逆元
const mo=;
var x,y:array[..]of longint;
dp:array[..]of int64;
exf,fac:array[..]of int64;
n,m,k,i,j:longint; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function c(x,y:longint):int64;
begin
// if y= then exit();
exit(fac[x]*exf[y] mod mo*exf[x-y] mod mo);
end; procedure qsort(l,r:longint);
var i,j,mid1,mid2:longint;
begin
i:=l; j:=r; mid1:=x[(l+r)>>]; mid2:=y[(l+r)>>];
repeat
while (mid1>x[i])or(mid1=x[i])and(mid2>y[i]) do inc(i);
while (mid1<x[j])or(mid1=x[j])and(mid2<y[j]) do dec(j);
if i<=j then
begin
swap(x[i],x[j]);
swap(y[i],y[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; begin
// assign(input,'1.in'); reset(input);
//assign(output,'1.out'); rewrite(output);
readln(n,m,k);
for i:= to k do read(x[i],y[i]);
inc(k); x[k]:=; y[k]:=;
inc(k); x[k]:=n; y[k]:=m;
qsort(,k);
fac[]:=;
for i:= to n+m do fac[i]:=fac[i-]*i mod mo;
exf[]:=; exf[]:=;
for i:= to n+m do exf[i]:=exf[mo mod i]*(mo-mo div i) mod mo;
for i:= to n+m do exf[i]:=exf[i-]*exf[i] mod mo;
dp[]:=;
for i:= to k do
begin
dp[i]:=c(x[i]+y[i]-,x[i]-);
for j:= to i- do
if y[j]<=y[i] then
begin
dp[i]:=dp[i]-dp[j]*c(x[i]-x[j]+y[i]-y[j],x[i]-x[j]);
dp[i]:=(dp[i] mod mo+mo) mod mo;
end;
end;
writeln(dp[k]); //close(input);
//close(output);
end.
【CF559C】 Gerald and Giant Chess(计数,方案数DP,数论)的更多相关文章
- CF559C Gerald and Giant Chess
题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...
- $CF559C\ Gerald\ and\ Fiant\ Chess$ 计数类$DP$
AcWing Description 有个$H$行$W$列的棋盘,里面有$N$个黑色格子,求一个棋子由左上方格子走到右下方格子且不经过黑色格子的方案数. $1<=H,M<=1e5,1< ...
- cf559C. Gerald and Giant Chess(容斥原理)
题意 $h \times w$的网格,有$n$个障碍点, 每次可以向右或向下移动 求从$(1, 1)$到$(h, w)$不经过障碍点的方案数 Sol 容斥原理 从$(1, 1)$到$(h, w)$不经 ...
- CF-559C Gerald and Giant Chess(计数DP)
给定一个 \(H*W\)的棋盘,棋盘上只有\(N\) 个格子是黑色的,其他格子都是白色的. 在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中.求这个卒从左上角移动到右下角 ...
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP
C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...
随机推荐
- 虚拟机设置NAT
需要开启虚拟机网络相关服务, 安装虚拟网卡, 还有必须安装 VMware Tools VMware虚拟机下实现NAT方式上网1. 把你的虚拟网卡VMnet8设置为自动获得IP.自动获得DNS服务器,启 ...
- RabbitMQ 学习资料
https://www.rabbitmq.com/getstarted.html http://www.cnblogs.com/luxiaoxun/p/3918054.html http://back ...
- C#基于联通短信Sgip协议构建短信网关程序
此软件基于中国联通Sgip协议程序接口,适合在中国联通申请了短信发送端口的公司使用.短信群发已经成为现在软件系统.网络营销等必不可少的应用工具.可应用在短信验证.信息群发.游戏虚拟商品购买.事件提醒. ...
- 通过luac编译lua脚本
在lua官网下载一个lua的release包,里面有已经编译好的lua启动文件(包括lua.exe),其中还有luac.exe, 这个程序是用来将lua文件编译成二进制码, 使用方法很简单,在控制台调 ...
- mysql 安装简介
Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...
- 看了下opengl相关的资料,踩了一个坑,记录一下
2019/03/10 下午看了下关于opengl的资料,然后把敲了下代码,然后程序报错了.代码如下: #include <glad/glad.h> #include <GLFW/gl ...
- 阿里云服务器 - node启动服务的时候只有监听内网IP才可以供外网访问
阿里云服务器 - node启动服务的时候只有监听内网IP才可以供外网访问
- 02 Django框架基础(APP的创建访问)
一.创建项目 1.命令:django-admin startproject sitename 2.IDLE环境:本质上都是执行上述命令 常用命令: python manage.py runserver ...
- destoon 短信发送函数及短信接口修改
// $DT在common.inc.php中定义, $CACHE = cache_read('module.php'); $DT = $CACHE['dt']; 从缓存里读取网站配置信息. //$d ...
- Install GStreamer on Ubuntu
apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-p ...