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

本文解析了一道关于锁的ACM题目,该锁由一系列字母轮构成,通过特定操作改变轮上字母来解锁。文章讨论了如何通过合法的操作序列使锁的状态发生变化,并计算不同锁状态的数量。

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

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? 

InputThere 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. 
OutputFor each test case, output the answer mod 1000000007Sample Input

1 1
1 1
2 1
1 2

Sample Output

1
26

题解:先放一放
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <iomanip>
 8 #include <cmath>
 9 #include <ctime>
10 #include <map>
11 #include <set>
12 #include <queue>
13 using namespace std;
14 #define lowbit(x) (x&(-x))
15 #define max(x,y) (x>y?x:y)
16 #define min(x,y) (x<y?x:y)
17 #define MAX 100000000000000000
18 #define MOD 1000000007
19 #define pi acos(-1.0)
20 #define ei exp(1)
21 #define PI 3.141592653589793238462
22 #define INF 0x3f3f3f3f3f
23 #define mem(a) (memset(a,0,sizeof(a)))
24 typedef long long ll;
25 ll gcd(ll a,ll b){
26     return b?gcd(b,a%b):a;
27 }
28 bool cmp(int x,int y)
29 {
30     return x>y;
31 }
32 const int N=10000005;
33 const int mod=1e9+7;
34 int f[N];
35 int find1(int x) {return x==f[x]?x:f[x]=find1(f[x]);};
36 int Union(int x,int y)
37 {
38     x=find1(x),y=find1(y);
39     if(x==y) return 0;
40     return f[x]=y,1;
41 }
42 ll multi(int x)
43 {
44     ll ans=1,tmp=26;
45     while(x){
46         if(x&1) ans=(ans*tmp)%mod;
47         tmp=(tmp*tmp)%mod;
48         x>>=1;
49     }
50     return ans;
51 }
52 int main()
53 {
54     int n,m;
55     while(~scanf("%d%d",&n,&m)){
56         for(int i=1;i<=n+2;i++) f[i]=i;
57         int a,b,ans=0;
58         for(int i=0;i<m;i++){
59             scanf("%d%d",&a,&b);
60             ans+=Union(a,b+1);
61         }
62         printf("%lld\n",power(n-ans));
63     }
64     return 0;
65 }

转载于:https://www.cnblogs.com/shixinzei/p/7295645.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值