梦回高中,定义的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)

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int MOD = 1e9+;
const int maxm = 2e6+; LL F[maxm], Finv[maxm], inv[maxm]; LL comb(int n, int m) { //C(n, m)
if(m < || m > n) return ;
return F[n]*Finv[n-m]%MOD*Finv[m]%MOD;
} void run_case() {
int r1, c1, r2, c2, n;
cin >> r1 >> c1 >> r2 >> c2;
n = c2+r2+;
inv[] = ;
for(int i = ; i <= n; ++i)
inv[i] = (MOD - MOD / i) * 1LL * inv[MOD % i] % MOD;
F[] = Finv[] = ;
for(int i = ; i <= n; ++i) {
F[i] = F[i-] *1LL* i % MOD;
Finv[i] = Finv[i-] * 1LL * inv[i] % MOD;
}
LL ans1=,ans2=,ans3=,ans4=;
for(int i = ; i <= r2; ++i) (ans1+=comb(c2++i,c2))%=MOD;
for(int i = ; i <= r2; ++i) (ans2+=comb(c1+i,c1-))%=MOD;
for(int i = ; i <= r1-; ++i) (ans3+=comb(c2++i,c2))%=MOD;
for(int i = ; i <= r1-; ++i) (ans4+=comb(c1+i,c1-))%=MOD;
cout << (ans1+ans4-ans2-ans3+MOD)%MOD;
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(8);
run_case();
cout.flush();
return ;
}

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. selenium Python实现附件上传

    对于web页面的上传功能一般有两类实现方式:一类是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器:另一个类是插件上传,一般基于flash/javascript或者 ...

  2. RTT学习之软件包

    网络工具集 (NetUtils) Ping 工具: 是一种网络诊断工具,用来测试数据包能否通过 IP 协议到达特定主机,依赖于LWIP,支持域名和IP访问: NTP 工具:NTP 是网络时间协议 (N ...

  3. Git管理代码

    使用Git管理代码 1. 分支管理模式 首先,master分支应该是非常稳定的,开发都在dev分支上,每个人都有自己的分支,时不时地往dev分支上合并就可以了.完成测试后,再把dev分支合并到mast ...

  4. JS-try/catch方法判断字符串是否为json格式

    定义: try/catch 语句用于处理代码中可能出现的错误信息,出现异常会导致程序崩溃,而try/catch 则可以保证程序的正常运行. try { //执行代码 不报错则 正常执行 不会进入下面的 ...

  5. 初探three.js几何体-Geometry

    three.js几何体我们还没有说完,这一节我们说一说THREE.Geometry(),简单几何体都是继承了这个对象,使用它会相对麻烦一些,但是可操作性非常高,今天我们使用它制作一个自定义几何体-五角 ...

  6. CSS中 - display: inline-block

    参考 https://stackoverflow.com/questions/9189810/css-display-inline-vs-inline-block An inline-block el ...

  7. Steam 游戏 《Sudoku Universe(数独宇宙)》、《Sudoku Killer(数独杀手)》、《Sudoku Jigsaw(数独拼图)》数字位置解析 ---------C# 数独程序解析(2020年寒假小目标11)

    日期:2020.02.11 博客期:151 星期二 今天,准备肝一个 C# 的数独读写工具(汇编语言也在努力学习命令方法中...),这三个游戏我早就买下了,一直放在 Steam 库里积灰,看着它的成就 ...

  8. 常用的php函数

    最严格身份证号码验证,支持15位和19世纪出生的人的身份证号码 # 计算身份证校验码,根据国家标准GB 11643-1999 function idcard_verify_number($idcard ...

  9. JQuery选择器&过滤器

    JQuery对象: JQuery对象的本质上是DOM数组,它对DOM元素进行了封装 JQuery对象和JavaScript对象可以互转(\$()/$obj()[i]),但是JQuery对象和Javas ...

  10. ES6-const定义常量

    在es5中我们一般将变量名大写来表明这是一个常量,但其实它是可以修改的. 在es6中可以用const来定义常量,它定义的常量不能修改.     const NAME = 'tom';     NAME ...