Cirno的完美算数教室(容斥)

【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


#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define N 100000
using namespace std;
typedef long long LL;
int a[N];
int b[N];
bool vis[100];
int l,r;
int n,t;
int m;

LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}

void dfs(int x,int y)           //全排列
{
    if(x>t||y>r) return;
    dfs(x+1,10*y+2);
    a[n++]=y;
    dfs(x+1,10*y+9);
    a[n++]=y;

}

int solve()       //容斥
{
    int sum=0;
    for(int i=1;i<(1<<m);i++)
    {
        LL mul=1;
        int bits=0;
        for(int j=0;j<m;j++)
        {
            if((1<<j)&i)
            {
                bits++;
                mul=mul*b[j]/gcd(mul,b[j]);
            }
        }
        cout<<"%%%"<<mul<<endl;
        if(bits&1)
            sum+=r/mul+(l-1)/mul;
        else
            sum-=r/mul+(l-1)/mul;
    }

    return sum;
}

int main()
{
    while(cin>>l>>r)
    {
        memset(vis,false,sizeof vis);
        t=int(log(r)/log(10))+1;
        cout<<"--"<<t<<endl;
        n=0;
        m=0;
        //int m=0;
        dfs(0,0);



        for(int i=0;i<n;i++)                     //筛法
            if(!vis[i])
            {
                b[m++]=a[i];
                for(int j=i+1;j<n;j++)
                {
                    //c//out<<j<<endl;
                    //system("pause");
                    if(!(a[j]%a[i]))
                        vis[j]=1;

                }
            }
        for(int i=0;i<m;i++)
            cout<<"&"<<b[i]<<endl;
        cout<<solve()<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值