hdu 5.1.5 code lock

這題,首先感謝google有個奇葩的手氣不錯,我直接用搜索都搜不到這道題,居然真的手氣不錯了一下!rp啊~

Code Lock

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

這題的並查集很無聊,亮點在想到用並查集和二分求26^x.至今對這兩點理解障礙。

碼的時候還挺順的,自己編譯也沒問題,提交就各種wa。數據給的好弱啊……最後發現是一個地方的long long寫成了int……哎喲哎喲。

#include <iostream>
using namespace std;
#define MAX 10000010

int root[MAX];
#define MOD 1000000007


int find(int x)
{
    if(root[x]!=x)
    {
        root[x]=find(root[x]);
    }
    return root[x];
}
    
    
long long binary(int x)
{
     if(x==0)return 1;
     long long a=binary(x/2);//a need to be long long
     a=a*a%MOD;
     if(x%2==1)a=a*26%MOD;
     return a;
}
     

bool merge(int l,int r)
{
     int fl=find(l);
     int fr=find(r);
     if(fl==fr)
         return false;
     else
     {
         root[fr]=fl;
         return true;
     }
}

int main()
{
     int n,m;
     long long ans;
     while(cin>>n>>m)
     {
         int i;
         for(i=0;i<=n;i++)
         {
             root[i]=i;
         }
         int x=0;
         while(m--)
         {
             int l,r;
             cin>>l>>r;
             l--; 
             if(merge(l,r))
                 x++;
            
         }
         ans=binary(n-x);
         cout<<ans<<endl;
     }
     system("pause");
     return 0;
}

某人說的對,有的題果然是智商問題……默默流淚……

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