随便rand出一道题没想到这么可怕QAQ
之前没写过容斥的题
写这个题是第一次
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
1 10
【样例输入2】
1234 4321
Sample Output
【样例输出1】
2
【样例输出2】
809
2
【样例输出2】
809
HINT
【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long L,R;
long long lucky[10001]={0,6,8};
int head=1,tail=2;
long long t[10001];
bool flag[10001];
int num;
long long ans;
long long gcd(long long a,long long b)
{
if (b) return gcd(b,a%b);
else return a;
}
void dfs(int now,bool bo,long long x)
{
if (now>num)
{
if (x!=1)
{
if (bo) ans+=R/x-(L-1)/x;
else ans-=R/x-(L-1)/x;
}
return;
}
dfs(now+1,bo,x);
long long temp=x/gcd(lucky[now],x);
double lcm=(double)(temp)*lucky[now];
if (lcm<=R)
dfs(now+1,!bo,temp*lucky[now]);
}
int main()
{
cin>>L>>R;
while (lucky[tail]<R)
{
lucky[++tail]=lucky[head]*10+6;
lucky[++tail]=lucky[head]*10+8;
head++;
}//先把lucky number搞出来
while (lucky[tail]>R) tail--;
for (int i=1;i<=tail;i++)
if (!flag[i])
{
t[++num]=lucky[i];
for (int j=i+1;j<=tail;j++)
if (lucky[j]%lucky[i]==0)
flag[j]=1;
}
for (int i=1;i<=num;i++) lucky[num-i+1]=t[i];//听说从大到小算可以减少对并集的运算
dfs(1,0,1);
cout<<ans;
}