HDC3461Code Lock(并查集)

来源:互联网 发布:mac 弹丸论破 言刃 编辑:程序博客网 时间:2024/06/06 19:24
//题目的大概意思是,给你一定的区间,然后再给你一定的区间,然后你可以进行的操作是将后来给你的操作区间的转轮转动(是全部都转动,不是只转动其中小部分,必须全部都转动,如此对于没有操作区间的条件便是26的N次方中情况,对于给出的操作区间则是通过计算不同操作区间的个数,然后求出26的n-x次方的结果,就是答案(所有不同的锁就是“不可操作区间”的所有组合情况。))
在简单一点的思维就是,相邻两个锁轮之间的差值的种类,比如说当时【1,2】操作区间时,就是26中,因为只有两个数,他们的差值种类就只有26种。
当为【1,3】时,则是有26*26中,(就是【1,2】有26种,【2,3】有26种,总共26*26)
D - 并查集

Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

SubmitStatusPractice HDU 3461

 

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 11 12 11 2
              

Sample Output

126
        
import java.util.Scanner;import java.math.BigInteger;import java.util.Arrays;import java.util.ArrayList;public class Main{    final static BigInteger sf=BigInteger.valueOf(1000000000+7);    final static int Maxn=10000000+5;    static int [] par = new int[Maxn];    public static int find(int x)    {        if(par[x]==x)        {            return x;        }        else        {           return par[x]=find(par[x]);        }    }    public static void init()    {        for(int i=0;i<Maxn;i++)        {            par[i]=i;        }    }    public static Boolean unite(int x,int y)//L->R+1,[L,R]    {        x=find(x);        y=find(y);        if(x==y)return false;        par[x]=y;        return true;    }    public static BigInteger GetRs(int n)    {        BigInteger ret=BigInteger.valueOf(1);        BigInteger x=BigInteger.valueOf(26);        while(n>0)        {            if((n%2)==1)            {                ret=ret.multiply(x).remainder(sf);            }            x=x.multiply(x).remainder(sf);            n>>=1;        }        return ret;    }    public static void main(String [] agrs)    {        Scanner cin=new Scanner(System.in);        int N,M,L,R,ans;        while(cin.hasNext())        {            init();            ans=0;            N=cin.nextInt();            M=cin.nextInt();            for(int i=0;i<M;i++)            {                L=cin.nextInt();                R=cin.nextInt();                if(unite(L,R+1))                {                    ans++;                }            }            System.out.println(GetRs(N-ans));        }    }}

 

0 0
原创粉丝点击