(并查集) Code Lock --HDOJ

Code Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 280 Accepted Submission(s): 128

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

Author
hanshuai

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

Recommend
zhouzeyong

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<set>
#define mod 1000000007

using namespace std;

int fa[10000005];

int fid(int  x)
{
    if(x != fa[x])
    {
        fa[x] = fid(fa[x]);
    }
    return fa[x];
}
long long Cal(int a,int n)
{
    long long ans = 1,t=a;
    while(n)
    {
        if(n%2)
            ans = ans * t % mod;
        n /= 2;
        t = t * t % mod;
    }
    return ans;
}
int main(void)
{
 //   freopen("in.txt","r",stdin);
    int n,m,l,r;
    while(scanf("%d %d",&n,&m) != EOF)
    {
        /*这里的初始化要从零开始,因为我们将L,R转换成了 L-1,R进行
        的合并,[1,2] 与 [3,5] 进行合并,等价于[1,5]这个区间
        我们需要用到并查集,我们需要把区间调整一下,改成[0,2] 和 [2,5],
        也就相当于[0,5] 区间了,这样我们就可以利用并查集进行合并了

        */
        for(int i=0; i<=n; ++i)
            fa[i] = i;

        /*
        区间个数最多为n个,即[1,1][2,2][3,3]...[n,n];
        每个区间有26个可能,即每个区间都可以是a到z
        最后总共有 26^n 种
        */
        int cnt = n;

        /*
        假如我们将[1,1][2,2][3,3]...[n,n]这些区间中前两个合并
        就变成了[1,2][3,3]...[n,n];区间少了一个
        [1,2] 这个区间有多少种可能那?还是26种
        因为aa->bb->cc---->zz 26种
            ab->bc->cd---->za 26种
            ac->bd->ce---->zb 26种
        所以,无论什么组合的两个字符,都是26种可能

        每输入一个区间,就判断L-1 与 R是否在一个集合,如果不在,就合并区间
        ,区间数就少1
        */
        for(int i=0; i<m; ++i)
        {
            scanf("%d %d",&l,&r);
            int x = fid(l-1);
            int y = fid(r);
            if(x != y)
            {
                fa[x] = y;
                cnt--;
   //             cout <<cnt<<endl;
            }
        }
        /*
        最后计算26的区间个数次方就OK了
        */
        cout<<Cal(26,cnt)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值