您的位置:首页 > 其它

hdu 3461 Code Lock【并查集+快速幂】

2016-05-02 15:11 302 查看
Code Lock
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1798    Accepted Submission(s): 671


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

 

 

Source

2010 ACM-ICPC Multi-University
Training Contest(3)——Host by WHU

 

 
题目大意: 给你一个长度为n的限制条件的串,给你m个可以一起动的区间,例如长度为3,可以一起动的区间为【1,3】,这时候我们再假设一下串的字母初始为aaa,那么他们三个可以一起变成:bbb,ccc,ddd.........................再假设可以一起动的区间为【1,2】我们再假设一下串的字母初始为aaa,那么这个串可以变成bba,cca,dda..............

问:在这样的条件下,能够得到多少初始串

分析样例:

对于第一组,我们可以通过a得到所有字母,而且假设从b得到所有字母也是可以的,但是因为和a形成的状态一样,所以记做1.

对于第二组,我们可以通过aa.ab.ac.ad.ae................得到所有串,而且ab和ba能够得到的串的状态是一样的,所以记做26.

思路:

对于可以一起动的一个区间(长度为k),我们能够得到的串是一共有:26^(k-1)种。辣么不难理解,我们要得到的最终结果就是把所有区间的情况数乘在一起。那我们是不是就能直接通过m的个数来决定最终的结果呢?显然不能,假如要是有这样的情况:【1,3】【4,6】【1,6】,显然动【1,3】和【4,6】是和【1,6】有一样的效果,所以【1,6】应该不记录进去那么这个问题如何解决呢?我们可以用并查集来搞定它,merge(0,3)再merge(3,6)那么我们不难发现,0,6已经是连接在一起的了,所以再像连接(0,6)的时候我们不接受即可。

辣么最终的思路就是这样滴:判断有多少个这样的合法区间cont,然后求26^(n-cont)即可。另外因为n比较大,所以要采用快速幂的求法。

AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
#define mod 1000000007
int f[10000005];
int cont;
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
{
f[B]=A;
cont++;
}
}
__int64 mi(__int64 a,__int64 b)
{
__int64 ans=1;
a%=mod;
while(b>0)
{
if(b%2==1)ans=(ans*a)%mod;
b/=2;
a=(a*a)%mod;
}
return ans;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
cont=0;
for(int i=0;i<=n;i++)f[i]=i;
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
merge(a-1,b);
}
printf("%I64d\n",mi(26,n-cont));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu hdu 3461 杭电 3461