[CF559 C]Gerald and Giant Chess

迷南。 2023-08-17 17:38 255阅读 0赞

题面描述

给定一个\(H*W\)的棋盘,棋盘上只有\(N\)个格子是黑色的,其他格子都是白色的。在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动到黑色格子中。求这个卒从左上角移动到右下角,一共有多少种可能的路线。

输入格式

第\(1\)行:\(3\)个正整数\(h,w,n(1 \leq h,w \leq 10^5,1\leq n \leq 2000)\)。
接下来n行,第\(i+1\)行包含两个整数\(r_i,c_i\),即黑色格子的坐标

输出格式

输出一个整数,即总的方案数。

样例

样例输入

100 100 3
15 16
16 15
99 88

样例输出

545732279

题解

题目的数据范围肯定是不能直接写暴力的(即使是记忆化搜索也会导致空间超限)。因此我们考虑先用组合计数的方式求出总的方案数,再减去不合法的方案数。
在一个正方形矩阵中,我们若想用最小的步数从\((0,0)\)走到\((x,y)\),一共只需要走x+y步,而决定方案数的仅仅是我们选择第\(i\)步是向\(x\)坐标移动还是向\(y\)坐标移动,因此可知总方案数为\(C_{x+y}^{x}\)
我们再减去其中加上所有会到达黑色方块的方案数即可。

  1. #include<bits/stdc++.h>
  2. #define int long long
  3. #define mod 1000000007
  4. #define maxn 200011
  5. using namespace std;
  6. inline char get(){
  7. static char buf[30000],*p1=buf,*p2=buf;
  8. return p1==p2 && (p2=(p1=buf)+fread(buf,1,30000,stdin),p1==p2)?EOF:*p1++;
  9. }
  10. inline int read(){
  11. register char c=get();register int f=1,_=0;
  12. while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();
  13. while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();
  14. return _*f;
  15. }
  16. struct edge{
  17. int x,y;
  18. }a[maxn];
  19. bool cmp(edge x1,edge x2){
  20. if(x1.x==x2.x) return x1.y<x2.y;
  21. return x1.x<x2.x;
  22. }
  23. int inv[maxn],fac[maxn];
  24. int ksm(int a,int b){
  25. int ans=1;
  26. while(b){
  27. if(b&1) ans*=a,ans%=mod;
  28. a*=a,a%=mod;
  29. b>>=1;
  30. }return ans%mod;
  31. }
  32. inline void init(){
  33. inv[0]=1,fac[0]=1;
  34. for(register int i=1;i<maxn;i++){
  35. fac[i]=(fac[i-1]*i)%mod;
  36. inv[i]=ksm(fac[i],mod-2);
  37. }
  38. }
  39. int n,m,k;
  40. int C(int m,int n){
  41. if(n==0)return 1;
  42. return (fac[m]*((inv[n]*inv[m-n])%mod))%mod;
  43. }
  44. int f[maxn];
  45. signed main(){
  46. //freopen("1.txt","r",stdin);
  47. init();
  48. n=read(),m=read(),k=read();
  49. for(register int i=1;i<=k;i++)a[i].x=read(),a[i].y=read();
  50. sort(a+1,a+k+1,cmp);
  51. k++;
  52. a[k].x=n,a[k].y=m;
  53. for(register int i=1;i<=k;i++){
  54. f[i]=C(a[i].x+a[i].y-2,a[i].x-1)%mod;
  55. for(register int j=1;j<i;j++){
  56. if(a[j].x>a[i].x||a[j].y>a[i].y) continue;
  57. f[i]-=f[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x);
  58. f[i]=((f[i]%mod)+mod)%mod;
  59. }
  60. }
  61. cout<<(f[k]%mod+mod)%mod;
  62. }

转载于:https://www.cnblogs.com/Chen574118090/p/11608903.html

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 [CF559 C]Gerald and Giant Chess

    题面描述 给定一个\\(H\W\\)的棋盘,棋盘上只有\\(N\\)个格子是黑色的,其他格子都是白色的。在棋盘左上角有一个卒,每一步可以向右或者向下移动一格,并且不能移动