TLE最后三个点
查看原帖
TLE最后三个点
380950
2020wty楼主2023/11/12 20:32
#include<iostream>
#include<cstdio>
#include<algorithm>
#define re register int
#define il inline
using namespace std;
typedef long long ll;
ll ans,L,R,tot,xy[101000],tot1,xy1[10100];
bool vis[100010];
il bool cmp(ll A,ll B){return A>B;}
il void dfs(ll now){
	if(now>R)return;
	if(now*10ll+6ll<=R){
		tot++;
		xy[tot]=now*10ll+6ll;
		dfs(now*10ll+6ll);
	}
	if(now*10ll+8ll<=R){
		tot++;
		xy[tot]=now*10ll+8ll;
		dfs(now*10ll+8ll);
	} 
}
il ll GCD(ll A,ll B){
	if(!B)return A;
	return GCD(B,A%B);
}
il void dfs1(ll lcm,ll cnt,ll now){
	if(now==0){
		if(cnt==0)return;
		if(cnt&1){
			ans=ans+R/lcm;
			ans=ans-(L-1ll)/lcm;
		}
		else{
			ans=ans-R/lcm;
			ans=ans+(L-1ll)/lcm; 
		}
		return;
	}
	dfs1(lcm,cnt,now-1);
	__int128 g=GCD(lcm,xy1[now]);
//	cout<<" "<<lcm<<" "<<xy1[now]<<endl;
	if(lcm/g>R/xy1[now])return;
	ll LCM=lcm/g*xy1[now];
//	cout<<LCM<<endl;
	dfs1(LCM,cnt+1,now-1);
}
int main(){
	scanf("%lld%lld",&L,&R);
	dfs(0ll);
//	cout<<"sdfg"<<tot<<endl;
	sort(xy+1,xy+tot+1);
	for(re i=1;i<=tot;i++){
		if(!vis[i]){
			tot1++;
			xy1[tot1]=xy[i];
			for(re j=i+1;j<=tot;j++)
				if(xy[j]%xy[i]==0)
					vis[j]=1;
		}
	}
//	cout<<tot1<<endl;
	sort(xy1+1,xy1+tot1+1,cmp);
	dfs1(1ll,0,tot1);
	printf("%lld",ans);
	return 0;
}

\bx\bx

2023/11/12 20:32
加载中...