跳到正文

剪贴板

剪贴板 0lirw9za

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn = 2e7 + 5;

template <typename T>
inline void read(T& x){
x = 0; int f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); }
while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); } x *= f;
}
template <typename T, typename... Args>
inline void read (T &x, Args&... Arg) { read (x), read (Arg...); }

ll lucky[maxn];
int cnt;                        //幸运数字的总个数
inline ll str_to_int(string x){
    int len = x.length();
    ll ret = 0;
    for(int i=0;i<len;i++)
        ret = ret * 10ll + (x[i] - '0');
    return ret;
}
inline void init(string now){   //预处理所有的幸运数字
    if(now.length() >= 11)
        return;
    lucky[++cnt] = str_to_int(now);
    init(now + '6');
    init(now + '8');
}
inline bool cmp(ll x,ll y){
    return x > y;
}
inline ll gcd(ll a,ll b){
    return b ? gcd(b,a % b) : a;
}
ll ans,l,r;
inline void solve(int now,int p,ll lcm){                  //solve算容斥,其中now:现在枚举的幸运数字位置,p:容斥系数,lcm:所选的数的lcm
    if(now > cnt){
        if(lcm > 1) ans += p * (r / lcm - (l - 1) / lcm);
        return;
    }
    solve(now + 1,p,lcm);               //不选now
    if(lcm / gcd(lcm,lucky[now]) <= r / lucky[now])     //选now
        solve(now + 1,-p,lcm * lucky[now] / gcd(lcm , lucky[now]));
}
int main(){

    init("6");
    init("8");
    sort(lucky + 1 ,lucky + 1 + cnt , cmp);             //将幸运数字从大到小排序,便于之后solve中lcm的增长更快,相当于是在剪枝
    read(l,r);
    solve(1,-1,1);
    printf("%lld\n",ans);

    return 0;
}