### Code Lock（並查集 + 快速冪）

Code Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 277 Accepted Submission(s): 127

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

``#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<stdio.h>using namespace std;long long mod = 1000000007;long  ff;long num =0;long father(int x){     int i = x;    while(x!=ff[x])        x = ff[x];   /* while(i!=x)//壓縮路徑 { int j = ff[i]; ff[i] = x; i = j; }*/    return x;}void Union_set(int l,int r){    long pa = father(l);    long pb = father(r);    if(pa!=pb)    {        ff[pa] = pb;        num++;    }}long long exp(long n)//快速冪算法{    long long sum =1;    long long tmp =26;    while(n)    {        if(n%2)//        {            sum = sum * tmp;            sum %= mod;        }        tmp = (tmp *tmp) % mod;        n = n /2;//向右移動一位    }    return sum;}int main(){    int n,m;    while(cin>>n>>m)    {        num =0;        for(int i =0;i<=n;i++)//初始化 這列由於我判斷是l-1 所以最左邊是0            ff[i] = i;        for(int i =0;i<m;i++)        {            int l,r;            cin>>l>>r;            Union_set(l-1,r);//這里只需要判斷Union_set(l-1,r) 或者 Union_set(l,r+1) 即可        }        cout<<exp(n-num)%mod<<endl;    }    return 0;}/*1 11 12 11 2*/``