题目:
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?
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.
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
题意:
给你n个字符长度的密码锁,每个字符可能为26中字母中的一个。给出m个区间【l,r】,代表可以把这个区间内的所有字母+1。如果两种密码(一开始有26^n种喽)能够通过上述区间变换变成一样的,那么这两种密码就是一样的密码。问存在多少种不同的密码。
解题思路:
一开始有26^n种,思考一下,发现每出现一个新的区间就会把这个区间内的方案数减少到原来的1/26,所以总方案数也减少到原来的1/26,对于【1,3】,【4,5】,【1,5】这三个区间,任选两个就能与三个区间全部都选的作用一样,所以它们其实是2个区间。那么,对于每一个区间,用并查集合并l和r+1,每次合并成功all++,就能按照上述方式判重(例如,合并1,4 all++,合并4,6 all++,再合并1,6的时候发现已经存在了,all不++),最后答案就是26^n/26^all,用快速幂写一个power(26,n-all)就行了。
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
int fa[10000005];
int all;
const int mod=1000000007;
int find(int x){
int i, j=x;
while(j != fa[j]) j = fa[j];
while(x != j){ i=fa[x]; fa[x]=j; x=i; }
return j;
}
void mer(int a,int b)
{
int aa=find(a);
int bb=find(b);
if(aa!=bb)
{
fa[aa]=bb;
all++;
}
}
int power(int a,int x)
{
ll res=1;
ll n=a;
while(x)
{
if(x&1) res=res*n%mod;
n=n*n%mod;
x>>=1;
}
return res;
}
int main()
{
int n,m,a,b;
while(cin>>n>>m)
{
all=0;
for(int i=1;i<=n+1;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
cin>>a>>b;
mer(a,b+1);
}
cout<<power(26,n-all)<<endl;
}
}