[CF559C] Gerald and Giant Chess 题解
Ame_wiki
·
·
题解
题面传送门:link。
题意:给定一个 h \times w 的棋盘,上面有 n 个黑点 (x_i,y_i) 不能走,求从 (1,1) 走到 (h,w) 方案数模 10^9+7。(1 \le h,w \le 1 \times 10^5,1 \le n \le 5000)
思路:首先对于没有黑点的情况,设 \text{dis}(n,m) 表示从 (1,1) 走到 (n,m) 的答案,为 C_{n+m}^{n} 或 C_{n+m}^{m},显然我们要求的就是 \text{dis}(h,w)。
爆搜考虑 \Theta(n^2) 的 dp,设 f_i 为走到第 i 个黑点且不经过其他黑点的方案数,这样我们只要把 (h,w) 当成第 n+1 个黑点求 f_{n+1} 即可。
$$f_i=\text{dis}(h,w)- \displaystyle\sum_{j=1}^{i-1}\ [x_i \ge x_j\ \text{and} \ y_i \ge y_j] \ f_j \times \text{dis}(x_i-x_j,y_i-y_j)$$
正确性显然,在处理 $j <i$ 时,经过 $j$ 前面的黑点的情况会被 $k < j<i$ 处理。
记得给 $x_i$ 和 $y_i$ 从小到大排序,注意减法时可能有负数,**$0$ 也要处理逆元**(调一上午)。
### code
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N=1e6+5,M=3005,inf=1e18,mod=1e9+7;
int qpow(int a,int b){ int c=1; while(b){ if(b%2){c=c*a%mod;} b/=2; a*=a; a%=mod; } return c%mod; }
int h,w,n,f[N],fc[N],inv[N];
void intt(){
fc[0]=1ll; inv[0]=qpow(1,mod-2);
for(int i=1;i<=300014;i++) fc[i]=(i*fc[i-1])%mod,inv[i]=qpow(fc[i],mod-2);
return ;
}
int C(int x,int y){
if(y>x) return 0ll;
else return (fc[x]*inv[x-y]%mod*inv[y])%mod;
}
struct kun{int x,y;}a[N];
int cmp(kun q,kun h){ return q.x<h.x||(q.x==h.x&&q.y<h.y); }
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
intt();
cin>>h>>w>>n;
for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y;
a[n+1].x=h;a[n+1].y=w;
sort(a+1,a+n+2,cmp);
for(int i=1;i<=n+1;i++) f[i]=C(a[i].x+a[i].y-2,a[i].x-1);
for(int i=1;i<=n+1;i++){
for(int j=1;j<i;j++){
if(a[i].x<a[j].x||a[i].y<a[j].y) continue;
f[i]-=f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x);
f[i]=(f[i]+mod)%mod;
}
}
cout<<(f[n+1]+mod)%mod;
return 0;
}
```
时间复杂度 $\Theta(n^2)$,计数好题,坑太多。