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

Code Lock

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 197 Accepted Submission(s): 89
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

#include<iostream>
using namespace std;
const int N=10000009;
const int Mod=1000000007;
int dyx[N],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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章