hdu3461 Code Lock (并查集+快速幂)

来源:互联网 发布:603383顶点软件 业绩 编辑:程序博客网 时间:2024/05/16 01:51

Code Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2046    Accepted Submission(s): 791


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 11 12 11 2
 

Sample Output
126
 

Author
hanshuai
 

Source
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3465 3467 3468 3464 3462 
 

Statistic | Submit | Discuss | Note

这道题是真不会。。题都没读懂 看了评论区才大概明白

直接贴过来了

这题一开始完全看不出是用并查集来解决的,在网上搜了不少大牛的思路和AC代码之后终于想通了,现在来做个总结,以后卡在这题的同学们也不用像我这样满世界找答案了~~题目的大意是一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个。再给你M个区间[L,M],表示该区间的字母可以一起同步“增加”(从'a'变为'b'为增1,'z'增1为'a')。假如一组密码按照给定的区间进行有限次的“增加”操作后可以变成另一组密码,那么我们认为这两组密码是相同的。该题的目标就是在给定N、M和M个区间的前提下计算有多少种不同的密码。根据题意,如果一个可调整的区间都没有的话,答案应该是26的N次方。每当加入一个区间的时候,答案就减少为之前的26分之1(因为该区间的加入使得原本不同的26种情况变得等价了)。因此当有x个“不同的”区间加入进来之后,答案应该为26^(N-x)。但是还有一些特殊情况需要考虑,一个是同样的区间重复加入是不会改变答案的,这点比较好理解。另一点是如果一个区间可以由其他若干个区间“拼接”而得到,那么它的加入不能改变答案。例如如果已经有了区间[1,3]和[4,5],那么再加入区间[1,5]答案也不会改变,因为[1,5]所能实现的密码变化全都可以由同步执行[1,3]与[4,5]来实现,也就是[1,3]+[4,5]等价于[1,5]。特别要注意的是[1,3]+[3,5]这种情况并不等价于[1,5]。推导到这里,题目的要求就演变成了求所给出的“不同的”区间的数量x,这一步就要靠并查集了。设父节点数组为par[N], par[L] == R+1 代表存在一个区间[L,R]。初始条件下令所有par[i] = -1,每加入一个区间[L,R]则执行Union(L,R+1)操作,若返回值为真(原本不存在[L,R],即 par[L] != R+1),则x增1,否则x不变。由此即可得到最终的x值。最后在求26^(N-x)时需要用到二分快速幂的方法,这个不是本题的重点,请大家自行百度吧。
当然 代码是自己敲的~  
#include <stdio.h>#define N 10000000+5int fa[N];int find(int x){if(fa[x]!=x) fa[x]=find(fa[x]);return fa[x];}void init(int n){for(int i=0;i<=n;i++){fa[i]=i;}}int main(){int n,m;while(~scanf("%d %d",&n,&m)){init(n);int l,r,ll,rr,sum=0;for(int i=0;i<m;i++){scanf("%d %d",&l,&r);ll=find(l-1);rr=find(r);if(ll!=rr){fa[ll]=rr;sum++;}}long long res=1;long long k=26;sum=n-sum;while(sum){if(sum%2) res=res*k%1000000007;k=k*k%1000000007;sum=sum/2;}printf("%lld\n",res);}return 0;}





1 0
原创粉丝点击