您的位置:首页 > 其它

ACM--steps--dyx--5.1.5--Code Lock

2015-05-01 09:26 387 查看

Code Lock

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 197 Accepted Submission(s): 89

[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output the answer mod 1000000007

[align=left]Sample Input[/align]

1 1
1 1
2 1
1 2


[align=left]Sample Output[/align]

1
26


[align=left]Author[/align]
hanshuai

[align=left]Source[/align]
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU

[align=left]Recommend[/align]
zhouzeyong

#include<iostream>
using namespace std;
const int N=10000009;
const int Mod=1000000007;
int dyx
,n,m;
//将每个结点初始化;
void init()
{
for(int i=0;i<=n;i++)
{
dyx[i]=i;//各个区间结点初始化;
}
}
int find(int x)
{
//带路径压缩的节点查找;
int r=x,i;
while(r!=dyx[r])
{
r=dyx[r];
}
while(x!=r)
{
//路径压缩;
i=dyx[x];
dyx[x]=r;
x=i;
}
return r;
}
bool cmb(int a,int b)
{
int RootA=find(a);
int RootB=find(b);
if(RootA==RootB)
return false;
dyx[RootA]=RootB;
return true;
}
//快速幂
long long QuickPower(int b)
{
//26^(n)%mod;
long long ans=1;
long long a=26;
a%=Mod;
while(b)
{
if(b%2==1)
ans=(ans*a)%Mod;
b/=2;
a=(a*a)%Mod;
}
return ans;
}
/*long long QuickPower(int n){
long long sum=1, tmp=26;
while(n){
if(n&1){
sum = sum*tmp;
sum %= Mod;
}
tmp = (tmp*tmp)%Mod;
n>>=1;
}
return sum;
} */
int main()
{
while(cin>>n>>m)
{
int cnt;
init();
int left,right;
cnt=0;
for(int i=0;i<m;i++)
{
cin>>left>>right;
left--;
//因为区间的缘故,[1,3],[3,5],[1,5]算作3个区间
//【1,3】,【4,5】,【1,5】;算作两个区间
//合并的时候不可以直接合并;
if(cmb(left,right))
{
cnt++;
}
}
cout<<QuickPower(n-cnt)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: