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] ...
随机推荐
- IIS Enabling HTTP Keep-Alives
IIS 6.0 from:https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/ea116535-8e ...
- Python的高级特性5:谈谈python的动态属性
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性. 看下面一种常见的get/set操作 In [174]: class ...
- HTTP 错误 500.22 - Internal Server Error
HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 最可能的原因: 此应用程序在 system.web/http ...
- C#获取文件MD5字符串
备注 哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才 ...
- hadoop: hive 1.2.0 在mac机上的安装与配置
环境:mac OS X Yosemite + hadoop 2.6.0 + hive 1.2.0 + jdk 1.7.0_79 前提:hadoop必须先安装,且处于运行状态(伪分式模式或全分布模式均可 ...
- 如何用 Nodejs 分析一个简单页面
本文目的 在浏览器地址栏中输入 localhost:3000,在页面显示 博客园首页 的 20 篇文章标题. 过程分析 首先需要端口的监听,这就需要引入 Node 中最重要的模块之一 express. ...
- jsp实现一条横线中间有字的样式
实现样式: ---------------------------------------------------- xxxxxx ---------------------------------- ...
- 一例完整的websocket实现群聊demo
前言 业余我都会花一些时间在tcp.http和websocket等领域的学习,现在觉得有点收获,所以把一个基于websocket的群聊功能的例子提供给大家玩玩.当然这是一个很完整的例子,包括webso ...
- js单选和复选框
http://blog.csdn.net/chelen_jak/article/details/44827393 http://www.gbtags.com/technology/jquerynews ...
- 51单片机中断interrupt……using……
51单片机中断细节的一些问题. interrupt0:外部中断0interrupt1:定时器中断0interrupt2:外部中断interrupt3:定时器中断1interrupt4:串口 using ...