题目传送门

  传送门I

  传送门II

题目大意

  一个无限大的棋盘上有一只马,设马在某个时刻的位置为$(x, y)$, 每次移动可以将马移动到$(x + A_x, y + A_y)$或者$(x + B_x, y + B_y)$。棋盘上有$n$个禁止位置不能经过,问马从$(0, 0)$走到$(E_x, E_y)$的方案数。

  容斥是显然的。

  每确定经过$k$个禁止位置的方案数的容斥系数是$(-1)^{k}$。

  考虑带上容斥系数来动态规划,

  注意到去掉重复的禁止位置后,$(0, 0), (E_x, E_y)$以及禁止位置构成了一个DAG。

  容斥相当于求从$(0, 0)$到$(E_x, E_y)$的经过偶数个禁止位置的路径数减去经过奇数个禁止位置的路径数。

  直接动态规划计数就好了。

Code

 /**
* bzoj
* Problem#4767
* Accepted
* Time: 333ms
* Memory: 10716k
*/
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
typedef bool boolean; const int N = , M = 1e9 + , D = 1e6 + ; int add(int a, int b) {
return ((a += b) >= M) ? (a - M) : (a);
} int sub(int a, int b) {
return ((a -= b) < ) ? (a + M) : (a);
} int mul(int a, int b) {
return a * 1ll * b % M;
} #define pii pair<int, int>
#define fi first
#define sc second void exgcd(int a, int b, int& x, int& y) {
if (!b)
x = , y = ;
else {
exgcd(b, a % b, y, x);
y -= (a / b) * x;
}
} int inv(int a, int n) {
int x, y;
exgcd(a, n, x, y);
return (x < ) ? (x + n) : (x);
} int fac[D], _fac[D]; inline void prepare() {
fac[] = ;
for (int i = ; i < D; i++)
fac[i] = mul(fac[i - ], i);
_fac[D - ] = inv(fac[D - ], M);
for (int i = D; --i; )
_fac[i - ] = mul(_fac[i], i);
} int comb(int n, int m) {
if (n < m)
return ;
return mul(fac[n], mul(_fac[n - m], _fac[m]));
} boolean Solve(int a1, int b1, int a2, int b2, int c1, int c2, pii& sol) {
int d = c2 * a1 - a2 * c1, f = a1 * b2 - a2 * b1;
if (d % f)
return false;
sol.sc = d / f;
if (a1) {
d = c1 - b1 * sol.sc;
if (d % a1)
return false;
sol.fi = d / a1;
} else {
d = c2 - b2 * sol.sc;
if (d % a2)
return false;
sol.fi = d / a2;
}
return sol.fi >= && sol.sc >= ;
} int n, Ex, Ey;
int Ax, Ay, Bx, By;
pii ps[N];
int deg[N];
set<pii> s;
boolean g[N][N]; //#define _DEBUG_
#ifdef _DEBUG_
FILE* fin = fopen("6.in", "r");
#else
FILE* fin = stdin;
#endif boolean Solve(pii ps, pii pt, pii& sol) {
return Solve(Ax, Bx, Ay, By, pt.fi - ps.fi, pt.sc - ps.sc, sol);
} inline void init() {
fscanf(fin, "%d%d%d", &Ex, &Ey, &n);
fscanf(fin, "%d%d%d%d", &Ax, &Ay, &Bx, &By);
for (int i = ; i <= n; i++) {
fscanf(fin, "%d%d", &ps[i].fi, &ps[i].sc);
if (s.count(ps[i]))
i--, n--;
s.insert(ps[i]);
}
} int f[N];
queue<int> que;
inline void solve() {
int t = n + ;
pii p, S(, ), T(Ex, Ey);
ps[] = S, ps[t] = T;
for (int i = ; i <= n; i++)
if (Solve(S, ps[i], p))
g[][i] = true, deg[i]++;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if ((i ^ j) && Solve(ps[i], ps[j], p))
g[i][j] = true, deg[j]++, assert(!g[j][i]);
for (int i = ; i <= n; i++)
if (Solve(ps[i], T, p))
g[i][t] = true, deg[t]++;
if (Solve(S, T, p))
g[][t] = true, deg[t]++; f[] = ;
que.push();
while (!que.empty()) {
int e = que.front();
que.pop();
for (int i = ; i <= t; i++) {
if (!g[e][i])
continue;
Solve(ps[e], ps[i], p);
if (i && i != t)
f[i] = sub(f[i], mul(f[e], comb(p.fi + p.sc, p.fi)));
else
f[i] = add(f[i], mul(f[e], comb(p.fi + p.sc, p.fi)));
if (!(--deg[i]))
que.push(i);
}
}
// for (int i = 1; i <= n; i++)
// assert(!deg[i]);
// if (deg[i])
// cerr << i << " " << ps[i].fi << " " << ps[i].sc << endl;
printf("%d\n", f[t]);
} int main() {
prepare();
init();
solve();
return ;
}

bzoj 4767 两双手 - 动态规划 - 容斥原理的更多相关文章

  1. bzoj 4767: 两双手 组合 容斥

    题目链接 bzoj4767: 两双手 题解 不共线向量构成一组基底 对于每个点\((X,Y)\)构成的向量拆分 也就是对于方程组 $Ax * x + Bx * y = X $ \(Ay * x + B ...

  2. BZOJ 4767: 两双手 [DP 组合数]

    传送门 题意: 给你平面上两个向量,走到指定点,一些点不能经过,求方案数 煞笔提一开始被题面带偏了一直郁闷为什么方案不是无限 现在精简的题意.....不就是$bzoj3782$原题嘛,还不需要$Luc ...

  3. BZOJ.4767.两双手(组合 容斥 DP)

    题目链接 \(Description\) 棋盘上\((0,0)\)处有一个棋子.棋子只有两种走法,分别对应向量\((A_x,A_y),(B_x,B_y)\).同时棋盘上有\(n\)个障碍点\((x_i ...

  4. BZOJ 4767 两双手

    题解: 发现这种题目虽然可以想出来,但磕磕碰碰得想挺久的 根据数学可以知道组成方案是唯一的(集合) 然后发现每个使用的大小可能是接近n^2的 直接dp(n^4)是过不了的 那么先观察观察 我们可以把每 ...

  5. 【BZOJ】4767: 两双手【组合数学】【容斥】【DP】

    4767: 两双手 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1057  Solved: 318[Submit][Status][Discuss] ...

  6. bzoj4767两双手 容斥+组合

    4767: 两双手 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 684  Solved: 208[Submit][Status][Discuss] ...

  7. 【BZOJ4767】两双手(动态规划,容斥)

    [BZOJ4767]两双手(动态规划,容斥) 题面 BZOJ 题解 发现走法只有两种,并且两维坐标都要走到对应的位置去. 显然对于每个确定的点,最多只有一种固定的跳跃次数能够到达这个点. 首先对于每个 ...

  8. BZOJ4767: 两双手【组合数学+容斥原理】

    Description 老W是个棋艺高超的棋手,他最喜欢的棋子是马,更具体地,他更加喜欢马所行走的方式.老W下棋时觉得无聊,便决定加强马所行走的方式,更具体地,他有两双手,其中一双手能让马从(u,v) ...

  9. Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1747  Solved: 1015[Submit][Stat ...

随机推荐

  1. robotframe中使用report,设置路径带有时间戳

    1.打开C:\Python27\Lib\site-packages\robotide\contrib\testrunner,找到testrunner.py.   修改代码块def _create_te ...

  2. 学习ActiveMQ(一):安装与启动

    一:简单介绍 AvtiveMQ是Apaceh所研发的一个开源消息中间件,用来在服务与服务之间进行异步通信,是基于JMS规范的.activemq包含发送者(sender).消息(message).队列( ...

  3. KeepAlive安装指南

    https://blog.csdn.net/yelllowcong/article/details/78764780

  4. 一键启动frida server的cmd脚本

    和以前写过的在pc直接操作的手机端的sqlite的脚本类似,需要用到重定向的命令 frida-server_helper.bat su /data/local/tmp/frida-server fri ...

  5. cnblogs

    想注册个博客园来着的,看着大佬们的博客都十分漂亮,但是发现我因为太菜没有办法搞定美化问题. 以后再说吧 写写东西,反正也没人看,但是写的时候尽量按给别人看的格式写吧 2019.3.15 开通博客 计划 ...

  6. PL_SQL学习

    打印输出: dbms_output.put_line('AA'); 显示服务器输出信息  set serveroutput on; 打印出eid=1的员工姓名: declare v_name varc ...

  7. webpack.config.js配置遇到Error: Cannot find module '@babel/core'&&Cannot find module '@babel/plugin-transform-react-jsx' 问题

    下文是网上找到的方法,是因为版本冲突的原因,参照后安装7版本解决 cnpm install -D babel-loader@ babel-core babel-preset-env 一. 问题描述 在 ...

  8. 意外的php之学习笔记

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gc_gongchao/article/details/37312039     什么是php? ph ...

  9. html-webpack-plugin插件使用时参数配置

    ERROR in multi main Module not found: Error: Cannot resolve 'file' or 'directory' ./public/pages/ind ...

  10. #WEB安全基础 : HTTP协议 | 文章索引

    本系列讲解WEB安全所需要的HTTP协议 #WEB安全基础 : HTTP协议 | 0x0 TCP/IP四层结构 #WEB安全基础 : HTTP协议 | 0x1 TCP/IP通信 #WEB安全基础 : ...