随便rand出一道题没想到这么可怕QAQ
之前没写过容斥的题
写这个题是第一次
【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000
#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;
}原文:http://blog.csdn.net/creationaugust/article/details/43601517