如果没操作的话是26^n,每加一个区间就除26,那么只需要统计区间个数就可以了
然而有一种特殊情况就是一个大区间里包含了若干个小区间且区间是不交叉且能填满大区间的。。例如:[1,5]和[1,2][3,5]这样的,小区间完全代替大区间可以实现功能。。所以这种情况得排除。。用并查集就可以了。。
当然需要快速幂
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(i,l,r) for(edge *j=h[x];j;j;=j->next)
#define inf 1000000007
#define ll long long
#define mem(a) memset(a,sizeof(a),0)
#define succ(x) (1<<x)
#define lowbit(x) (x&&(-x))
#define NM 10000005
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,f[NM];
ll pow(){
ll s=1;
for(ll t=26;n;n>>=1){
if(n%2)(s*=t)%=inf;
(t*=t)%=inf;
}
return s;
}
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
int main(){
freopen("data.in","r",stdin);
while(~scanf("%d%d",&n,&m)){
mem(f);
inc(i,0,n)f[i]=i;
inc(i,1,m){
int x=find(read()-1),y=find(read());
if(x!=y){
f[x]=y;n--;
}
}
printf("%lld\n",pow());
}
return 0;
}
Code Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2223 Accepted Submission(s): 860
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
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 3465 3467 3468 3464 3462