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,3],[4,5]如果可以转动,那么之后如果出现[1,5]便无效了,如果没哟可移动区间,那么所有的情况是26^n,每出现一个新的可移动区间,n--,注意[1,3],[3,5]不能包括后面的[1,5],因为3重复了。这里有个技巧,就是区间合并的时候取[l-1,r],这样如[1,3],[4,5]的就能合并了。还有用快速幂的时候要注意最后的n要用__int64型,不然会出错。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int pre[10000005];
__int64 f(int b)
{
  __int64 ans = 1,a=26,c=1000000007;
  a=a%c;
  while(b>0)
  {
   if(b%2==1)
   ans = (ans * a) % c;
   b = b/2;
   a = (a * a) % c;
  }
  return ans;
}

int find(int x)
{
int i,j=x,r=x;
while(r!=pre[r])r=pre[r];
while(j!=pre[j]){
i=pre[j];
pre[j]=r;
j=i;
}
return r;
}

int main()
{
int n,m,i,j,ans,a,b,t1,t2;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=n;
for(i=0;i<=n;i++)pre[i]=i;
for(i=1;i<=m;i++){
scanf("%d%d",&a,&b);
a--;
t1=find(a);t2=find(b);
if(t1==t2)continue;
ans--;
pre[t1]=t2;
}
printf("%I64d\n",f(ans)%1000000007);
}
return 0;
}

hdu3461 Code Lock的更多相关文章

  1. Code Lock[HDU3461]

    Code LockTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  2. Code Lock

    Code Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Su ...

  3. HDU 3461 Code Lock(并查集+二分求幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3461 A lock you use has a code system to be opened in ...

  4. HDU 3461 Code Lock(并查集)

    很好的一个题,思想特别6 题意:给你小写字母个数n,每个字母可以向上翻动,例如:d->c,a->z.然后给你m对数(L,R)(L<=R),表示[L,R]之间可以同时向上翻动,且翻动后 ...

  5. hdu Code Lock

    题意是说有N个字母组成的密码锁, 如[wersdfj],   每一位上的字母可以转动, w可转动变成x, z变成a.但是题目规定, 只能同时转动某个区间上的所有字母, 如[1,3], 那么第1到第3个 ...

  6. HDU 3461 Code Lock(并查集,合并区间,思路太难想了啊)

    完全没思路,题目也没看懂,直接参考大牛们的解法. http://www.myexception.cn/program/723825.html 题意是说有N个字母组成的密码锁,如[wersdfj],每一 ...

  7. hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)

    想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...

  8. hdu 3461 Code Lock

    http://acm.hdu.edu.cn/showproblem.php?pid=3461 并差集和幂取模 这道题主要是求不可操作区间. #include <cstdio> #inclu ...

  9. HDU 3461 Code Lock(并查集的应用+高速幂)

    * 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...

随机推荐

  1. 【C++】《C++ Primer 》第三章

    第三章 字符串.向量和数组 一.命名空间的using声明 使用某个命名空间:例如 using std::cin表示使用命名空间std中的名字cin. 头文件的代码一般不应该使用using声明,这是因为 ...

  2. MongoDB备份(mongodump)与恢复(mongorestore)工具实践

    mongodump和mongorestore实践 1.mongodump备份工具 mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档 ...

  3. python模块详解 | filecmp

    简介: filecmp是python内置的一个模块,用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单 两个主要的方法: filecmp.cmp(f1, f2[, shallow]) 比较 ...

  4. 【Linux】使用cryptsetup加密磁盘 策略为LUKS

    LUKS(Linux Unified Key Setup)为Linux硬盘分区加密提供了一种标准,它不仅能通用于不同的Linux发行版本,还支持多用户/口令.因为它的加密密钥独立于口令,所以如果口令失 ...

  5. [分享] 最流行的 10 个 JavaScript 库

    1. Lodash https://github.com/lodash/lodash 一个工具库,用得还蛮多. 2. Chalk https://github.com/chalk/chalk 给终端加 ...

  6. 【中文】【deplearning.ai】【吴恩达课后作业目录】

    [目录][吴恩达课后作业目录] 吴恩达深度学习相关资源下载地址(蓝奏云) 课程 周数 名称 类型 语言 地址 课程1 - 神经网络和深度学习 第1周 深度学习简介 测验 中英 传送门 无编程作业 编程 ...

  7. uni-app开发经验分享八: 实现微信APP支付的全过程详解

    背景 最近项目使用uni-app实现微信支付,把过程简单记录下,帮助那些刚刚基础uni-app,苦于文档的同学们.整体来说实现过程和非uni-app的实现方式没有太大不同,难点就在于uni-app对于 ...

  8. 無法直接連接互聯網,需要使用代理時(Scrapy)

    在windows系統中,如果無法直接連接互聯網,需要使用代理時該怎麽做呢? 1. 在powershell中設置proxy 背景:使用公司電腦,無法直接訪問互聯網,想要訪問互聯網就得使用代理,但是在控制 ...

  9. status 404 reading EduClient#getCourseInfoOrder(String)解决过程

    UcenterClient#getUserInfoOrder(String) failed and no fallback available.解决过程 报错内容: com.netflix.hystr ...

  10. Promise 中reject 和 catch 处理上有什么区别

    reject 是用来抛出异常,catch 是用来处理异常reject 是 Promise 的方法,而 catch 是 Promise 实例的方法reject后的东西,一定会进入then中的第二个回调, ...