BZOJ 1853 SCOI2010 幸运数字 容斥原理+DFS

25 篇文章 0 订阅
13 篇文章 1 订阅

题目大意:求[l,r]区间内有多少个数是只由6和8组成的数的倍数

同2393 链接:http://blog.csdn.net/popoqqq/article/details/41807333

此题数据强力了一些 由于r<=10^10 所以计算LCM的时候会爆long long

于是我们可以用double求出LCM的近似值与r进行比较 如果小于r再取精确值进行计算

此外就是搜索的时候要从大到小搜 从小到大会TLE

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 2050
using namespace std;
typedef long long ll;
ll l,r,a[M],b[M],ans;
bool v[M];
void DFS1(ll now)
{
	if(now>r) return ;
	a[++a[0]]=now;
	DFS1(now*10+6);
	DFS1(now*10+8);
}
void DFS2(int pos=b[0],int flag=-1,ll now=1)
{
	if(!pos)
	{
		if(now^1)
			ans+=(r/now-(l-1)/now)*flag;
		return ;
	}
	DFS2(pos-1,flag,now);
	ll temp=b[pos]/__gcd(now,b[pos]);
	if((long double)temp*now>r) return ;
	DFS2(pos-1,-flag,temp*now);
}
int main()
{
	int i,j;
	cin>>l>>r;
	DFS1(6);DFS1(8);
	sort(a+1,a+a[0]+1);
	for(i=1;a[i];i++)
		if(!v[i])
		{
			b[++b[0]]=a[i];
			for(j=i+1;a[j];j++)
				if(a[j]%a[i]==0)
					v[j]=1;
		}
	DFS2();
	cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值