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

2023-12-10 04:48
文章标签 二分 code 查集 hdu lock 3461

本文主要是介绍HDU 3461 Code Lock(并查集+二分求幂),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3461


原题:


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多脑细胞 。。。


题意是说有N个字母组成的密码锁, 如【wersdfj】,   每一位上的字母可以转动, w可转动变成x, z变成a。但是题目规定, 只能同时转动某个区间上的所有字母, 如【1,3】, 那么第1到第3个的所有字母要同时转动,那么【 wersdfj 】经过一次操作就变成 【 xfssdfj 】.    一共有M 个区间是可以操作的。  

题目还规定:If a lock can change to another after a sequence of operations, we regard them as same lock. 

就是说, 经过可操作区间进行的操作得到的所有锁情况,都是同一个的。 也就是说,所有不同的锁就是“不可操作区间”的所有组合情况。


在最初时,每个字母看作是一个独立的区间, 那么就有N个区间, 可以很容易地用初始化的并查集来表示。然后一个区间可以看作是一个“元素”,  我们只需要求出共有多少个可操作区间x, 然后就可以计算得到N-x个不可操作的区间。 不可操作区间的所有组合,就是能组成的所有不同的锁。

每个区间可以有26种情况, 那么就共有26^(N-x)种情况。由于N可能会很大,所以直接计算浪费时间了,用二分求幂法来计算出结果。


这题最关键的一步是求出有多少个可操作区间。 用并查集来做时要注意,不能直接合并Union(L, R),   

假设有区间【1,3】, 【3,5】,【1,5】,  如果按照Union(L, R)来算, 那么得到两种可操作区间。但实际上是有3中可操作区间的,因为1~3, 3~5是有重叠了3的两个区间,所以1~5区间的情况不包括在前两个区间的情况内(可自己在纸上画画)。

如果是【1,3】, 【4,5】,【1,5】,  那么就是只有两种情况,因为最后一种的转动情况包含在了前两种转动的情况之内了


只需稍微处理下,Union(L-1, R)或者Union(L, R+1)都可以。


代码:

#include<cstdio>
#define N 10000005
#define MOD 1000000007
int f[N],n,m;
void init(){for(int i=0; i<=n; ++i)f[i]=i;
}
int find(int x){int i, j=x;while(j!=f[j])j=f[j];while(x!=j){i=f[x]; f[x]=j; x=i;}return j;
}
bool Union(int x,int y){int a=find(x),b=find(y);if(a==b)return false;f[a] = b;return true;
}
long long myPower(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 l,r;while(~scanf("%d%d",&n,&m)){int cnt=0;init();for(int i=0; i<m; ++i){scanf("%d%d",&l,&r);--l;if(Union(l,r)) ++cnt;}printf("%lld\n", myPower(n-cnt)%MOD);}return 0;
}




——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)





这篇关于HDU 3461 Code Lock(并查集+二分求幂)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/476054

相关文章

MySQL的隐式锁(Implicit Lock)原理实现

《MySQL的隐式锁(ImplicitLock)原理实现》MySQL的InnoDB存储引擎中隐式锁是一种自动管理的锁,用于保证事务在行级别操作时的数据一致性和安全性,本文主要介绍了MySQL的隐式锁... 目录1. 背景:什么是隐式锁?2. 隐式锁的工作原理3. 隐式锁的类型4. 隐式锁的实现与源代码分析4

MySQL中Next-Key Lock底层原理实现

《MySQL中Next-KeyLock底层原理实现》Next-KeyLock是MySQLInnoDB存储引擎中的一种锁机制,结合记录锁和间隙锁,用于高效并发控制并避免幻读,本文主要介绍了MySQL中... 目录一、Next-Key Lock 的定义与作用二、底层原理三、源代码解析四、总结Next-Key L

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while