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; }
相关文章推荐
- ACM--steps--dyx--1.3.5--开门人和关门人
- ACM--steps--dyx--1.3.6--第二小整数
- ACM--dyx--steps--5.1.3--Is It A Tree?
- ACM--steps--dyx--1.2.6--Quicksum
- ACM--steps--dyx--悼念512汶川大地震遇难同胞——老人是真饿了
- ACM--steps--dyx--1.2.1
- ACM--steps--dyx--1.2.7--Lowest Bit
- ACM--steps--dyx--1.3.8--Fighting for HDU
- ACM--steps--dyx--Hat's Fibonacci
- ACM--steps----dyx--1.2.8--AC Me
- ACM--steps--dyx--2.1.1--最小公倍数
- ACM--steps--dyx--1.2.2--
- ACM--dyx--steps--3.1.8--Queuing
- ACM--steps--dyx--2.3.7--下沙的沙子有几粒?
- ACM--steps-dyx--1.2.3--find your present (2)(异或)
- ACM--steps--dyx--2.3.2--Exponentiation
- ACM--steps--dyx--5.1.4--find the most comfortable road
- ACM--steps--dyx--1.3.2--今年暑假不AC
- ACM--steps--dyx--1.2.4--Buildings
- ACM--steps--dyx--1.2.5--The Seven Percent Solution