【Codeforces Round #313 Div. 1】 559C Gerald and Giant Chess

本文介绍了一种解决巨棋盘游戏中从左上角到右下角路径问题的算法,通过计算组合数并避免黑格的方法得出所有可行路径的数量。

Description
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we’ll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?
The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.


【题目大意】
给出一个棋盘为h*w,现在要从(1,1)到(h,w),其中有n个黑点不能走,问有多少种可能从左上到右下(1,1和h,w永远是可以走的)


【题目分析】
有一个很显然的结论,从一个a*b的矩阵的左上角走到右下角,总共的方案数是C(a,a+b)的。

证明如下:
无论如何从左上角走到右下角,总是要经过a条横边,b条竖边的。显然,总共要走a+b步。这样就把问题转化成了有a+b个步骤,选取其中的a个横这走,其余的b个显然竖着走就可以了。

然后我们很容易想到比较有趣的做法,统计经过每一个黑点的路径数然后再用总的方案数减去。显然会有许许多多的重复的情况,这样子有可能会用到容斥定理这种丧心病狂的东西。
稍作思考,如果我们加大限制的力度,比如说对于一个黑点,我们规定他是从左上角出来之后第一个经过(不允许经过其他的黑点)的黑点,这样子,就没有重复的路径,也就不需要容斥定理了。
   
    之后就是一些细节的问题了,比如说计算组合数如何进行取模意义下的除法,就需要用到乘法逆元这一类的东西,这里就不再论述了。


【代码】(借鉴了网上的代码)

#include <cstdio>
#include <algorithm>
#include <pair>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define LL long long
const int maxn=400001;
const int maxm=4001;
const int mod=1e9+7;
LL dp[maxm],f[maxn];
int h,w,n;
pair <int,int> p[maxm];
LL powm(LL a,LL b)
{
    LL ret=1;
    while (b)
    {
        if (b&1) ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
LL C(LL a,LL b)
{
    if (b>a) return 0;
    return f[a]*powm(f[b]*f[a-b]%mod,mod-2)%mod;
}
LL Lu(LL n,LL m)
{
    if (m==0) return 1;
    return (C(n%mod,m%mod)*Lu(n/mod,m/mod))%mod;
}
int main()
{
    f[0]=1;
    for (int i=1;i<=200010;++i) f[i]=(f[i-1]*i)%mod;
    scanf("%d%d%d",&h,&w,&n);
    for (int i=1;i<=n;++i)
        scanf("%d%d",&p[i].first,&p[i].second);
    p[n+1].first=h,p[n+1].second=w;
    n++;
    sort(p+1,p+n+1);
    for (int i=1;i<=n;++i)
    {
        dp[i]=Lu(p[i].first+p[i].second-2,p[i].first-1);
        for (int j=1;j<i;++j)
        {
            if (p[j].second<=p[i].second)
            {
                int a=p[i].first-p[j].first+p[i].second-p[j].second;
                int b=p[i].first-p[j].first;
                dp[i]-=dp[j]*Lu(a,b)%mod;
                (dp[i]+=mod)%=mod;
            }
        }
    }
    printf("%I64d\n",dp[n]);
}
内容概要:本文档系统性地介绍了2024年最新提出的两种智能优化算法——青蒿素优化算法与霜冰优化算法(RIME)的原理、实现方法及其性能对比分析,并提供了完整的Matlab代码实现。文档不仅聚焦于核心算法的仿真与验证,还整合了大量前沿科研资源,涵盖微电网优化、风电功率预测、无人机三维路径规划、电动汽车调度、图像融合、负荷预测、通信信号处理、电力系统故障恢复等多个高价值应用场景。所有案例均基于Matlab/Simulink平台进行建模与仿真,强调算法在复杂工程系统中的实际应用能力,旨在为科研人员提供一套从理论到代码再到应用的完整复现体系。; 适合人群:具备一定编程基础和科研背景的研究生、高校教师及工程技术人员,尤其适合从事智能优化算法研究、新能源系统优化、自动化控制、电力系统调度、无人机导航与路径规划等相关领域的研究人员。; 使用场景及目标:①用于高水平学术论文的复现与创新性研究,提升科研效率与成果产出;②应用于复杂工程系统的建模仿真与智能优化设计,如多能互补系统调度、无人机避障路径规划、微电网能量管理等;③作为智能优化算法的教学与学习资料,深入理解现代元启发式算法的设计思想与实现机制。; 阅读建议:建议读者结合文档中提供的Matlab代码与Simulink仿真模型,按照目录结构循序渐进地学习与实践,优先选择与自身研究方向契合的案例进行代码复现,重点关注算法参数设置、收敛曲线分析与多算法对比实验部分,以全面提升算法应用与科研创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值