当前位置: 移动技术网 > IT编程>开发语言>C/C++ > BZOJ1853: [Scoi2010]幸运数字(容斥原理)

BZOJ1853: [Scoi2010]幸运数字(容斥原理)

2018年08月28日  | 移动技术网IT编程  | 我要评论

野战蓝鲫,都市之风云法神,就业规划书

题意

询问区间$(l, r)$中有多少个数是只含$6, 8$的数的倍数

sol

思路很妙啊。

首先在$10^{10}$内只含$6, 8$的数有$\sum_{i = 1}^{10} 2^i = 2046$个。

然后去掉相同的,应该是有$943$个。

之间算不好算,考虑用容斥原理。

但是直接容斥的复杂度很显然是$2^n$的

考虑剪枝!

当前的数大于上界,肯定要return

由于题目规定的数在$\sqrt {10^{10}}$内也就十几个,因此是可以跑过的。

注意中间算lcm的时候是会爆long long的!我们需要先转成double,再判断

 

// luogu-judger-enable-o2
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#define int long long 
//#define int long long
using namespace std;
const int maxn = 1e6;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int mx = 10000000000ll;
vector<int> a;
int flag[maxn], vis[maxn], b[maxn], out, cnt;
void pre(int now) {
    if(now >= mx) return ;
    a.push_back(now);
    pre(now * 10 + 6); pre(now * 10 + 8);
}
void dfs(int x, int opt, int l, int r, int pre) {
    if(x > r) return ;
    //printf("%i64d\n", out);
    out += opt * (r / x - l / x);
    for(int i = pre + 1; i <= cnt; i++) {
        int g = __gcd(x, b[i]);
        if((double) (x / g) * b[i] > (double)r) return;
        dfs(x / g * b[i], opt * -1, l, r, i);
    }
}
int solve(int l, int r) {
    out = 0;
    for(int i = 1; i <= cnt; i++)
        dfs(b[i], 1, l, r, i);
    return out;
}
main() {
    pre(6);
    pre(8);
    //printf("%d", a.size()); 
    for(int i = 0; i < a.size(); i++) 
        for(int j = 0; j < a.size(); j++)
            if((i != j) && (a[j] > a[i]) && (a[j] % a[i] == 0)) flag[j] = 1;
    //printf("%d\n", si);
    for(int i = 0; i < a.size(); i++)
        if(!flag[i]) b[++cnt] = a[i]; 
    sort(b + 1, b + cnt + 1, greater<int>());
    //for(int i = 1; i <= cnt; i++)
    //-    printf("%d\n", cnt);
    int a = read(), b = read();
    printf("%lld", solve(a - 1, b) + 1);
    return 0;
}
/*
2 10000000000
*/

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网