hdu3461 Code Lock
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?
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.
1 1
2 1
1 2
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的更多相关文章
- Code Lock[HDU3461]
Code LockTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- Code Lock
Code Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Su ...
- 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 ...
- HDU 3461 Code Lock(并查集)
很好的一个题,思想特别6 题意:给你小写字母个数n,每个字母可以向上翻动,例如:d->c,a->z.然后给你m对数(L,R)(L<=R),表示[L,R]之间可以同时向上翻动,且翻动后 ...
- hdu Code Lock
题意是说有N个字母组成的密码锁, 如[wersdfj], 每一位上的字母可以转动, w可转动变成x, z变成a.但是题目规定, 只能同时转动某个区间上的所有字母, 如[1,3], 那么第1到第3个 ...
- HDU 3461 Code Lock(并查集,合并区间,思路太难想了啊)
完全没思路,题目也没看懂,直接参考大牛们的解法. http://www.myexception.cn/program/723825.html 题意是说有N个字母组成的密码锁,如[wersdfj],每一 ...
- hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)
想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...
- hdu 3461 Code Lock
http://acm.hdu.edu.cn/showproblem.php?pid=3461 并差集和幂取模 这道题主要是求不可操作区间. #include <cstdio> #inclu ...
- HDU 3461 Code Lock(并查集的应用+高速幂)
* 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...
随机推荐
- 【JavaWeb】Servlet 程序
Servlet 程序 Servlet Servlet 是在 Web 服务器中运行的小型 Java 程序.Servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求. ...
- C语言变量
C语言变量 任何一种编程语言都离不开变量,特别是数据处理型程序,变量的使用非常频繁,没有变量参与程序甚至无法编制,即使编制运行后的意义也不大.变量之所以重要,是因为变量是编程语言中数据的符号标识和载体 ...
- CVE-2018-1273 Spring Data Commons 远程命令执行漏洞复现
一.漏洞描述 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架,Spring Data Commons是Spring Data下所有子项目共享的基础框架.Spring Data ...
- Table controls and tabstrip controls
本文转载自http://www.cnblogs.com/clsoho/archive/2010/01/21/1653268.html ONTROLS Syntax Forms Declaration ...
- Redis-第五章节-8种数据类型
目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...
- Java开发工具类集合
Java开发工具类集合 01.MD5加密工具类 import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...
- 小程序map学习:使用map获取当前位置并显示出来
在小程序开发的过程中,我碰到过一个做map的需求,在我开发的时候我碰到了一些问题,这里总结出来,给大家一些解决方法. 简易小程序dome下载 代码片段分享: js部分: var amapFile = ...
- ryu—流量监视
1. 代码解析 ryu/app/simple_monitor_13.py: from operator import attrgetter from ryu.app import simple_swi ...
- 页面切换提速30%!京东商城APP首屏耗时监控及优化实践
https://mp.weixin.qq.com/s/vIG_x1MWC33HKV1GxalVHg 原创 平台研发朱跃棕等 京东零售技术 2020-12-09 网络接口请求可以从接口结构合理性及多接口 ...
- Architecture and design 洋葱 中间件 装饰器
Go kit - Frequently asked questions https://gokit.io/faq/ Architecture and design Introduction - Und ...