Description
~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种数字~~
现在Cirno想知道~一个区间中~~有多少个数能被baka数整除~
但是Cirno这么天才的妖精才不屑去数啦
只能依靠聪明的你咯。
Input
一行正整数L R
( 1 < L < R < 10^10)
Output
一个正整数,代表所求的答案
Sample Input
1 100
Sample Output
58
HINT
Source
和SCOI2010幸运数字一样
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long L,R;
long long lucky[10001]={0,2,9};
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+2,lucky[++tail]=lucky[head]*10+9,head++;
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;
}