Code Lock(并查集 + 快速幂)

本文介绍了一个名为CodeLock的问题,该问题涉及一系列轮盘组成的锁,每个轮盘上有26个英文字母。通过特定的连续轮盘操作改变锁的状态,讨论了如何计算不同锁状态的数量,并提供了一种算法解决方案。

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

Code Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 277 Accepted Submission(s): 127

Problem Description
A lock you use has a code system to be opened instead of a key. The lock contains a sequence of wheels. Each wheel has the 26 letters of the English alphabet ‘a’ through ‘z’, in order. If you move a wheel up, the letter it shows changes to the next letter in the English alphabet (if it was showing the last letter ‘z’, then it changes to ‘a’).
At each operation, you are only allowed to move some specific subsequence of contiguous wheels up. This has the same effect of moving each of the wheels up within the subsequence.
If a lock can change to another after a sequence of operations, we regard them as same lock. Find out how many different locks exist?

Input
There are several test cases in the input.

Each test case begin with two integers N (1<=N<=10000000) and M (0<=M<=1000) indicating the length of the code system and the number of legal operations.
Then M lines follows. Each line contains two integer L and R (1<=L<=R<=N), means an interval [L, R], each time you can choose one interval, move all of the wheels in this interval up.

The input terminates by end of file marker.

Output
For each test case, output the answer mod 1000000007

Sample Input
1 1
1 1
2 1
1 2

Sample Output
1
26

解题思路
有n个字母 执行m个操作 然后问如果考虑这n个字母则有26的n次方种 然而题目要求中又说了 当执行之种可执行的操作 则认为是统一序列 所以只需要用N - 可执行的序列种数 的26 次方即可
而这里需要注意的是[1,3][3,5][1,5]这是三把不同的锁 因为3执行了两次
而[1,3][4,5][1,5] 则是两把锁
所以我们在合并时只需要合并[l-1,r] 这个区间即可
然后直接用一个快速幂算法求出来即可

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>

using namespace std;

long long mod = 1000000007;
long  ff[10000007];
long num =0;
long father(int x)
{
     int i = x;
    while(x!=ff[x])
        x = ff[x];

   /* while(i!=x)//压缩路径
    {
        int j = ff[i];
        ff[i] = x;
        i = j;
    }*/
    return x;
}
void Union_set(int l,int r)
{
    long pa = father(l);
    long pb = father(r);
    if(pa!=pb)
    {
        ff[pa] = pb;
        num++;
    }
}
long long exp(long n)//快速幂算法
{
    long long sum =1;
    long long tmp =26;
    while(n)
    {
        if(n%2)//
        {
            sum = sum * tmp;
            sum %= mod;
        }
        tmp = (tmp *tmp) % mod;
        n = n /2;//向右移动一位
    }
    return sum;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        num =0;
        for(int i =0;i<=n;i++)//初始化 这列由于我判断是l-1 所以最左边是0
            ff[i] = i;
        for(int i =0;i<m;i++)
        {
            int l,r;
            cin>>l>>r;
            Union_set(l-1,r);//这里只需要判断Union_set(l-1,r) 或者 Union_set(l,r+1) 即可
        }
        cout<<exp(n-num)%mod<<endl;
    }
    return 0;
}



/*
1 1
1 1
2 1
1 2
*/
<think>好的,我现在需要帮助用户解决在SpringBoot中使用MyBatis-Plus实现两个表ECI_DATA_GROUP和ECI_DATA_CODE的关联查询并支持分页的问题。首先,我得回顾一下用户提供的引用内容,看看有哪些已有的信息可以利用。 用户提到的引用[1]、[2]、[3]、[4]都是关于MyBatis-Plus(MP)和分页查询的整合,特别是引用[3]中提到了多表联查和分页的实现方法。用户的需求是两表关联查询加分页,所以需要结合这些引用中的步骤来实现。 首先,用户需要配置分页插件。根据引用[2]和[4],分页插件的配置是通过MybatisPlusInterceptor添加PaginationInnerInterceptor。这一点是必须的,否则分页功能无法生效。所以第一步应该是确认用户是否已经正确配置了分页插件,如果没配置的话,需要指导用户添加配置类。接下来是处理多表联查。MyBatis-Plus本身并不直接支持多表联查,但可以通过自定义SQL实现。引用[1]提到使用MyBatis-Plus-Join(MPJ),但用户的问题中没有明确提到是否使用这个工具。根据引用[3]中的示例,用户可能更倾向于使用原生的MyBatis-Plus方式,即通过自定义SQL语句,结合QueryWrapper来拼接条件。因此,可能需要指导用户编写自定义的Mapper接口,使用@Select注解定义SQL,并且利用${ew.customSqlSegment}来动态添加查询条件。 然后,分页查询的实现需要结合PageHelper或者MyBatis-Plus的分页方法。引用[3]中的示例使用了startPage()和getDataTable()方法,这可能是在Controller中调用的。需要确认用户是否使用PageHelper或者MyBatis-Plus自带的Page对象。根据引用[2],MyBatis-Plus的分页插件配置后,可以使用Page对象进行分页查询,所以在Service层需要创建Page对象,并在Mapper接口的方法中使用这个Page参数,这样SQL会自动加入分页语句。另外,关联查询的SQL语句需要正确编写,确保ECI_DATA_GROUP和ECI_DATA_CODE之间的连接条件正确,比如ON子句的关联字段是否正确。例如,如果ECI_DATA_GROUP的主键是id,而ECI_DATA_CODE中有group_id作为外键,那么JOIN条件应该是ON eci_data_group.id= eci_data_code.group_id。同时,需要处理返回的结果集。由于是两表关联查询,返回的数据可能需要封装到一个DTO(Data TransferObject)中,包含两个表的字段。因此,用户可能需要创建一个DTO类,如EciDataGroupCodeDTO,来接收查询结果。在Mapper的XML文件或者@Select注解中,需要确保查询的列与DTO的属性对应,或者使用别名来匹配。最后,测试分页功能是否正常工作,确保传入的页码和每页数量参数正确,并且返回的分页数据包含总记录数和分页结果。可能需要提醒用户注意SQL的优化,避免分页查询时的性能问题,尤其是在大数据量的情况下。总结一下步骤:配置分页插件->创建DTO类->编写自定义SQL的Mapper方法->在Service层调用分页方法-> Controller中处理分页请求。需要确保每个步骤都正确无误,特别是SQL语句和分页插件的配置。此外,用户可能需要处理查询条件,动态添加where子句,这时候QueryWrapper的使用就很重要,可以通过ew.customSqlSegment来动态拼接条件。可能遇到的问题包括:分页插件未正确配置导致分页失效,SQL语句错误导致关联查询失败,结果集映射不正确导致字段无法正确填充到DTO,或者分页参数传递错误。需要逐一检查这些点,确保每一步都正确。</think>### SpringBoot+MyBatis-Plus实现两表关联分页查询 #### 一、配置分页插件 在SpringBoot配置类中添加MyBatis-Plus分页插件: ```java @Configuration public class MyBatisConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } ``` 此配置使MyBatis-Plus支持物理分页[^2][^4]。 --- #### 二、创建DTO接收联查结果 定义数据传输对象`EciDataGroupCodeDTO`: ```java @Data public class EciDataGroupCodeDTO { // ECI_DATA_GROUP字段 private Long groupId; private String groupName; // ECI_DATA_CODE字段 private String codeValue; private String codeDesc; } ``` --- #### 三、Mapper层实现关联查询 1. **自定义SQL语句**: ```java @Mapper public interface EciDataMapper extends BaseMapper<EciDataGroup> { @Select("SELECT g.id as groupId, g.name as groupName, c.code_value, c.code_desc " + "FROM ECI_DATA_GROUP g " + "LEFT JOIN ECI_DATA_CODE c ON g.id = c.group_id " + "${ew.customSqlSegment}") Page<EciDataGroupCodeDTO> selectGroupWithCode(Page<?> page, @Param(Constants.WRAPPER) QueryWrapper wrapper); } ``` 说明: - 使用`LEFT JOIN`实现两表关联 - `${ew.customSqlSegment}`动态拼接查询条件[^3] --- #### 四、Service层实现分页逻辑 ```java @Service public class EciDataServiceImpl implements EciDataService { @Autowired private EciDataMapper eciDataMapper; @Override public Page<EciDataGroupCodeDTO> getGroupWithCodePage(int pageNum, int pageSize, QueryWrapper wrapper) { Page<EciDataGroupCodeDTO> page = new Page<>(pageNum, pageSize); return eciDataMapper.selectGroupWithCode(page, wrapper); } } ``` --- #### 五、Controller层接口 ```java @RestController @RequestMapping("/eci-data") public class EciDataController { @Autowired private EciDataService eciDataService; @GetMapping("/page") public Result getPage( @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(required = false) String groupName) { QueryWrapper<EciDataGroupCodeDTO> wrapper = new QueryWrapper<>(); if (StringUtils.isNotBlank(groupName)) { wrapper.like("g.name", groupName); } return Result.success(eciDataService.getGroupWithCodePage(pageNum, pageSize, wrapper)); } } ``` --- #### 六、查询示例 访问接口: ``` GET /eci-data/page?pageNum=1&pageSize=5&groupName=test ``` 响应结构: ```json { "code": 200, "data": { "records": [ { "groupId": 1, "groupName": "test_group", "codeValue": "A001", "codeDesc": "测试编码" } ], "total": 15, "size": 5, "current": 1 } } ``` --- ### 注意事项 1. **字段别名匹配**:SQL中的列别名必须与DTO字段名完全一致 2. **索引优化**:关联字段建议建立索引,如`ECI_DATA_CODE.group_id` 3. **分页参数验证**:前端需限制`pageSize`最大值 4. **N+1问题**:大数据量时建议使用JOIN代替多次单表查询[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值