题目链接:http://codeforces.com/contest/560/problem/E

给你一个n*m的网格,有k个坏点,问你从(1,1)到(n,m)不经过坏点有多少条路径。

先把这些坏点排序一下。

dp[i]表示从(1,1)到第i个坏点且不经过其他坏点的路径数目。

dp[i] = Lucas(x[i], y[i]) - sum(dp[j]*Lucas(x[i]-x[j], y[i]-x[j])) , x[j] <= x[i] && y[j] <= y[i] //到i点所有的路径数目 - 经过其他点的路径数目

  1. //#pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <list>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef __int64 LL;
  15. typedef pair <int, int> P;
  16. const int N = 2e3 + ;
  17. struct Node {
  18. LL x, y;
  19. bool operator <(const Node &cmp) const {
  20. return x == cmp.x ? y < cmp.y : x < cmp.x;
  21. }
  22. }node[N];
  23. LL mod = 1e9 + ;
  24. LL dp[N]; //经过i点不经过其他点的case数
  25. LL f[]; //阶乘
  26.  
  27. LL Pow(LL a , LL n , LL mod) {
  28. LL res = ;
  29. while(n) {
  30. if(n & )
  31. res = res * a % mod;
  32. a = a * a % mod;
  33. n >>= ;
  34. }
  35. return res;
  36. }
  37.  
  38. LL Comb(LL a , LL b , LL mod) {
  39. if(a < b) {
  40. return ;
  41. }
  42. if(a == b) {
  43. return ;
  44. }
  45. return f[a] * Pow(f[a - b] * f[b] % mod, mod - , mod) % mod;
  46. }
  47.  
  48. LL Lucas(LL n , LL m , LL mod) {
  49. LL ans = ;
  50. while(m && n && ans) {
  51. ans = (ans * Comb(n % mod , m % mod , mod)) % mod;
  52. n /= mod;
  53. m /= mod;
  54. }
  55. return ans;
  56. }
  57.  
  58. int main()
  59. {
  60. f[] = ;
  61. for(LL i = ; i <= ; ++i) {
  62. f[i] = f[i - ] * i % mod;
  63. }
  64. LL row, col, sum;
  65. int n;
  66. scanf("%lld %lld %d", &row, &col, &n);
  67. for(int i = ; i <= n; ++i) {
  68. scanf("%lld %lld", &node[i].x, &node[i].y);
  69. node[i].x--, node[i].y--;
  70. }
  71. sort(node + , node + n + );
  72. for(int i = ; i <= n; ++i) {
  73. sum = ;
  74. for(int j = ; j < i; ++j) {
  75. if(node[i].x >= node[j].x && node[i].y >= node[j].y) {
  76. sum = (dp[j]*Lucas(node[i].x + node[i].y - node[j].x - node[j].y, node[i].x - node[j].x, mod) % mod + sum) % mod;
  77. }
  78. }
  79. dp[i] = ((Lucas(node[i].x + node[i].y, node[i].x, mod) - sum) % mod + mod) % mod;
  80. }
  81. sum = ;
  82. for(int i = ; i <= n; ++i) {
  83. sum = (dp[i]*Lucas(row + col - - node[i].x - node[i].y, row - - node[i].x, mod) % mod + sum) % mod;
  84. }
  85. printf("%lld\n", ((Lucas(row + col - , col - , mod) - sum) % mod + mod) % mod);
  86. return ;
  87. }

Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess (Lucas + dp)的更多相关文章

  1. dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...

  2. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP

    C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  3. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    这场CF又掉分了... 这题题意大概就给一个h*w的棋盘,中间有一些黑格子不能走,问只能向右或者向下走的情况下,从左上到右下有多少种方案. 开个sum数组,sum[i]表示走到第i个黑点但是不经过其他 ...

  4. Codeforces Round #313 (Div. 1) A. Gerald's Hexagon

    Gerald's Hexagon Problem's Link: http://codeforces.com/contest/559/problem/A Mean: 按顺时针顺序给出一个六边形的各边长 ...

  5. Codeforces Round #313 (Div. 2) C. Gerald's Hexagon 数学

    C. Gerald's Hexagon Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/pr ...

  6. Codeforces Round #313 (Div. 1) A. Gerald's Hexagon 数学题

    A. Gerald's Hexagon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/p ...

  7. Codeforces Round #313 (Div. 2) B. Gerald is into Art 水题

    B. Gerald is into Art Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560 ...

  8. 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald&#39;s Hexagon

    [CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/C 题面: C. Gerald's Hexagon time limit per tes ...

  9. Codeforces Round #313 (Div. 2) C. Gerald&#39;s Hexagon(补大三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. bzoj3668: [Noi2014]起床困难综合症

    从高位到低位枚举期望的应该是ans最高位尽量取一.如果该数最高位为o的话能够取得1直接更新ans否则判断该位取1是否会爆m不会的话就加上. #include<cstdio> #includ ...

  2. POJ 1976 A Mini Locomotive【DP】

    题意:给出一列火车,可以由三个火车头拉,每个火车头最多拉m节车厢(这m节车厢需要保持连续),再给出n节车厢,每节车厢的人数,问最多能够载多少人到终点. 可以转化为三个长度相等的区间去覆盖n个数,使得这 ...

  3. linux sed 命令

    转载:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ...

  4. Vim+Ctags+Taglist组合:

    Ctags 1,sudo apt-get install Ctags //会提示最新版本的名字:Exuberant Ctags 2,在源码的最上层目录执行:ctags -R //会在当前目录先生成一个 ...

  5. CentOS 6安装mock

    最近工作中需要用到mock,这里介绍两种安装方式.本文的环境为CentOS 6.4 x86_64. 一,使用yum安装mock 安装第三方yum源RPMForge Centos5 64位 wget h ...

  6. 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较

    一.SQL获取系统时间 mysql> select now() from dual; +---------------------+ | now() | +------------------- ...

  7. Windows服务调用Quartz.net 实现消息调度

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  8. PreferenceActivity使用方法

              public class MainActivity extends Activity { @Override protected void onCreate(Bundle save ...

  9. 推荐一个网站Stack Overflow

    网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...

  10. 在 Asp.NET MVC 中使用 SignalR 实现推送功能

    一,简介Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送请 ...