Code Lock

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=3461

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

题意

1.问题:问你用26个小写字母,能组成长度为n的多少种不同的序列。

2.操作:他会给你m个操作区间,形如[l,r],每次你可以令一个区间里的所有字母往后一位,如a变成b,z变成a.

3.种类判定:如果两个序列进行若干次操作可以变成相同形态,则视为一种序列。

题解

老实说我看了很多题解,但是并没有完全理解,可能是我太弱了,我就大概说一下我自己的一知半解。

1.如果没有操作区间,答案记作\(\large sum=26^n\).

2.假如只有一个可操作的区间[l,r],每一种序列,都可以有26个同种不同样的序列(包括自己),则答案为\(\LARGE \frac{sum}{26}\).

3.如果m个操作区间均不重叠,那么答案为\(\LARGE \frac{sum}{26^n}\).

4.那么有重叠怎么办呢,有重叠实际上只要不是完全一样似乎就没有影响,比如[1,2]和[2,3],对于每一种序列,可以有\(\large 26^2\)个同种不同样的序列。

5.那么什么是完全一样呢,比如[1,3],[4,5]和[1,5]就是完全一样,因为[1,5]的任何变换都可以由[1,3],[4,5]一起完成。

6.最后大牛们用了神奇的并查集,即对于[l,r],就将l和r+1并为同一个集合,记录每次成功合并的次数。(不用快速幂也不会超时)

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 10000050
const ll mo=1000000007;
int n,m,bl[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int find(int x){return x==bl[x]?x:bl[x]=find(bl[x]);}
bool merge(int x,int y)
{
x=find(x); y=find(y);
if(x==y)return 0;
bl[y]=x;
return 1;
}
ll kpow(ll x,ll p)
{
ll ans=1;
while(p)
{
if (p&1)ans=ans*x%mo;
p>>=1;
x=x*x%mo;
}
return ans;
}
void work()
{
read(n); read(m);
for(int i=1;i<=n+1;i++)bl[i]=i;
ll p=n,ans=1;
for(int i=1;i<=m;i++)
{
int x,y;
read(x); read(y);
y++;
if(merge(x,y))p--;
}
ans=kpow(26,p);
printf("%lld\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}

HDU 3461 思维+并查集的更多相关文章

  1. HDU 4496 D-City(并查集,逆思维)

    题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...

  2. B - Rikka with Graph HDU - 5631 (并查集+思维)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  3. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  4. 思维+并查集 hdu5652

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意: 输入T,接下来T个样例,每个样例输入n,m代表图的大小,接下来n行,每行m个数,代表图, ...

  5. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  6. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  7. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  9. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

随机推荐

  1. docker部署nginx+vue项目

    1.vue项目打包 npm run build 会在项目生成dist文件夹,这个文件夹可以使用nginx或tomcat来发布服务 2.查找nginx基础镜像 可以通过以下网站找到符合自己的基础镜像,我 ...

  2. 利用 force index优化sql语句性能

    [转自:https://blog.csdn.net/bruce128/article/details/46777567]并进行总结 今天写了一个统计sql,在一个近亿条数据的表上执行,200s都查不出 ...

  3. 生产环境缺陷来源VS 缺陷管理响应机制

    生产环境缺陷主要来源于用户反馈.版本内遗留.内部反馈和监控后台报警,具体内容包含以下途径: 1.用户反馈: ①  前台电话方式 ②  意见反馈后台 ③  第三方平台:如微博.App Store等渠道 ...

  4. Java core dump

    目录 生成Java core dump core dump分析 生成Java core dump 可以按照下面这个文章的指引来通过jni调用触发Java core dump Generating a ...

  5. Oracle JDBC 连接池

    1.简介 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据 ...

  6. ubuntu18.04 systemctl

    systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本.systemd 的特性有:支持并行化任务:同一时候採用 socket 式与 D-Bus 总线式激活服务 ...

  7. PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

  8. 移动端BI的设计

    在移动化.大数据浪潮的今天,基于数据做决策应该是每一家公司的标配:每家公司都有专门负责数据的人,也都应该有一个BI部门.而移动BI,基于手机端随时随地进行数据查询和分析——更是BI中不可或缺的一部分. ...

  9. JAVA 基础编程练习题32 【程序 32 左移右移】

    32 [程序 32 左移右移] 题目:取一个整数 a 从右端开始的 4-7 位. 程序分析:可以这样考虑: (1)先使 a 右移 4 位. (2)设置一个低 4 位全为 1,其余全为 0 的数.可用~ ...

  10. JavaScript 模块化

    当项目越来越大时,会遇到一些问题: 1.命名冲突 2.文件依赖 所有就有了javascript模块化开发概念. 模块化开发的演变: 1.函数块:最开始用全局函数将代码块包括在函数体内,然后把很多函数写 ...