【LG2567】[SCOI2010]幸运数字

题面

洛谷

题目大意:

问你区间\([L,R](1\leq L\leq R\leq 10^{10})\)中有几个数是仅由\(6,8\)组成的数的倍数。

题解

首先考虑容斥。

但是这种数字去掉有倍数关系的数还有\(943\)个,还是无法直接容斥。

这时候可以借鉴一下\(meet\;in\;the\;middle\)的方式进行处理。

发现去掉前\(20\)个数后,我们剩下的数的倍数加起来只有\(10^6\)级别了,那么我们对于前面\(20\)个数的情况,我们直接容斥解决,后面的数字我们可以全部枚举出来然后排序去重。

然后这样的话还有一个小问题,就是前后合并时可能会算重,这样的话直接枚举后面每个数然后判断一下是否是前面的数的倍数即可。

复杂度经过分析其实还是可以接受的,就是常数有点大,你可以考虑开个\(O2\)或者\(20\rightarrow 19\)再用哈希表去重。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int tot;
unsigned long long L, R, t[3000000];
bool vis[3000];
vector<unsigned long long> num;
unsigned long long fac[1000];
int siz;
void dfs(int x, unsigned long long res) {
if (res <= R && res) num.push_back(res);
if (x == tot + 1) return ;
dfs(x + 1, res * 10 + 6);
dfs(x + 1, res * 10 + 8);
}
long long solve() {
long long ans = 0;
for (register int s = 1, l = min(siz, 19); s < 1 << l; s++) {
unsigned long long lcm = 0; int tt = 0;
for (register int i = 0; i < l; i++)
if (s >> i & 1) {
if (lcm) lcm = lcm * fac[i] / __gcd(fac[i], lcm);
else lcm = fac[i];
++tt;
}
ans += (tt & 1 ? 1 : -1) * (R / lcm - (L - 1) / lcm);
}
if (siz <= 19) return ans;
tot = 0;
for (int i = 19; i < siz; i++)
for (unsigned long long j = R / fac[i] * fac[i]; j >= L; j -= fac[i])
t[++tot] = j;
sort(&t[1], &t[tot + 1]);
tot = unique(&t[1], &t[tot + 1]) - t - 1;
for (int i = 1; i <= tot; i++) {
bool flag = 1;
for (int j = 0; j < 19 && flag; j++)
if (t[i] % fac[j] == 0) flag = 0;
ans += flag;
}
return ans;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
cin >> L >> R;
long long tmp = R;
while (tmp) ++tot, tmp /= 10;
dfs(1, 0);
sort(num.begin(), num.end());
for (int i = 0; i < (int)num.size(); i++) {
if (vis[i]) continue;
fac[siz++] = num[i];
for (int j = i + 1; j < (int)num.size(); j++)
if (num[j] % num[i] == 0) vis[j] = 1;
}
printf("%lld\n", solve());
return 0;
}

【LG2567】[SCOI2010]幸运数字的更多相关文章

  1. BZOJ 1853: [Scoi2010]幸运数字

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2117  Solved: 779[Submit][Status] ...

  2. Bzoj 1853: [Scoi2010]幸运数字 容斥原理,深搜

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1774  Solved: 644[Submit][Status] ...

  3. bzoj 1853: [Scoi2010]幸运数字 容斥

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1170  Solved: 406[Submit][Status] ...

  4. bzoj1853[Scoi2010]幸运数字 容斥

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 3027  Solved: 1128[Submit][Status ...

  5. BZOJ_2393_Cirno的完美算数教室&&BZOJ_1853_[Scoi2010]幸运数字 _深搜+容斥原理

    BZOJ_2393_Cirno的完美算数教室&&BZOJ_1853_[Scoi2010]幸运数字 _深搜+容斥原理 题意: ~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种 ...

  6. 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2472  Solved: 911 Description 在中国 ...

  7. 1853: [Scoi2010]幸运数字[容斥原理]

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2405  Solved: 887[Submit][Status] ...

  8. BZOJ1853 Scoi2010 幸运数字 【枚举+容斥】

    BZOJ1853 Scoi2010 幸运数字 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号 ...

  9. [BZOJ1853][Scoi2010]幸运数字 容斥+搜索剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 3202  Solved: 1198[Submit][Status ...

随机推荐

  1. pandas的使用(4)

    pandas的使用(4)--文件读取和保存

  2. SpringCloud入门概述

    SpringCloud入门概述 Spring的三大模块:SpringBoot(构建),Spring Cloud(协调),Spring Cloud Data Flow(连接)注意:Spring Boot ...

  3. .tar.gz 文件和 .tar.xz 文件的区别

    tar.gz and tar.xz both are compressed tar-files, but with different compression methods. tar.gz is c ...

  4. [转帖]SQL Server 10分钟理解游标

    SQL Server 10分钟理解游标 https://www.cnblogs.com/VicLiu/p/11671776.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合 ...

  5. nodejs anywhere 搭建本地静态文件服务

    一.背景 工作中有时候往往会遇到下述场景:例如需要将新打好的安装包等文件临时性的给到同事,可能还需要给到多个同事.这时,我们往往有如下几种方案: 1,一般都会有公司内部的文件系统,上传文件后将对应的地 ...

  6. logstash 对配置文件conf敏感信息,密码等加密

    logstash的配置文件conf经常会涉及敏感信息,比如ES,mysql的账户密码等,以下使用logstash导入mysql为例子,加密隐藏mysql的密码. 在向keystore中添加key及其s ...

  7. axios安装及使用

    使用npm安装 $ npm install axios 使用 bower安装 $ bower install axios 使用 cdn: <script src="https://un ...

  8. C# 文件操作总结

    一.需求分析 1.将信息记录到本地记事本中. 2.将记录的信息读取出来. 3.计算出某个文件夹下所有后缀名为txt的数量和dll的数量. 4.从网络上下载文件. 二.二话不说上代码 using Sys ...

  9. 来看一下Java中“-”与equeals的区别

    简介: == ==是比较两个变量的值,如果是基本数据类型,那么就是比较的基本数据的大小值 情况一 int a=1; int b=1; System.out.println(a==b); 以上图中:== ...

  10. 彻底抛弃 jQuery ,不然还留着过年?

    我以前很喜欢 jQuery,而且说实话,我是先学jQuery,再学 JavaScript 的.所以我写这篇文章有点像是在背叛 jQuery. 我知道,关于为什么不应该用 jQuery 的文章已经汗牛充 ...