梦回高中,定义的f(i,j)为从(0,0)到(i,j)一共有多少条路可以选择,易知我们要做i+j次选择,其中有i次是选择x轴,剩下的是y轴,所以f(i,j)=C(i+j,i)=C(i+j,j),给你一个范围[r1,r2],[c1,c2],求出所有的f(i,j)之和,我们可以用容斥,设g(r,c)为范围[0,r][0,c]的f之和,那么答案就是g(r2,c2)-g(r2,c1-1)-g(r1-1,c2)+g(r1-1,c1-1),目标就转换为快速求g,由组合数公式,g(r,c)就可以从r*c个f和变成r个f和,g(r,c) = f(0,0)+f(0,1)+~~~+f(0,c)+~~~+f(r,0)+f(r,1)+~~~+f(r,c),f(0,0)+f(0,1)+~~~+f(0,c)=f(1,c)=C(c+1,c), f(r,0)+f(r,1)+~~~+f(r,c)=f(r+1,c)=C(c+1+r,c) 这样复杂度就是O(n),预处理后每个组合数是O(1)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define lowbit(x) ((x)&(-x))
  4. typedef long long LL;
  5.  
  6. const int MOD = 1e9+;
  7. const int maxm = 2e6+;
  8.  
  9. LL F[maxm], Finv[maxm], inv[maxm];
  10.  
  11. LL comb(int n, int m) { //C(n, m)
  12. if(m < || m > n) return ;
  13. return F[n]*Finv[n-m]%MOD*Finv[m]%MOD;
  14. }
  15.  
  16. void run_case() {
  17. int r1, c1, r2, c2, n;
  18. cin >> r1 >> c1 >> r2 >> c2;
  19. n = c2+r2+;
  20. inv[] = ;
  21. for(int i = ; i <= n; ++i)
  22. inv[i] = (MOD - MOD / i) * 1LL * inv[MOD % i] % MOD;
  23. F[] = Finv[] = ;
  24. for(int i = ; i <= n; ++i) {
  25. F[i] = F[i-] *1LL* i % MOD;
  26. Finv[i] = Finv[i-] * 1LL * inv[i] % MOD;
  27. }
  28. LL ans1=,ans2=,ans3=,ans4=;
  29. for(int i = ; i <= r2; ++i) (ans1+=comb(c2++i,c2))%=MOD;
  30. for(int i = ; i <= r2; ++i) (ans2+=comb(c1+i,c1-))%=MOD;
  31. for(int i = ; i <= r1-; ++i) (ans3+=comb(c2++i,c2))%=MOD;
  32. for(int i = ; i <= r1-; ++i) (ans4+=comb(c1+i,c1-))%=MOD;
  33. cout << (ans1+ans4-ans2-ans3+MOD)%MOD;
  34. }
  35.  
  36. int main() {
  37. ios::sync_with_stdio(false), cin.tie();
  38. //cout.setf(ios_base::showpoint);cout.precision(8);
  39. run_case();
  40. cout.flush();
  41. return ;
  42. }

ABC154F - Many Many Paths的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. LeetCode-62-Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  9. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

随机推荐

  1. python中的基本类型

    字符串类型 字符串相当于羊肉串,而字符串就是一串被串起来的字符,在单引号.双引号或三引号内包裹一串字符.需要注意的是:三引号内的字符串可以换行,而单引号内的字符不行 name='anny' name= ...

  2. 使用 yum 安装 MariaDB 与 MariaDB 的简单配置

    1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB,两条命令都可以 systemctl sta ...

  3. Redis搭建一主一从及哨兵模式(二)

    废话不多说,直接进入正题. 一.主从搭建 进入redis的根目录,拷贝一份redis.conf,最初的一份留作模版: ①cp  redis.conf redis.conf.template ②vim ...

  4. 移动端rem自适应方案

    一般设计师给我们的设计稿尺寸都为750*1340 .. 网易,淘宝移动端首页上html元素的font-size 目前就先说一下网易的做法 引入下面这段js,用于计算动态的font-size (func ...

  5. 火爆微信朋友圈的Excel速成班视频课程

    Excel速成班视频课程,一共有10节课,附带课件. 目录结构如下: 目录:/2020032-Excel速成班视频 [4.6G] ┣━━课件 [1.9M] ┃ ┣━━第八课Excel实用技巧12例.x ...

  6. ubuntu中数据迁移的时候出现ImportError错误

    今天在Flask框架中使用数据库迁移的时候出现了如下错误: ImportError: libmysqlclient.so.20: cannot open shared object file: No ...

  7. 基于scikitlearn的深度学习环境安装(三)(完整版)

    OS Linux  Ubuntu14.04 安装 pip (python2.7.9或以上自带pip) sudo apt-get install python-pip pip是python环境下安装包的 ...

  8. HDU1540 Turnal Warfare

    线段树保存每个区间的左边最大连续长度和右边最大连续长度~ #include<cstdio> #include<cstring> #include<algorithm> ...

  9. zk的单机部署,与客户端的使用

    下载zk wget https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz 安装jdk t ...

  10. 转发-【分享】思科无线控制器HA模式升级

    思科无线控制器HA模式下升级文档   当前使用版本:8.0.120.0 计划升级版本:8.2.151.0   其他工具: TFTP Server: 3CDaemon 远程登录:SecureCRT   ...