Description
在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种“近似幸运号码”。lxhgww规定,凡是“幸运号码”的倍数都是“近似幸运号码”,当然,任何的“幸运号码”也都是“近似幸运号码”,比如12,16,666都是“近似幸运号码”。 现在lxhgww想知道在一段闭区间[a, b]内,“近似幸运号码”的个数。
Input
输入数据是一行,包括2个数字a和b
Output
输出数据是一行,包括1个数字,表示在闭区间[a, b]内“近似幸运号码”的个数
Sample Input
【样例输入1】
1 10
【样例输入2】
1234 4321
Sample Output
【样例输出1】
2
【样例输出2】
809
HINT
【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000
很明显的容斥裸题,预处理出所有‘68’数字后,利用容斥求解即可
只是这道题有两个地方需要注意
一、幸运数字中可能会存在a是b的倍数,所以这种情况要把a先剔除
二、数据范围为10^10,注意求lcm乘的时候不要爆long long,可先开个double检验一下,
哦对还有,dfs的时候先搜大的数,这样能够比较早的剔除掉大于R的情况
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO"%lld"
#endif
#define LL long long
using namespace std;
const int maxn=10000+5;
int cnt,tot;
LL L,R,num[maxn],ans;
void init(LL x)
{
if (x>R) return ;
if (x) num[++cnt]=x;
init(x*10+6);
init(x*10+8);
}
bool cmp(LL x,LL y)
{
return x>y;
}
LL gcd(LL a,LL b)
{
return b?gcd(b,a%b):a;
}
void dfs(int ID,LL sum,LL lcm)
{
if (ID>tot)
{
if (sum&1) ans+=R/lcm-(L-1)/lcm;
else if (sum) ans-=R/lcm-(L-1)/lcm;
return ;
}
dfs(ID+1,sum,lcm);
LL temp=lcm/gcd(num[ID],lcm);
if(((double)num[ID]*temp)<=R) dfs(ID+1,sum+1,num[ID]*temp);
}
int main()
{
// freopen("scoi.in","r",stdin);
scanf(AUTO AUTO,&L,&R);
init(0);
sort(num+1,num+cnt+1,cmp);
for (int i=cnt;i>0;i--) if(num[i])
for (int j=i-1;j>0;j--) if (!(num[j]%num[i]))
num[j]=0;
for (int i=1;i<=cnt;i++) if (num[i]) num[++tot]=num[i];
dfs(1,0,1);
printf(AUTO,ans);
return 0;
}