Code Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 197 Accepted Submission(s): 89At 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?
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.
1 1 1 1 2 1 1 2
1 26
#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;
}