Code Lock(并查集 + 快速幂)

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

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

解题思路
有n个字母 执行m个操作 然后问如果考虑这n个字母则有26的n次方种 然而题目要求中又说了 当执行之种可执行的操作 则认为是统一序列 所以只需要用N - 可执行的序列种数 的26 次方即可
而这里需要注意的是[1,3][3,5][1,5]这是三把不同的锁 因为3执行了两次
而[1,3][4,5][1,5] 则是两把锁
所以我们在合并时只需要合并[l-1,r] 这个区间即可
然后直接用一个快速幂算法求出来即可

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>

using namespace std;

long long mod = 1000000007;
long  ff[10000007];
long num =0;
long father(int x)
{
     int i = x;
    while(x!=ff[x])
        x = ff[x];

   /* while(i!=x)//压缩路径
    {
        int j = ff[i];
        ff[i] = x;
        i = j;
    }*/
    return x;
}
void Union_set(int l,int r)
{
    long pa = father(l);
    long pb = father(r);
    if(pa!=pb)
    {
        ff[pa] = pb;
        num++;
    }
}
long long exp(long n)//快速幂算法
{
    long long sum =1;
    long long tmp =26;
    while(n)
    {
        if(n%2)//
        {
            sum = sum * tmp;
            sum %= mod;
        }
        tmp = (tmp *tmp) % mod;
        n = n /2;//向右移动一位
    }
    return sum;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        num =0;
        for(int i =0;i<=n;i++)//初始化 这列由于我判断是l-1 所以最左边是0
            ff[i] = i;
        for(int i =0;i<m;i++)
        {
            int l,r;
            cin>>l>>r;
            Union_set(l-1,r);//这里只需要判断Union_set(l-1,r) 或者 Union_set(l,r+1) 即可
        }
        cout<<exp(n-num)%mod<<endl;
    }
    return 0;
}



/*
1 1
1 1
2 1
1 2
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值