Runner's Problem_矩阵快速幂+dp

本文介绍了一种使用矩阵快速幂算法解决迷宫路径计数问题的方法。具体地,对于一个存在障碍物的3行m列的矩形迷宫,从特定起点到终点的不同路径数量,可通过矩阵快速幂高效计算得出。文章提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getchar()读的快
F. Runner's Problem
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.

You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to:

  • (i - 1, j + 1) — only if i > 1,
  • (i, j + 1), or
  • (i + 1, j + 1) — only if i < 3.

However, there are n obstacles blocking your path. k-th obstacle is denoted by three integers ak, lk and rk, and it forbids entering any cell (ak, j) such that lk ≤ j ≤ rk.

You have to calculate the number of different paths from (2, 1) to (2, m), and print it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 104, 3 ≤ m ≤ 1018) — the number of obstacles and the number of columns in the matrix, respectively.

Then n lines follow, each containing three integers ak, lk and rk (1 ≤ ak ≤ 3, 2 ≤ lk ≤ rk ≤ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk ≤ j ≤ rk. Some cells may be blocked by multiple obstacles.

Output

Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0.

Example
Input
Copy
2 5
1 3 4
2 2 3
Output
Copy
2

#include<bits/stdc++.h>
using namespace std;
#define ll long long

const int N=1e4+10;
const int mod=1e9+7;

inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}

struct P{
    ll x,y,z;
}p[N*2];

bool cmp(P a,P b){
    return a.x<b.x;
}

struct mat{
    int z[3][3];
}x,a[8];

inline mat operator*(const mat &a,const mat& b){
    mat c;
    memset(c.z,0,sizeof(c.z));
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++)
        c.z[i][k]=(c.z[i][k]+1ll*a.z[i][j]*b.z[j][k])%mod;
    return c;
}


inline mat pw(mat x,ll y){
    mat r;
    for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
    r.z[i][j]=i==j;
    for(;y;y>>=1,x=x*x)
        if(y&1)r=r*x;
    return r;
}


int u[4];
int cal(){
    return (u[1]?4:0)+(u[2]?2:0)+(u[3]?1:0);
}


int main(){
    freopen("in.txt","r",stdin);
    ll n=read(),m=read();
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);
    for(int i=1;i<=n;i++){
        int x,y=read();
        p[i]=P{read(),y,1};
        p[i+n]=P{read()+1,y,-1};
    }
    sort(p+1,p+1+n+n,cmp);
    for(int i=0;i<8;i++){
        if(!(i&4))a[i].z[0][0]=a[i].z[1][0]=1;
        if(!(i&2))a[i].z[0][1]=a[i].z[1][1]=a[i].z[2][1]=1;
        if(!(i&1))a[i].z[1][2]=a[i].z[2][2]=1;
    }
    x.z[0][1]=1;p[0].x=2;
    /*
    for(int i=1;i<=n+n;i++)
        cout<<p[i].x<<" "<<p[i].y<<" "<<p[i].z<<endl;
        */
    for(int i=1;i<=n+n;i++){
        x=x*pw(a[cal()],p[i].x-p[i-1].x),u[p[i].y]+=p[i].z;
/*
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++)
                cout<<x.z[j][k]<<" ";
            cout<<endl;
        }
        cout<<"---"<<endl;
        */
    }
    x=x*pw(a[0],m-p[n+n].x+1);
    cout<<x.z[0][1]<<endl;
}

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
<think>好的,我现在需要帮助用户解决关于如何使用mmcv库中的load_checkpoint函数的问题。首先,我得回忆一下mmcv的相关文档,特别是关于加载检查点的部分。记得在mmcv中,load_checkpoint函数通常用于加载训练好的模型权重,可能是在MMDetection或者其他OpenMMLab项目中使用。 用户提到了想从mmcv库导入这个函数,所以需要确认正确的导入路径。可能有两种情况:一种是直接从mmcv.runner导入,另一种可能是通过mmcv_custom来导入,特别是如果用户使用了自定义的mmcv版本的话。不过根据常规情况,应该是在mmcv.runner中。 接下来,我需要给出一个具体的使用示例。比如,加载一个预训练的ResNet模型。需要先导入模型类,然后创建模型实例,再使用load_checkpoint函数加载权重。要注意的是,load_checkpoint返回的是一个包含状态字典和其他元数据的字典,所以用户需要手动将权重应用到模型上,使用load_state_dict方法。 另外,需要注意版本兼容性问题。不同版本的mmcv可能有不同的函数参数或行为,所以需要提醒用户检查版本是否符合要求。比如,mmcv的版本是否与mmdetection或其他相关库的版本匹配,避免出现导入错误或参数不匹配的情况。 可能用户在使用过程中会遇到的问题包括路径错误、版本不兼容、模型结构不匹配等。这时候需要建议用户参考官方文档,或者提供查看版本的方法,如打印mmcv的版本号,或者检查CUDA和GCC的版本是否合适,如引用中提到的检查nvcc和gcc版本的方法。 此外,用户可能需要了解如何扩展或自定义加载过程,比如使用map_location参数将权重加载到指定设备,或者忽略某些关键字。这时候需要给出相应的代码示例,说明如何传递这些参数。 最后,需要确保回答的结构清晰,分步骤说明使用方法,并提供相关问题,帮助用户进一步深入学习。例如,如何处理加载时的键不匹配,或者如何加载部分权重,这些都是用户可能遇到的后续问题。</think>在MMCV框架中使用`load_checkpoint`函数加载模型权重需要遵循以下规范流程: ### 基础使用方法 1. **导入函数**: ```python from mmcv.runner import load_checkpoint ``` 2. **典型应用场景**: ```python import torch.nn as nn from mmcv.runner import load_checkpoint # 创建模型实例 model = ResNet(depth=50) # 加载预训练权重(假设checkpoint路径为'./work_dirs/latest.pth') checkpoint = load_checkpoint(model, filename='./work_dirs/latest.pth', map_location='cpu') # 查看加载的元数据 print(f'Loaded epoch: {checkpoint["meta"]["epoch"]}') ``` ### 关键参数说明 - `model`:必须传入已初始化的模型实例 - `filename`:支持本地路径/URL路径/文件对象 - `map_location`:指定权重加载设备(如`cuda:0`或`cpu`) - `strict`:默认为True,强制模型结构与checkpoint完全匹配 ### 高级用法示例 ```python # 加载部分权重并忽略尺寸不匹配的层 checkpoint = load_checkpoint( model, 'https://download.openmmlab.com/pretrain/resnet50.pth', map_location='cuda:0', strict=False, revise_keys=[(r'^module\.', '')] # 去除多GPU训练时的前缀 ) ``` ### 版本注意事项 1. 确认环境版本匹配: ```python import mmcv print(mmcv.__version__) # 要求 >=1.5.1 ``` 2. 推荐使用官方提供的预训练模型,如OpenMMLab模型库中的模型[^3][^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值