Code Lock 并查集&&二分求幂

博客围绕Code Lock问题展开,该锁通过代码系统开启,每次可移动特定连续轮子。若锁经一系列操作能相互转换则视为相同。输入包含锁长度和合法操作数,输出不同锁的数量取模结果。使用并查集和二分求幂的方法编写代码求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 /*
 2 并查集加二分求幂
 3 
 4 */
 5 #include<iostream>
 6 #include<string>
 7 #include<cstring>
 8 #include<cstdio>
 9 #include<queue>
10 using namespace std;
11 #define MOD 1000000007
12 int fa[10000005];
13 int i,j,k,n,m;
14 void init()
15 {
16   for(int it=0;it<=n;it++)
17   {
18       fa[it]=it;
19   }
20 }
21 int find(int x)
22 {
23     int r=x;
24     while(r!=fa[r])
25       r=fa[r];
26     while(x!=r)
27     {
28         int temp=fa[x];
29         fa[x]=r;
30         x=temp;
31     }
32     return r;
33 }
34 bool Unon(int a,int b)
35 {
36     int x=find(a);
37     int y=find(b);
38     if(x==y)return false;
39     fa[x]=y;
40     return true;
41 }
42 __int64 pow(__int64 a,int b)
43 {
44     __int64 sum=1;
45     while(b)
46     {
47         if(b&1)sum=(sum*a)%MOD;
48         a=(a*a)%MOD;
49         b>>=1;
50     }
51     return (sum%MOD);
52 }
53 int main()
54 {
55     while(scanf("%d%d",&n,&m)!=EOF)
56     {
57         init();
58         int a,b,cnt=0;
59         for(i=0;i<m;i++)
60         {
61             scanf("%d%d",&a,&b);
62             a--;
63             if(Unon(a,b))cnt++;
64         }
65         printf("%I64d\n",pow((__int64)26,n-cnt));
66     }
67     return 0;
68 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3407489.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值