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] ...
随机推荐
- offsetParent详解
offsetParent与parentNode一样,都是获取父节点,但是offsetParent却有很大的不同之处: offsetParent找有定位的父节点,没有定位默认是body,ie7以下定位在 ...
- linux下sendmail邮件系统安装操作记录
电子邮件系统的组成:1)邮件用户代理(Mail User Agent , MUA),MUA是一个邮件系统的客户端程序,它提供了阅读,发送和接受电子邮件的用户接口. 最常用的 MUA 有: linux ...
- python设计模式1:导言
<设计模式>一书总结了23个模式,依据各自的目的又被分为创建型模式(creational pattern).结构型模式(structural pattern)和行为型模式(behavior ...
- 未能正确加载包“Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage
本文出处:http://blog.sina.com.cn/s/blog_6fe3efa301016i64.html vs 2005 ,vs 2008, vs 2010,安装后有时出现这个错误(我的机器 ...
- git 本地分支与远程分支
github上已经有master分支 和dev分支 在本地 git checkout -b dev 新建并切换到本地dev分支 git pull origin dev 本地分支与远程分支相关联 在本地 ...
- distributed caching for .net applications
distributed caching for .net applications fast, scalable distributed caching with meaningful perform ...
- [转]终于找到全annotation配置springMVC的方法了(事务不失效)
原文:http://icanfly.iteye.com/blog/778401 icanfly 写道 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将servic ...
- web一周
学习web有一周了,老师进度有点小快,但是我还是感觉挺不错的. 对于一开始什么都不认识到能看懂是什么意思,并且可以写一些内容,我感觉还是比较欣慰,老师还是比较负责的,我每次都去找更远的代码学习 ...
- YII框架概念与安装
Yii概念: YII安装: 下载最版本http://www.framework.com 解压至访问目录下 直接打开advanced/init.bat文件输入0之后输入yes 打不开 ...
- Canvas之蛋疼的正方体绘制体验
事情的起因 之前写了篇谈谈文字图片粒子化 I,并且写了个简单的demo -> 粒子化.正当我在为写 谈谈文字图片粒子化II 准备demo时,突然想到能不能用正方体代替demo中的球体粒子.我不禁 ...