HDU 3461 思维+并查集

时间:2024-01-16 12:56:44

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

题意

1.问题:问你用26个小写字母,能组成长度为n的多少种不同的序列。

2.操作:他会给你m个操作区间,形如[l,r],每次你可以令一个区间里的所有字母往后一位,如a变成b,z变成a.

3.种类判定:如果两个序列进行若干次操作可以变成相同形态,则视为一种序列。

题解

老实说我看了很多题解,但是并没有完全理解,可能是我太弱了,我就大概说一下我自己的一知半解。

1.如果没有操作区间,答案记作\(\large sum=26^n\).

2.假如只有一个可操作的区间[l,r],每一种序列,都可以有26个同种不同样的序列(包括自己),则答案为\(\LARGE \frac{sum}{26}\).

3.如果m个操作区间均不重叠,那么答案为\(\LARGE \frac{sum}{26^n}\).

4.那么有重叠怎么办呢,有重叠实际上只要不是完全一样似乎就没有影响,比如[1,2]和[2,3],对于每一种序列,可以有\(\large 26^2\)个同种不同样的序列。

5.那么什么是完全一样呢,比如[1,3],[4,5]和[1,5]就是完全一样,因为[1,5]的任何变换都可以由[1,3],[4,5]一起完成。

6.最后大牛们用了神奇的并查集,即对于[l,r],就将l和r+1并为同一个集合,记录每次成功合并的次数。(不用快速幂也不会超时)

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 10000050
const ll mo=1000000007;
int n,m,bl[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int find(int x){return x==bl[x]?x:bl[x]=find(bl[x]);}
bool merge(int x,int y)
{
x=find(x); y=find(y);
if(x==y)return 0;
bl[y]=x;
return 1;
}
ll kpow(ll x,ll p)
{
ll ans=1;
while(p)
{
if (p&1)ans=ans*x%mo;
p>>=1;
x=x*x%mo;
}
return ans;
}
void work()
{
read(n); read(m);
for(int i=1;i<=n+1;i++)bl[i]=i;
ll p=n,ans=1;
for(int i=1;i<=m;i++)
{
int x,y;
read(x); read(y);
y++;
if(merge(x,y))p--;
}
ans=kpow(26,p);
printf("%lld\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}