BZOJ 1853 [Scoi2010]幸运数字 (容斥原理)

1853: [Scoi2010]幸运数字

Time Limit: 2 Sec   Memory Limit: 64 MB
Submit: 1591   Solved: 568
[ 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


可以利用容斥原理,加上奇数个集合的并,减去偶数个集合的并

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#define ll long long 
using namespace std;
const int MAXN = 10000 + 10;
int m, n;
bool vis[MAXN];
ll a[MAXN], b[MAXN], ans, l, r;
void pre(int x, ll y)
{
    if(y > r) return ;
    if(x > 0) a[++m] = y;
    pre(x + 1, y * 10 + 6);
    pre(x + 1, y * 10 + 8);
}
ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
void dfs(int x, int y, ll z)
{
    if(x > n)
    {
        if(y & 1) ans += r / z - (l - 1) / z;
        else if(y) ans -= r / z - (l - 1) / z;
        return ;
    }
    dfs(x + 1, y, z);
    ll rs = z / gcd(a[x], z);
    if(((double)a[x] * rs) <= r) dfs(x + 1, y + 1, a[x] * rs);
}
int main()
{
    scanf("%lld%lld", &l, &r);
    pre(0, 0);sort(a + 1, a + 1 + m);
    for(int i=1;i<=m;i++)
    {
        if(!vis[i])
        {
            b[++n] = a[i];
            for(int j=i+1;j<=m;j++) if(!(a[j] % a[i]))
            vis[j] = true;
        }
    }
    for(int i=1;i<=n;i++) a[n-i+1] = b[i];
    dfs(1, 0, 1);
    printf("%lld\n", ans);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值