码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3461 Code Lock

时间:2015-05-12 18:58:39      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:并查集

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

标签:并查集

原文地址:http://blog.csdn.net/kirito_acmer/article/details/45672101

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!