首页 > 代码库 > HDU 3461 Code Lock(并查集+二分求幂)

HDU 3461 Code Lock(并查集+二分求幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3461
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? 

InputThere 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. 
OutputFor each test case, output the answer mod 1000000007Sample Input

1 11 12 11 2

Sample Output

126

题解:先放一放
 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime>10 #include <map>11 #include <set>12 #include <queue>13 using namespace std;14 #define lowbit(x) (x&(-x))15 #define max(x,y) (x>y?x:y)16 #define min(x,y) (x<y?x:y)17 #define MAX 10000000000000000018 #define MOD 100000000719 #define pi acos(-1.0)20 #define ei exp(1)21 #define PI 3.14159265358979323846222 #define INF 0x3f3f3f3f3f23 #define mem(a) (memset(a,0,sizeof(a)))24 typedef long long ll;25 ll gcd(ll a,ll b){26     return b?gcd(b,a%b):a;27 }28 bool cmp(int x,int y)29 {30     return x>y;31 }32 const int N=10000005;33 const int mod=1e9+7;34 int f[N];35 int find1(int x) {return x==f[x]?x:f[x]=find1(f[x]);};36 int Union(int x,int y)37 {38     x=find1(x),y=find1(y);39     if(x==y) return 0;40     return f[x]=y,1;41 }42 ll multi(int x)43 {44     ll ans=1,tmp=26;45     while(x){46         if(x&1) ans=(ans*tmp)%mod;47         tmp=(tmp*tmp)%mod;48         x>>=1;49     }50     return ans;51 }52 int main()53 {54     int n,m;55     while(~scanf("%d%d",&n,&m)){56         for(int i=1;i<=n+2;i++) f[i]=i;57         int a,b,ans=0;58         for(int i=0;i<m;i++){59             scanf("%d%d",&a,&b);60             ans+=Union(a,b+1);61         }62         printf("%lld\n",power(n-ans));63     }64     return 0;65 }

HDU 3461 Code Lock(并查集+二分求幂)