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++

    #include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...

  2. Tomcat的整体架构

    Tomcat通过连接器和容器这两个核心组件完成整体工作,连接器负责处理socket连接和网络字节流与Request和Response对象的转化:容器负责加载和管理Servlet,以及具体处理Reque ...

  3. XSS - Pikachu

    概述: Cross-Site Scripting 简称为"CSS",为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS.一般XSS可以分为如下几种常见类型 ...

  4. WPF NET5 Prism8.0的升级指南

    前言 ​ 曾经我以学习的目的写了关于在.NET Core3.1使用Prism的系列文章.NET Core 3 WPF MVVM框架 Prism系列文章索引,也谢谢大家的支持,事实上当初的版本则是Pri ...

  5. oracle新增ID主键列,如何补全旧数据的ID值

    1.创建SEQUENCE CREATE SEQUENCE MONKEY.TEST_ADD_IDCOL_ID CACHE 100; 2.新增表栏位 ALTER TABLE MONKEY.TEST_ADD ...

  6. python中hmac模块的使用

    hmac(hex-based message authentication code)算法在计算哈希的过程中混入了key(实际上就是加盐),和hashlib模块中的普通加密算法相比,它能够防止密码被撞 ...

  7. Table controls and tabstrip controls

    本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...

  8. 深度学习DeepLearning技术实战研修班

    深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电脑测试) 一.培训方式 ...

  9. 企业项目迁移go-zero全攻略(二)

    承接上篇:上篇文章讲到 go-zero 架构设计和项目设计.本篇文章接着这个项目设计,将生成的 app 模块 中 gateway 和 RPC 进行改造.废话不多说,让我们开始! gateway ser ...

  10. NOIP2020 T2 字符串匹配题解

    首先考虑O(n^3)的暴力怎么写. 显然,可以枚举字符串\(A\)+\(B\)的右端点,左端点显然是1,暴力判断是否能与后面的字符构成循环节,对于满足 \(k*(A+B)+C=\) 整个字符串\((k ...