hdu 3461 Code Lock(并查集+二分求幂)

点击打开链接

1、题目大意:

给定由N个字母组成的密码锁,如【abcdef】每一位字母都可以转动,转动该字母时,将变成它的下一个字母,如‘a'转动后是b,x转动后是y,

接着给定M个区间,每次转动给定区间内的所有字母,如密码锁【abcdef】,给定区间【1,3】转动这个区间一次后变为【bcddef】;

注意【1,3】,【3,5】跟【1,5】不同,这会有三种不同的密码锁,因为3重叠,操作了2次,

【1,3】,【4,5】和【1,5’】就相同,会有2种不同的密码锁,

//处理此问题,只需稍稍变化一下并查集即可,可以是merge(l-1,r),或者是merge(l,r+1);注意set[]初始化问题

题目看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?
如果一个密码锁转动后结果一样,看做一种密码锁,经可操作区间后得到的密码锁一定是一样的,要求有多少种不同的密码锁,即求不可操作区间排列组合有多少个,

一开始将每个字母都看做一个独立区间,就有N个区间,通过并查集求出有count个可操作区间,那么就有N-count个不可操作区间,而每个不可操作区间有26种可能,所以有26的(N-count)次方个不同组合

2.题目:

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


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
 


 

3、代码:

#include<stdio.h>
#include<math.h>
int set[10000005];
int count;
#define mod 1000000007
int find(int x)
{
    int r=x;
    while(r!=set[r])
    r=set[r];
    int i=x;
    while(i!=r)
    {
        int j=set[i];
        set[i]=r;
        i=j;
    }
    return r;
}
void merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        set[fx]=fy;
        count++;
    }
}

long long exp(int n){
    long long sum=1, tmp=26;
    while(n){
        if(n&1){
            sum = sum*tmp;
            sum %= mod;
        }
        tmp = (tmp*tmp)%mod;
        n>>=1;
    }
    return sum;
}
int main()
{
    int n,m,l,r;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        count=0;
        for(int i=0;i<=n;i++)//i=0,错在i=1
        set[i]=i;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&l,&r);
            merge(l-1,r);//若果是l-1,上边初始化就从0开始,如果用merge(l,r+1),初始化i是1到n+1
        }
        printf("%lld\n",exp(n-count)%mod);
    }
    return 0;
}
/*
1 1
1 1
2 1
1 2
*/


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值