# [SCOI2016]萌萌哒（倍增+并查集）

``````#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=101000;
int fa[N*22],id[N][22],pw[22],n,m,ans,book[N],tot,xb[N*22],l[N*22];
int find(int x){
if(fa[x]==x)return x;
else return fa[x]=find(fa[x]);
}
int ksm(int x,int b){
int tmp=1;
while(b){
if(b&1)tmp=tmp*x%mod;
b>>=1;
x=x*x%mod;
}
return tmp;
}
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
void pre_work(int x){
pw[0]=1;
for(int i=1;i<=20;i++)pw[i]=pw[i-1]*2;
int len=log2(x);
for(int j=0;j<=len;j++)
for(int i=1;i+pw[j]-1<=n;i++)
id[i][j]=++tot,xb[tot]=i,l[tot]=j,fa[tot]=tot;
}
signed main(){
pre_work(n);
while(m--){
for(int i=20;i>=0;i--)
if(a+pw[i]-1<=b){
int x=find(id[a][i]),y=find(id[c][i]);
if(x!=y)fa[x]=y;
a=a+pw[i];c=c+pw[i];
}
}
int len=log2(n);
for(int j=len;j>=1;j--)
for(int i=1;i+pw[j]-1<=n;i++){
int f=find(id[i][j]);
int x=find(id[i][j-1]);
int y=find(id[xb[f]][l[f]-1]);
if(x!=y)fa[x]=y;
x=find(id[i+pw[j-1]][j-1]);
y=find(id[xb[f]+pw[l[f]-1]][l[f]-1]);
if(x!=y)fa[x]=y;
}
for(int i=1;i<=n;i++){
int x=find(id[i][0]);
if(book[x]==0)ans++,book[x]=1;
}
printf("%lld",9ll*ksm(10,ans-1)%mod);
return 0;
}``````

[SCOI2016]萌萌哒（倍增+并查集）

(0)
(0)

© 2014 mamicode.com 版权所有 京ICP备13008772号-2