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] ...
随机推荐
- js获取样式的兼容写法
var currentStyle = function(element){ return element.currentStyle || document.defaultView.getCompute ...
- APIO2013 tasksauthor
喜闻乐见的提答题,这道题还是蛮有趣的 数据结构题写得心塞,来一道提答意思意思 如果喜欢这类题的话还可以去做做uoj83. 这题是给出了两个问题,一个最短路,一个无向图染色问题. Data 1 Floy ...
- Discuz! X2.5判断会员登录状态及外部调用注册登录框
Discuz! X2.5判断会员登录状态及外部调用注册登录框 有关discuz论坛会员信息,收集的一些资料: 用dedecms+discuz做了个门户加论坛形式的网站,但是dedecms顶部目前只能q ...
- ATI显卡添加自定义分辨率
run regedit, 浏览到这个键目录下 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Video 先导出一个作为备份. 查找 DALNo ...
- this.down和this.up用法
down既可以加id也可以加xtype
- DEDECMS之八 漏洞错误和疑难杂症
1.dedecms文章加粗b属性后出现strong或者b标签修改 dedecms的文章,如果设置了加粗的属性后,文章标题那会自动添加一个strong或者是b标签,如何去掉呢,方法如下: a.更改自动添 ...
- tomcat启动时报错
http://www.oschina.net/question/1162040_229925?sort=time 解决:
- C语言 memset函数盲点
#include <stdio.h> #include <stdlib.h> #include <string.h> struct packet { int len ...
- Pechkin:html -> pdf 利器
Pechkin 是GitHub上的一个开源项目,可方便将html转化成pdf文档,使用也很方便,下面是winform项目中的示例代码: using System; using System.Diagn ...
- Jenkins进阶系列之——17Jenkins升级、迁移和备份
升级Jenkins Jenkins的开发迭代非常快,每周发布一个开发版本,长期支持版每半年更新一次(ps:大版本更新).如此频繁的更新,怎么升级呢? war:下载新版的war文件,替换旧版本war文件 ...