codeforces 560E Lucas定理+dp

太过爱你忘了你带给我的痛 2022-07-19 00:19 292阅读 0赞

E. Gerald and Giant Chess

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we’ll just say that the game takes place on anh × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.

Input

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).

Next n lines contain the description of black cells. The i-th of these lines contains numbers r**i, c**i (1 ≤ r**i ≤ h, 1 ≤ c**i ≤ w) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.

Output

Print a single line — the remainder of the number of ways to move Gerald’s pawn from the upper left to the lower right corner modulo109 + 7.

Examples

input

  1. 3 4 2
  2. 2 2
  3. 2 3

output

  1. 2

input

  1. 100 100 3
  2. 15 16
  3. 16 15
  4. 99 88

output

  1. 545732279

题意:求从1,1到n,m的方法数,每次可以从当前的点向右边移动一格,或者向下移动一格,但是有的格子是坏的,不能经过这些点。

分析:考虑dp来做,把坏的点排序,dp[i]表示到第i个坏的点但是不经过其他坏点有多少种方法,最后添加一个坏点n,m正好最后求dp【k】就是答案,统计dp[i]的过程中先算出从1,1到这点的组合数,然后减去x跟y都要比当前的点小的坏点到这个点的组合数,由于组合数中n,m都比较大,因此要用lucas定理来求。

  1. #include<bitset>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. #define F first
  16. #define S second
  17. using namespace std;
  18. typedef long long ll;
  19. typedef pair<int,int> pii;
  20. inline int in()
  21. {
  22. int res=0;char c;int f=1;
  23. while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
  24. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  25. return res*f;
  26. }
  27. const int N=100010,MOD=1e9+7;
  28. ll dp[2345],fac[N<<1];
  29. ll power(ll a,ll n,ll p)
  30. {
  31. ll ret=1;
  32. while(n)
  33. {
  34. if(n & 1) ret = ret*a%p;
  35. a = a*a%p;
  36. n >>= 1;
  37. }
  38. return ret;
  39. }
  40. ll C(ll n,ll m, ll p)
  41. {
  42. if(m>n) return 0;
  43. return fac[n]*power(fac[m]*fac[n-m]%p,p-2,p)%p;
  44. }
  45. ll Lucas(ll n,ll m,ll p)
  46. {
  47. if(m==0) return 1;
  48. return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p;
  49. }
  50. pii a[2345];
  51. int main()
  52. {
  53. fac[0]=1;
  54. for(int i=1;i<N<<1;i++){ //
  55. fac[i] = fac[i-1]*i%MOD;
  56. }
  57. int n=in(),m=in(),k=in();
  58. for(int i=0;i<k;i++){
  59. a[i].F=in(),a[i].S=in();
  60. }
  61. a[k].F=n,a[k].S=m;
  62. sort(a,a+k);
  63. for(int i=0;i<=k;i++){
  64. dp[i] = Lucas(a[i].F-1+a[i].S-1,a[i].F-1,MOD);
  65. for(int j=0;j<i;j++){
  66. if(a[j].S > a[i].S) continue;
  67. dp[i] = (dp[i]-Lucas(a[i].F-a[j].F+a[i].S-a[j].S,a[i].F-a[j].F,MOD)*dp[j]+MOD)%MOD;
  68. }
  69. }
  70. dp[k] += MOD;
  71. dp[k] %= MOD;
  72. cout<<dp[k]<<endl;
  73. return 0;
  74. }

发表评论

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

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

相关阅读

    相关 Codeforces 750E 线段树DP

    题意:给你一个字符串,有两种操作:1:把某个位置的字符改变。2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树

    相关 Codeforces 735E 树形DP

    题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp\[x\]\

    相关 Codeforces 722E 组合数学 DP

    题意:有一个n \ m的棋盘,你初始在点(1, 1),你需要去点(n, m)。你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终

    相关 Lucas定理模板

    Lucas定理是用来求解C(m,n)mod p的值的。其中m和n的值可以很大,p一定是素数。 对阶乘打表的模板 LL quick_mod(LL a, LL b, L