2018.10.05 bzoj2393: Cirno的完美算数教室(容斥原理+搜索)

版权声明:随意转载哦......但还是请注明出处吧: https://blog.csdn.net/dreaming__ldx/article/details/82946823

传送门
经典题目。
显然满足题意的数最多不超过1024个。
然后对于两个数 a , b a,b ,如果 a a b b 的倍数,那么就不必计算 a a 的贡献。
处理出来之后容斥原理+爆搜剪枝就能过了。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll L,R,divv[1050],Div[1050],ans=0,f=1;
bool BK;
int tot=0,cnt=0;
inline void dfs(ll sum){
	if(sum>R)return;
	if(sum)divv[++tot]=sum;
	dfs(sum*10+2),dfs(sum*10+9);
}
inline ll gcd(ll a,ll b){while(b){ll t=a;a=b,b=t%a;}return a;}
inline void dfs(int pos,int dep,ll mul){
	if(mul>R)return;
	if(!dep){if(mul)ans+=f*(R/mul-(L-1)/mul),BK=0;return;}
	if(pos==cnt+1)return;
	dfs(pos+1,dep,mul),dfs(pos+1,dep-1,mul/gcd(mul,Div[pos])*Div[pos]);
}
int main(){
	cin>>L>>R,dfs(0),sort(divv+1,divv+tot+1);
	for(int i=1;i<=tot;++i){
		bool f=true;
		for(int j=i-1;j;--j)if(divv[i]%divv[j]==0){f=false;break;}
		if(f)Div[++cnt]=divv[i];
	}
	for(int i=1;i<=cnt;++i,f*=-1){
		BK=1,dfs(1,i,1);
		if(BK)break;
	}
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dreaming__ldx/article/details/82946823
今日推荐