Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1110 Solved: 528
Description
一个长度为n的大数,用S1S2S3…Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条
件表示为四个数,l1,r1,l2,r2,即两个长度相同的区间,表示子串Sl1Sl1+1Sl1+2…Sr1与Sl2Sl2+1Sl2+2…S
r2完全相同。比如n=6时,某限制条件l1=1,r1=3,l2=4,r2=6,那么123123,351351均满足条件,但是12012,13
1141不满足条件,前者数的长度不为6,后者第二位与第五位不同。问满足以上所有条件的数有多少个。
Input
第一行两个数n和m,分别表示大数的长度,以及限制条件的个数。接下来m行,对于第i行,有4个数li1,ri1,li2
,ri2,分别表示该限制条件对应的两个区间。
1≤n≤10^5,1≤m≤10^5,1≤li1,ri1,li2,ri2≤n;并且保证ri1-li1=ri2-li2。
Output
一个数,表示满足所有条件且长度为n的大数的个数,答案可能很大,因此输出答案模10^9+7的结果即可。
Sample Input
4 2
1 2 3 4
3 3 3 3
Sample Output
90
HINT
Source
直接倍增思维并查集就可以了,对于每个大小的块维护并查集即可,然后最后来pushdown,然后再计数就可以了,具体的可以看这篇博客 机房模拟赛
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
long long p;
const long long mod = 1000000007LL;
const int MAXN = 100000;
int fa[20][ MAXN * 2 ], pow[ MAXN * 2 ], n, m, L, R, l, r, len;
long long fast_pow( long long x ) {
long long BASE = 10LL, tmp = 1LL;
while( x ) {
if( x & 1 ) tmp = tmp * BASE % mod;
x >>= 1;
BASE = BASE * BASE % mod;
}
return tmp;
}
int find( int M, int x ) {
if( fa[M][x] == x ) return x;
else return fa[M][x] = find( M, fa[M][x] );
}
void Union( int M, int x, int y ) {
x = find( M, x ), y = find( M, y );
if( x != y ) fa[M][x] = fa[M][y];
}
int main( ) {
scanf( "%d", &n );
for( register int i = 2; i <= n; i++ ) pow[i] = pow[ i >> 1 ] + 1;
for( register int i = pow[n]; i >= 0; i-- ) for( register int j = 1; j <= n; j++ ) fa[i][j] = j;
scanf( "%d", &m );
while( m-- ) {
scanf( "%d%d%d%d", &L, &R, &l, &r );
int M = pow[ R - L + 1 ];
Union( M, L, l );
Union( M, R - ( 1 << M ) + 1, r - ( 1 << M ) + 1 );
}
for( register int j = pow[n]; j; j-- ) {
for( register int i = 1; i + ( 1 << j ) - 1 <= n; i++ ) {
int fat = find( j, i );
Union( j - 1, i, fat );
Union( j - 1, i + ( 1 << ( j - 1 ) ), fat + ( 1 << ( j - 1 ) ) );
}
} int tmp = 0;
for( register int i = 1; i <= n; i++ )
if( find( 0, i ) == i ) tmp++;
printf( "%lld\n", 9LL * fast_pow( tmp - 1 ) % mod );
return 0;
}