1853: [Scoi2010]幸运数字
Time Limit: 2 Sec Memory Limit: 64 MB Submit: 2237 Solved: 813
[Submit][Status][Discuss] 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 SourceDay1
暴力跑出所有<=r的“幸运号码“,然后容斥算出区间内“近似幸运号码“个数。
容斥部分用dfs实现,遵循奇加偶减的原则。
生成幸运号码时如果有Ai为Aj的倍数,那么Ai可以删掉,因为容斥时Ai的倍数一定会被Aj筛选出来,这样做可以加快dfs的速度。
容斥资料:http://www.cppblog.com/vici/archive/2011/09/05/155103.html
/* bakapiano 17.1.16
BZOJ 1853
数论 容斥原理 */
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#define MAXN 100005
#define INF (1<<30)
#define LL long long
#define ls ch[o][0]
#define rs ch[o][1]
#define key ch[ch[root][1]][0]
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define fv 16843009
using namespace std;
inline LL read()
{
LL f=1,t=0;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){t=t*10+ch-'0',ch=getchar();}
return t*f;
}
inline void swap(LL &a,LL &b){LL t=a;a=b;b=t;}
LL ans;
LL len,l,r,n;
LL head=0,a[MAXN],b[MAXN];
bool vis[MAXN];
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
void init(LL t)
{
if(t>r) return;
if(t) a[++head]=t;
init(t*10+6);
init(t*10+8);
}
void dfs(LL x,LL y,LL t)
{
if(x>n)
{
if(!y) return;
if(y&1) ans+=r/t-(l-1)/t;
else ans-=r/t-(l-1)/t;
return;
}
dfs(x+1,y,t);
LL temp=t/gcd(a[x],t);
if((double)temp*a[x]<=r)
dfs(x+1,y+1,temp*a[x]);
}
int main()
{
scanf("%lld%lld",&l,&r);
init(0);
sort(a+1,a+1+head);
for(int i=1;i<=head;i++)
{
if(!vis[i])
{
b[++n]=a[i];
for(LL j=i+1;j<=head;j++)
if(a[j]%a[i]==0) vis[j]=1;
}
}
for(int i=1;i<=n;i++)
a[n-i+1]=b[i];
/*for(int i=1;i<=n;i++)
printf("%lld ",b[i]);*/
dfs(1,0,1);
printf("%lld",ans);
return 0;
}