当前位置:网站首页>路径计数2(dp + 组合数)
路径计数2(dp + 组合数)
2022-06-11 03:21:00 【to cling】
Solution
- dp[i] 表示走到第i障碍物的合法方案数
- 走到点(x, y)的所有方案数: C ( x + y − 2 , x − 1 ) C(x + y - 2, x - 1) C(x+y−2,x−1)
- d p [ i ] = C ( i . x + i . y − 2 , i . x − 1 ) − ∑ d p [ j ] ∗ C ( i . x − j . x + i . y − j . y , i . x − j . x ) dp[i] = C(i.x + i.y - 2, i.x - 1) - \sum dp[j] * C(i.x - j.x + i.y - j.y, i.x - j.x) dp[i]=C(i.x+i.y−2,i.x−1)−∑dp[j]∗C(i.x−j.x+i.y−j.y,i.x−j.x)
即:合法方案数 = 所有方案数 - 所有非法的方案数
Code
const ll mod = 1000000007;
using namespace std;
const int N = 2e6 + 5;
ll f[N];
ll inv[N], fac[N];
void init()
{
fac[0] = inv[1] = inv[0] = 1;
//注意:inv[0] = 1, 即零的阶乘的逆元为1
for (int i = 1; i <= 2e6; i++)
fac[i] = fac[i - 1] * i % mod;
//先求出1~n的逆元
for (int i = 2; i <= 2e6; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
//1~n的逆元累成即为 1~n的阶乘的逆元
for (int i = 2; i <= 2e6; i++)
inv[i] = inv[i] * inv[i - 1] % mod;
}
ll C(int n, int m)
{
return fac[n] * inv[n - m] % mod * inv[m] % mod;
}
pii s[N];
int main()
{
IOS;
init();
int n, m; cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> s[i].ft >> s[i].sd;
s[m + 1] = {
n, n };
for (int i = 1; i <= m + 1; i++)
{
f[i] = C(s[i].ft + s[i].sd - 2, s[i].ft - 1);
for (int j = 1; j < i; j++)
{
ll t = C(s[i].ft - s[j].ft + s[i].sd - s[j].sd, s[i].ft - s[j].ft);
f[i] = ((f[i] - f[j] * t % mod) % mod + mod) % mod;
}
}
cout << f[m + 1] << endl;
return 0;
}
边栏推荐
- Logical deletion_ Swagger2 framework integration
- A simple understanding of C language array
- 多线程四部曲之NSThread
- 2022 年 5 月产品大事记
- Computer vision (AI) interview
- B_QuRT_User_Guide(19)
- B_QuRT_User_Guide(16)
- Integrated MP code generator
- @Controller @transactional @service annotation is invalid and less dependent
- 【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩
猜你喜欢

com. mchange. v2.c3p0. Combopooleddatasource red

In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May

Visit the swagger times unable to infer base url

PIP installation Qt5.

postgresql源码学习(二十)—— 故障恢复①-事务日志格式

【ELT.ZIP】OpenHarmony啃论文俱乐部——数据高通量无损压缩方案

B_ QuRT_ User_ Guide(17)

Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?

突破中国品牌创新技术实力,TCL做对了什么?

Instructor add function_ Enable auto fill_ Instructor modification function
随机推荐
File file = new file ("test.txt") file path
文件合成器
Mavros控制无人机在gazebo环境下进行双目SLAM
音乐正版率关键数据缺失,网易云音乐IPO胜算几何?
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
org. apache. solr. common. SolrException:Could not load core configuration for core hotel
Shell reads files by line
被“内卷”酸翻的OPPO Reno6
Solr import MySQL database report: Data config problem: invalid byte 2 of 2-byte UTF-8 sequence
{dataSource-1} closing ... {dataSource-1} closed
突破中国品牌创新技术实力,TCL做对了什么?
Pyqt5:qlineedit control code
The solution of invalid @data annotation in idea2018
MySQL learning notes: JSON nested array query
Windows10 installing keras
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
SQL | 游戏行业部分指标
ASLR
postgresql copy语句
canvas绘图——如何把图形放置画布中心