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

## 4767: 两双手

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1057  Solved: 318
[Submit][Status][Discuss]

## Description

)呢？两种移动方法不同当且仅当移动步数不同或某一步所到达的点不同。老W听了这个问题，觉得还不够有趣，他

## Input

|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500

4 4 1
0 1 1 0
2 3

## Sample Output

40

Solution

【注意】预处理阶乘的逆元线性从后往前线性求得，不然会TLE！

Code

#include<bits/stdc++.h>
#define mod 1000000007
#define LL long long
using namespace std;

int Ax, Ay, Bx, By, n, Ex, Ey;

struct Node {
int x, y;
} QAQ[1005];
bool cmp(Node a, Node b) { if(a.x == b.x)    return a.y < b.y; return a.x < b.x; }

LL mpow(LL a, LL b) {
LL ans = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1)    ans = ans * a % mod;
return ans;
}

void cal(int &x, int &y) {////解方程计算两种步数
LL a1, a2, b1, b2;
b1 = y * Ax - x * Ay, b2 = Ax * By - Ay * Bx;
a1 = x * By - y * Bx, a2 = Ax * By - Ay * Bx;
if(a2 == 0 || b2 == 0) { x = -1, y = -1; return ; }
if((a1 / a2) * a2 != a1 || (b1 / b2) * b2 != b1) { x = -1, y = -1; return ; }
x = a1 / a2, y = b1 / b2;
}

LL fac[1000005], inv[1000005];
LL C(LL a, LL b) {
if(a < b)    return 0;
return fac[a] * inv[a-b] % mod * inv[b] % mod;
}

void init() {
fac[0] = 1;
for(int i = 1; i <= 1000000; i ++)
fac[i] = fac[i-1] * i % mod;
inv[0] = 1; inv[1000000] = mpow(fac[1000000], mod - 2);
for(int i = 999999; i >= 1; i --)
inv[i] = inv[i + 1] * (i + 1) % mod;////线性求阶乘逆元
}

LL f[1005];
int main() {
scanf("%d%d%d", &Ex, &Ey, &n);
scanf("%d%d%d%d", &Ax, &Ay, &Bx, &By);
cal(Ex, Ey);
for(int i = 1; i <= n; i ++) {
scanf("%d%d", &QAQ[i].x, &QAQ[i].y);
cal(QAQ[i].x, QAQ[i].y);
if(QAQ[i].x < 0 || QAQ[i].y < 0 || QAQ[i].x > Ex || QAQ[i].y > Ey) {/////不合法的步数筛掉
n --; i --;
}
}
QAQ[++n].x = Ex, QAQ[n].y = Ey;
sort(QAQ + 1, QAQ + 1 + n, cmp);

init();

for(int i = 1; i <= n; i ++) {
f[i] = C(QAQ[i].x + QAQ[i].y, QAQ[i].x);
if(f[i] == 0)    continue;
for(int j = 1; j < i; j ++) {
f[i] -= (f[j] * C(QAQ[i].x - QAQ[j].x + QAQ[i].y - QAQ[j].y, QAQ[i].x - QAQ[j].x)) % mod;/////容斥
f[i] = (f[i] % mod + mod) % mod;
}
}
printf("%lld", f[n]);
return 0;
}
posted @ 2018-10-14 16:00  Wans_ovo  阅读(182)  评论(0编辑  收藏  举报