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 ...
随机推荐
- c++开源库rapidxm
1.引入头文件: 一般用到的头文件: #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils ...
- 前端 9.16腾讯-2019校园招聘(正式卷)编程题题解(js)
第一题 和谐的数字 牛牛很喜欢研究数字.一天,他发明了一种数字,叫做“和谐的数字”. 和谐的数字定义如下: 定义S(n)为数字n各位数字之和,如果S(n)能够整除n,那么就称n为一个“和谐的数字”. ...
- Python爬虫学习教程:天猫商品数据爬虫
天猫商品数据爬虫使用教程 下载chrome浏览器 查看chrome浏览器的版本号,下载对应版本号的chromedriver驱动 pip安装下列包 pip install selenium pip in ...
- docker安装-单机/多机安装
操作系统ubuntu14.04 16.04 v单机安装步骤: #安装httpsca证书 apt-get install apt-transport-https ca-certificates #添 ...
- 《如何写好商业计划书》---创业学习---训练营第三课---HHR---
一,<开始上课> 1,投资人不愿意约见的原因:创始人没有把项目的投资价值和亮点呈现在商业计划书里. 2,BP的三个常见的错误:不够完整,关键内容没有呈现出来:华而不实:篇幅过长. 3,预热 ...
- elasticsearch数据组织结构
elasticsearch数据组织结构 1. mapping 1.1. 简介 mapping:意为映射关系,特别是指组织结构.在此语境中可理解为数据结构,包括表结构,表约束,数据类型等 ...
- 20 JavaScript随机&逻辑&比较&类型转换
JavaScript 随机 Math.random(): 返回0~1之间的随机数,包括0不包括1 Math.floor():下舍入.Math.floor(2.9) = 2.Math.floor(Mat ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 副本和视图
副本是一个数据的完整的拷贝,如果我们对副本进行修改,它不会影响到原始数据,物理内存不在同一位置. 视图是数据的一个别称或引用,通过该别称或引用亦便可访问.操作原有数据,但原有数据不会产生拷贝.如果我们 ...
- Jmeter_用户定义的变量
1.线程组->添加->配置原件->用户定义的变量 2.自定义变量引用: ${ }
- mcast_block_source函数
#include <errno.h> #include <sys/socket.h> #define SA struct sockaddr int mcast_block_so ...