BZOJ 1853: [Scoi2010]幸运数字
1853: [Scoi2010]幸运数字
Time Limit: 2 Sec Memory Limit: 64 MB
Submit: 2117 Solved: 779
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 10
【样例输入2】
1234 4321
Sample Output
2
【样例输出2】
809
HINT
【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000
Source
分析
不难想到用容斥原理来统计。
先预处理出所有小于1e10的幸运数字,并不是很多。但是发现枚举所有的组合还是会爆炸的,需要一些剪枝。
1. 对于两个幸运数字,x<y,如果有y为x的倍数,则y可以忽略,因为x可以完全覆盖y的倍数。
2. 对于一种组合,如果目前的积已经大于N,即再进行下去得到的都是加减0的无意义操作,可以直接跳出。
3. 可以把GCD函数写成非递归的形式,但貌似没多大用,跑出来的结果差距不是很大,也许是我写得不好。
4. 枚举的时候从大往小枚举,据说有奇效,因为懒癌晚期,我并没有对比验证。
代码
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = ; const LL lim = 10000000000LL; template <class T>
__inline void read(T &x)
{
x = ; char c = getchar(); while (c < '')
c = getchar(); while (c >= '')
{
x = x* + c - '';
c = getchar();
}
} LL gcd(LL a, LL b)
{
if (a < b)
{
a ^= b;
b ^= a;
a ^= b;
}
while (b)
{
a %= b;
a ^= b;
b ^= a;
a ^= b;
}
return a;
} LL num[N]; int tot = ; __inline void prework(void)
{
int t, tail = ; for (t = ; num[t] <= lim; ++t)
{
num[++tail] = num[t] * + ;
num[++tail] = num[t] * + ;
} for (int i = ; i <= t; ++i)
{
bool flag = true;
for (int j = ; j <= tot; ++j)
if (num[i] % num[j] == )
{ flag = false; break; }
if (flag)num[++tot] = num[i];
}
} LL answer, limit; void search(int t, bool f, LL sum)
{
if (t)
{
search(t - , f, sum); LL GCD = gcd(num[t], sum); if (sum / GCD <= limit / num[t])
{
LL LCM = sum / GCD * num[t]; if (f)
answer -= limit / LCM;
else
answer += limit / LCM; search(t - , !f, LCM);
}
}
} __inline LL count(LL n)
{
limit = n;
answer = ; int pos = ; while (pos <= tot
&& num[pos] <= n)++pos; search(pos - , , ); return answer;
} signed main(void)
{
prework(); LL a; read(a);
LL b; read(b); printf("%lld\n",
count(b)
- count(a - )
);
}
BZOJ_1853.cpp
@Author: YouSiki
BZOJ 1853: [Scoi2010]幸运数字的更多相关文章
- Bzoj 1853: [Scoi2010]幸运数字 容斥原理,深搜
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1774 Solved: 644[Submit][Status] ...
- bzoj 1853: [Scoi2010]幸运数字 容斥
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1170 Solved: 406[Submit][Status] ...
- BZOJ 1853: [Scoi2010]幸运数字(容斥原理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题意: 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运 ...
- bzoj 1853: [Scoi2010]幸运数字&&2393: Cirno的完美算数教室【容斥原理】
翻了一些blog,只有我用状压预处理嘛2333,.把二进制位的0当成6,1当成8就行啦.(2393是2和9 然后\( dfs \)容斥,加上一个数的\( lcm \),减去两个数的\( lcm \), ...
- ●BZOJ 1853 [Scoi2010]幸运数字
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题解: 容斥原理,暴力搜索,剪枝(这剪枝剪得真玄学) 首先容易发现,幸运号码不超过 2 ...
- 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2472 Solved: 911 Description 在中国 ...
- 1853: [Scoi2010]幸运数字[容斥原理]
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
- BZOJ2393 & 1853 [Scoi2010]幸运数字 【搜索 + 容斥】
题目 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是" ...
- AC日记——[SCOI2010]幸运数字 bzoj 1853
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
随机推荐
- 当元素的样式为display:none时获取他的宽高
其实这里可以用一个偷梁换柱的办法,把display:none改为 display:block;visibility:hidden;position:absolute; 在jquery的swap方法中实 ...
- 在C#中将String转换成Enum:
一: 在C#中将String转换成Enum: object Enum.Parse(System.Type enumType, string value, bool ignoreCase); 所以,我 ...
- dotnetGen 系列终于开源了
https://github.com/2881099/dotnetGen .Net 3.0 + SqlServer 生成器 https://github.com/2881099/dotnetGen_s ...
- android中按电源键锁屏然后解锁导致Activity调用onDestory以及如何防止锁屏
今天在android项目中按电源键锁屏,然后解锁,发现子Activity关闭了,回到了主页,这个问题困扰了我很久,最后打log发现,在按电源键的时候,调用了子Activity的onDestroy()方 ...
- Linux 退格键不回显
在程序使用system("stty erase ^H");可以实现在输入状态下,按退格键删除字符,不回显. 调用tcsetattr修改linux基本输入的控制字符定义 //Linu ...
- 在iframe中获取本iframe DOM引用
window.frameElement 获取本iframe DOM window.frameElement.contentDocument.getElementById('id') 获取这个ifram ...
- Entity Framework6 with Oracle(可实现code first)
Oracle 与2个月前刚提供对EF6的支持.以前只支持到EF5.EF6有很多有用的功能 值得升级.这里介绍下如何支持Oracle 一.Oracle 对.net支持的一些基础知识了解介绍. 1.早 ...
- 工作随笔——mysql子查询删除原表数据
最近在开发的时候遇到一个mysql的子查询删除原表数据的问题.在网上也看了很多方法,基本也是然并卵(不是写的太乱就是效率太慢). 公司DBA给了一个很好的解决方案,让人耳目一新. DELETE fb. ...
- 一次莽撞的行为:在phpmyadmin中修改MySQL root密码后无法操作数据库
一.手贱行为(✿◡‿◡) 在一次开发中通过xampp方式安装了PHP环境,需要操作数据库时通过phpmyadmin访问MySQL,在一次数据库操作时想起没有设置密码,于是直接在mysql数据库中的us ...
- Bootstrap系列 -- 6. 列表
一. 普通无序列表 <ul> <li>无序列表项目</li> <li>无序列表项目</li> <li>无序列表项目</li ...