Code Lock(并查集 + 快速幂)
来源:互联网 发布:网络语苏是什么意思 编辑:程序博客网 时间:2024/05/17 01:13
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 11 12 11 2*/
- Code Lock(并查集 + 快速幂)
- hdu 3461 Code Lock【并查集+快速幂】
- hdu 3461 Code Lock(并查集+快速幂)
- hdu 3461 Code Lock(并查集+快速幂)
- hdu3461 Code Lock(并查集+快速幂)
- hdu3461 Code Lock (并查集+快速幂)
- HDU 3461 Code Lock(并查集的应用+快速幂)
- hdu3461Code Lock 快速幂+并查集
- hdu3461Code Lock【并查集+快速幂】
- HDU 3461 Code Lock(并查集+二分求幂)
- hdu 3461 Code Lock(并查集+二分求幂)
- HDU-3461 Code Lock 并查集 + 二分求幂
- HDU3461 Code Lock 并查集应用
- HDU 3461 Code Lock (并查集)
- 【并查集】 HDOJ 3461 Code Lock
- (并查集) Code Lock --HDOJ
- HDU - 3461 Code Lock (并查集和幂运算)
- hdu-3461-Code Lock-并查集+高次幂取模
- python matplotlib 画dataframe的时间序列图
- 第一章 linux redhat(centos7) 目录结构
- mybatis入门 ---1
- sizeof的使用总结
- PAT乙级1001. 害死人不偿命的(3n+1)猜想 (15)
- Code Lock(并查集 + 快速幂)
- 进制表示 & 文件权限解读
- IDE控制台打印代码链接
- UVA
- 微信公众号开发注意事项
- ADTF(Assist Automotive Data and Time-Triggered Framework)介绍
- JAVA之初识springMVC框架
- c++中不知道的事儿
- STM32L151 PWM输出示例