ABC154F - Many Many Paths
梦回高中,定义的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的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- 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 ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
随机推荐
- STM32开发记录
一.使用keil时经常遇到的问题 1.没有下载固件包 2.下载器没选择对 3.没安装下载器的驱动 二.关于数据手册和开发手册 1.如何下载这些手册 在官网下载,官网永远是一手资源 意法半导体官网:ww ...
- thinkphp 连接webservice接口
嗯,我现在真的好像骂人啊,但是我又是个文明的人,所以我就写出来让自己冷静一下 ok,正事,thinkphp连别人写的webservice接口 刚开始他叫什么nc接口,就把我给骗了,这就是人家的名字,和 ...
- 「luogu3380」【模板】二逼平衡树(树套树)
「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...
- ubuntu 虚拟机添加多个站点
我们安装好lamp环境,然后开始操作,比如一个站点叫test.ubuntu1.com,一个叫test.ubuntu2.com 1.修改hosts文件,路径/etc/hosts sudo vim /et ...
- echart中饼状图的高亮显示。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- vue-cli项目结构详解
vue-cli的webpack模板项目配置文件分析 https://blog.csdn.net/hongchh/article/details/55113751/ 由于最近在vue-cli生成的web ...
- spring boot 配置时区差别
前提 数据库时区:GMT+8 show variables like '%time_zone%'; 本机电脑时区: 情景一.不指定时区 传递的参数映射到Data不指定时区,连接数据库不指定时区,保存时 ...
- bootstrap标记说明
<span class="caret"> 这就是 一个倒三角
- Tomcat访问控制及站点部署(以WAR包形式上传)!(重点)
访问控制 首先安装好jdk以及apache-tomcat并能访问tomcat网页 点击server status了解服务状态会报403的错误 第一步:修改user.xml配置文件 [root@loca ...
- Nginx平滑升级版本!(重点)
一.解释nginx的平滑升级 随着nginx越来越流行使用,并且nginx的优势也越来越明显,nginx的版本迭代也开始了加速模式,1.9.0版本的nginx更新了许多新功能,例如stream四层代理 ...