当前位置:网站首页>路径计数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;
}
边栏推荐
- In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
- Pyqt5:slider slider control
- If the source code of the home page module is not separated ----- > nanny level source code analysis (1)
- ROS Basics - use the launch file (I) - start multiple ROS nodes in batch
- B_QuRT_User_Guide(19)
- C语言指针
- postgresql源码学习(十七)—— MVCC②-快照与隔离级别简介
- ASLR
- Hough transform of image
- Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?
猜你喜欢

PostgreSQL source code learning (17) -- mvcc ② - Introduction to snapshot and isolation level

一文搞懂单片机驱动8080LCD

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

Solr initialization failure: requesthandler INIT failure

ThoughtWorks.QRCode功能齐全的生成器

If the source code of the home page module is not separated ----- > nanny level source code analysis (1)

Logical deletion_ Swagger2 framework integration

WinDbg-虚拟机-双机调试-驱动文件的调试

org. apache. solr. common. SolrException:Could not load core configuration for core hotel

Cygwin reports an error child_ info_ fork::abort: XXX. dll: Loaded to different address: parent(XXX) != child(XXX)
随机推荐
ThoughtWorks.QRCode功能齐全的生成器
Deep parsing of question mark expressions
Unity之数据持久化——Json
三维GIS行业需求及展望
TweenMax五彩小球弹跳动画
PostgreSQL source code learning (22) - fault recovery ③ - transaction log registration
正则表达式
Stringutils string tool class used by FreeMarker to create templates
【ELT.ZIP】OpenHarmony啃论文俱乐部——数据高通量无损压缩方案
cv. Houghcircles: Circular Hough transform opencv
CheckBox美化按钮选中样式
Difference between idea open and import project
A simple understanding of C language array
Pyqt5:qlineedit control code
文件合成器
Operations on annotation and reflection
Windows10 installing keras
Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
How should Xiaobai start the Amazon self support evaluation?
The solution of invalid @data annotation in idea2018