zoukankan      html  css  js  c++  java
  • hdu3461 Code Lock

    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

    这题用到了并查集的合并知识,[1,3],[4,5]如果可以转动,那么之后如果出现[1,5]便无效了,如果没哟可移动区间,那么所有的情况是26^n,每出现一个新的可移动区间,n--,注意[1,3],[3,5]不能包括后面的[1,5],因为3重复了。这里有个技巧,就是区间合并的时候取[l-1,r],这样如[1,3],[4,5]的就能合并了。还有用快速幂的时候要注意最后的n要用__int64型,不然会出错。

    #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<map> #include<string> using namespace std; int pre[10000005]; __int64 f(int b) {   __int64 ans = 1,a=26,c=1000000007;   a=a%c;   while(b>0)   {    if(b%2==1)    ans = (ans * a) % c;    b = b/2;    a = (a * a) % c;   }   return ans; } int find(int x) { int i,j=x,r=x; while(r!=pre[r])r=pre[r]; while(j!=pre[j]){ i=pre[j]; pre[j]=r; j=i; } return r; } int main() { int n,m,i,j,ans,a,b,t1,t2; while(scanf("%d%d",&n,&m)!=EOF) { ans=n; for(i=0;i<=n;i++)pre[i]=i; for(i=1;i<=m;i++){ scanf("%d%d",&a,&b); a--; t1=find(a);t2=find(b); if(t1==t2)continue; ans--; pre[t1]=t2; } printf("%I64d ",f(ans)%1000000007); } return 0; }

  • 相关阅读:
    1021 个位数统计 (15 分
    1020 月饼 (25 分)
    1019 数字黑洞 (20 分)
    1018 锤子剪刀布 (20 分)
    1017 A除以B (20 分)
    KMP 串的模式匹配 (25 分)
    11-散列4 Hashing
    11-散列3 QQ帐户的申请与登陆 (25 分)
    11-散列1 电话聊天狂人 (25 分)
    使用正则表达式验证手机号码合法性
  • 原文地址:https://www.cnblogs.com/herumw/p/9464805.html
Copyright © 2011-2022 走看看