当前位置:网站首页>路径计数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;
}
边栏推荐
猜你喜欢

ThoughtWorks. QRcode full-featured generator

Demand and Prospect of 3D GIS Industry

has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested

canvas+svg线条粒子动画网页背景

Logical deletion_ Swagger2 framework integration

【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩
![[safety science popularization] mining technology starts from the love story of a man of science and Engineering](/img/01/73376c133c33527e479685f8680238.jpg)
[safety science popularization] mining technology starts from the love story of a man of science and Engineering

Instructor add function_ Enable auto fill_ Instructor modification function

一文搞懂单片机驱动8080LCD

Lecturer paging query_ Instructor condition query with page
随机推荐
【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩
Stringutils string tool class used by FreeMarker to create templates
ThoughtWorks.QRCode功能齐全的生成器
关于玩家身上有个普通Set并发安全的讨论
B_QuRT_User_Guide(19)
2022 年 5 月产品大事记
postgresql源码学习(21)—— 故障恢复②-事务日志初始化
單片機通信數據延遲問題排查
Multi thread alternate output ab
@Controller @transactional @service annotation is invalid and less dependent
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
Pyqt5:qlineedit control code
TimeHelper
msg=SolrCore ‘collection1‘ is not available due to init failure: Could not l
Troubleshooting of single chip microcomputer communication data delay
HQChart实战教程55-欧易网K线面积图
js顶部图标菜单点击切换背景色js特效
TweenMax五彩小球弹跳动画
postgresql copy语句
[safety science popularization] have you been accepted by social workers today?