【BZOJ2393】Cirno的完美算数教室

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值