Description
老W是个棋艺高超的棋手,他最喜欢的棋子是马,更具体地,他更加喜欢马所行走的方式。老W下棋时觉得无聊,便决定加强马所行走的方式,更具体地,他有两双手,其中一双手能让马从(u,v)移动到(u+Ax,v+Ay)而另一双手能让马从(u,v)移动到(u+Bx,v+By)。小W看见老W的下棋方式,觉得非常有趣,他开始思考一个问题:假设棋盘是个无限大的二维平面,一开始马在原点(0,0)上,若用老W的两种方式进行移动,他有多少种不同的移动方法到达点(Ex,Ey)呢?两种移动方法不同当且仅当移动步数不同或某一步所到达的点不同。老W听了这个问题,觉得还不够有趣,他在平面上又设立了n个禁止点,表示马不能走到这些点上,现在他们想知道,这种情况下马有多少种不同的移动方法呢?答案数可能很大,你只要告诉他们答案模(10^9+7)的值就行。
Solution
首先,对于两个向量张成的平面,我们可以将它们线性组合来表示成平面上任意一个点。
可以通过解方程得到的是正整数解,那么我们可以从一个点移动到另一个点。如果
C⃗ =pA⃗ +qB⃗
C
→
=
p
A
→
+
q
B
→
,那么一共有
(p+qq)
(
p
+
q
q
)
种方案。
对于这个题目,我们可以先算出不考虑障碍的移动方案数再减去不合法的数量。将障碍按
p+q
p
+
q
降序排列,这样就可以保证如果可以从
A
A
点走到点,那么
A
A
一定在之前。
设
fi
f
i
表示从起点不经过任何障碍走到
i
i
的方案数。从前往后考虑,如果发现可以从之前某个点走到这个点,那么
fi
f
i
应该减去
fj×(p+qq)
f
j
×
(
p
+
q
q
)
,其中
(p+qq)
(
p
+
q
q
)
表示从
j
j
走到的方案数。于是我们可以得到
fi
f
i
再乘上从该障碍走到终点的方案即为不合法的数量(不需要再考虑后面的障碍了,因为我们每次只会计算以一个障碍为开头的方案数)。
Code
/************************************************
* Au: Hany01
* Date: Jul 10th, 2018
* Prob: [BZOJ4767] 两双手
* Email: hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 505, Mod = 1e9 + 7, maxm = 1e6;
const double eps = 1e-5;
int n, N;
LL fac[maxm << 1], ifac[maxm << 1], f[maxn], Ans;
inline LL Pow(LL a, LL b) { LL Ans = 1; for ( ; b; b >>= 1, (a *= a) %= Mod) if (b & 1) (Ans *= a) %= Mod; return Ans; }
struct Point {
int x, y, dx, dy, fl;
bool operator < (const Point& A) const { return dx + dy < A.dx + A.dy; }
} T, A, B, P[maxn], tmp;
inline int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; }
inline bool calc(Point &P) {
//a * ax + bx * b = px
//a * ay + by * b = py
//===>
//a + bx / ax * b = px / ax
//a + by / ay * b = py / ay
double a, b, ax = A.x, ay = A.y, bx = B.x, by = B.y, px = P.x, py = P.y;
if (A.x == 0) b = px / bx, a = (py - b * by) / ay;
else if (A.y == 0) b = py / by, a = (px - b * bx) / ax;
else b = (px / ax - py / ay) / (bx / ax - by / ay), a = (px - b * bx) / ax;
if (dcmp(a - round(a)) || dcmp(b - round(b)) || dcmp(b) < 0 || dcmp(a) < 0) return 0;
P.dx = round(a), P.dy = round(b), chkmax(N, P.dx + P.dy);
return 1;
}
inline LL C(int n, int m) { return fac[n] * ifac[m] % Mod * ifac[n - m] % Mod; }
int main()
{
#ifdef hany01
File("bzoj4767");
#endif
T.x = read(), T.y = read(), n = read(), A.x = read(), A.y = read(), B.x = read(), B.y = read();
if (!calc(T)) { puts("0"); return 0; }
for (int i = 1; i <= n; ++ i) {
P[i].x = read(), P[i].y = read();
if (!calc(P[i])) -- i, -- n;
}
sort(P + 1, P + 1 + n);
//f[i] stands for the number of ways to reach node i without passing by any obstruction.
fac[0] = 1;
For(i, 1, N) fac[i] = fac[i - 1] * i % Mod;
ifac[N] = Pow(fac[N], Mod - 2);
Fordown(i, N, 1) ifac[i - 1] = ifac[i] * i % Mod;
Ans = C(T.dx + T.dy, T.dx);
For(i, 1, n) {
if (P[i].dx + P[i].dy > T.dx + T.dy) break;
tmp.x = T.x - P[i].x, tmp.y = T.y - P[i].y;
if (!calc(tmp)) continue;
f[i] = C(P[i].dx + P[i].dy, P[i].dx);
For(j, 1, i - 1) {
tmp.x = P[i].x - P[j].x, tmp.y = P[i].y - P[j].y;
if (calc(tmp)) (f[i] -= f[j] * C(tmp.dx + tmp.dy, tmp.dx) % Mod) %= Mod;
}
tmp.x = T.x - P[i].x, tmp.y = T.y - P[i].y;
calc(tmp);
(Ans -= f[i] * C(tmp.dx + tmp.dy, tmp.dx) % Mod) %= Mod;
}
printf("%lld\n", (Ans + Mod) % Mod);
return 0;
}
//南风知我意,吹梦到西洲。
// -- 佚名《西洲曲》