这道题其实跟幸运数字一模一样,再写一遍,但是还是不知道为什么时间复杂度是有保证的?!!
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
long long l,r;
long long a[20010];
bool tag[20010];
long long ans;
int tot;
void dfs1(long long x)
{
if (x>r) return;
if (x) a[++tot]=x;
dfs1(x*10+2);dfs1(x*10+9);
}
bool cmp(long long a,long long b)
{
return a>b;
}
long long gcd(long long a,long long b)
{
if (b==0) return a;
else return gcd(b,a%b);
}
void dfs2(int i,long long lcm,int dep)
{
if (i>tot) return;
dfs2(i+1,lcm,dep);
dep++;
if (tag[i]) return;
lcm=lcm*a[i]/gcd(lcm,a[i]);
if (lcm<=0 || lcm>r) return;
if (dep&1) ans+=r/lcm-(l-1)/lcm;
else ans-=r/lcm-(l-1)/lcm;
dfs2(i+1,lcm,dep);
}
int main()
{
scanf("%lld%lld",&l,&r);
dfs1(0);
sort(a+1,a+tot+1,cmp);
for (int i=1;i<=tot;i++)
for (int j=i+1;j<=tot;j++)
if (i%j==0) tag[i]=1;
dfs2(1,1,0);
printf("%lld\n",ans);
return 0;
}