zoukankan      html  css  js  c++  java
  • 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 /*
     2 并查集加二分求幂
     3 
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstring>
     8 #include<cstdio>
     9 #include<queue>
    10 using namespace std;
    11 #define MOD 1000000007
    12 int fa[10000005];
    13 int i,j,k,n,m;
    14 void init()
    15 {
    16   for(int it=0;it<=n;it++)
    17   {
    18       fa[it]=it;
    19   }
    20 }
    21 int find(int x)
    22 {
    23     int r=x;
    24     while(r!=fa[r])
    25       r=fa[r];
    26     while(x!=r)
    27     {
    28         int temp=fa[x];
    29         fa[x]=r;
    30         x=temp;
    31     }
    32     return r;
    33 }
    34 bool Unon(int a,int b)
    35 {
    36     int x=find(a);
    37     int y=find(b);
    38     if(x==y)return false;
    39     fa[x]=y;
    40     return true;
    41 }
    42 __int64 pow(__int64 a,int b)
    43 {
    44     __int64 sum=1;
    45     while(b)
    46     {
    47         if(b&1)sum=(sum*a)%MOD;
    48         a=(a*a)%MOD;
    49         b>>=1;
    50     }
    51     return (sum%MOD);
    52 }
    53 int main()
    54 {
    55     while(scanf("%d%d",&n,&m)!=EOF)
    56     {
    57         init();
    58         int a,b,cnt=0;
    59         for(i=0;i<m;i++)
    60         {
    61             scanf("%d%d",&a,&b);
    62             a--;
    63             if(Unon(a,b))cnt++;
    64         }
    65         printf("%I64d
    ",pow((__int64)26,n-cnt));
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    AngularJs+bootstrap搭载前台框架——准备工作
    AngularJs+bootstrap搭载前台框架——基础页面
    AngularJs调用Restful实现CRUD
    用AngularJs制作单页面应用
    Unity3D中使用Projector生成阴影
    Linux OpenGL 实践篇-16 文本绘制
    leetcode 233. 数字1的个数
    leetcode 189. 轮转数组
    leetcode 127 单词接龙
    leetcode 4.两个排序数组的中位数
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3407489.html
Copyright © 2011-2022 走看看