幸运数字(bzoj 1853)
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
/*
容斥原理。
先求出范围内所有的幸运数字,然后筛它们的倍数,直接O(n)筛很明显会超时,直接求的话可能会一个数被统计多次,
这样就用到了容斥原理。
先考虑只有6和8的情况,答案为lim/6+lim/8-lim/lcm(6,8)。
当数字变多时,我们会发现加上的都是奇数集合的贡献,减去的都是偶数集合的贡献,dfs即可。
有一个需要注意的地方,算lcm的时候会爆long long,要用double。
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 10010
#define lon long long
using namespace std;
lon l,r,a[N],b[N],n,m,ans;
bool vis[N];
void init(lon x){
if(x>r) return;
if(x) a[++m]=x;
init(x*+);
init(x*+);
}
lon gcd(lon x,lon y){
if(!y) return x;
return gcd(y,x%y);
}
void dfs(int x,int y,lon z){
if(x>n){
if(y&) ans+=r/z-(l-)/z;
else if(y) ans-=r/z-(l-)/z;
return;
}
dfs(x+,y,z);
lon tmp=z/gcd(z,a[x]);
if((double)tmp*a[x]<=r)
dfs(x+,y+,tmp*a[x]);
}
int main(){
cin>>l>>r;
init();
sort(a+,a+m+);
for(int i=;i<=m;i++){
if(!vis[i]) b[++n]=a[i];
for(int j=i+;j<=m;j++)
if(a[j]%a[i]==) vis[j]=;
}
for(int i=;i<=n;i++) a[n-i+]=b[i];
dfs(,,);
cout<<ans;
return ;
}
幸运数字(bzoj 1853)的更多相关文章
- AC日记——[SCOI2010]幸运数字 bzoj 1853
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
- BZOJ 1853: [Scoi2010]幸运数字
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2117 Solved: 779[Submit][Status] ...
- 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】 1853: [Scoi2010]幸运数字 (容斥原理)
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2472 Solved: 911 Description 在中国 ...
- BZOJ 1853 【Scoi2010】 幸运数字
Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认 为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,8 ...
- bzoj 1853: [Scoi2010]幸运数字&&2393: Cirno的完美算数教室【容斥原理】
翻了一些blog,只有我用状压预处理嘛2333,.把二进制位的0当成6,1当成8就行啦.(2393是2和9 然后\( dfs \)容斥,加上一个数的\( lcm \),减去两个数的\( lcm \), ...
- 1853: [Scoi2010]幸运数字[容斥原理]
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
随机推荐
- C++ 学习笔记 (八)重载 重写 重定义以及名字覆盖
学习C++必定会遇到重载.重写.重定义.概念的东西多也是学习C++蛋疼之处,但是还是得弄懂,学懂了也就不觉得多了. 概念,特点: 重载: 直白点说就是函数名字相同,传参的个数,类型不一样.判断标准在于 ...
- 【NTT】loj#6261. 一个人的高三楼
去年看过t老师写这题博客:以为是道神仙题 题目大意 求一个数列的$k$次前缀和.$n\le 10^5$. 题目分析 [计数]cf223C. Partial Sums 加强版.注意到最后的式子是$f_i ...
- 三 python并发编程之多线程-理论
一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 车间负责把资源整合 ...
- C++多态实例
#include <iostream> #include <string> using namespace std; //class 实现 class Employee { s ...
- jichu
第 题 请编写函数 fun,其功能时:计算并输出当 x<0.97 时下列多项式的值,直到| sn-s(n-)|<0.000001 为止. Sn=+.5x+)/!x()+…+)()…..() ...
- 洛谷 P3740 [HAOI2014]贴海报
题目描述 Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规则如下: electo ...
- A JavaScript Image Gallery
childNodes property: The childNodes property is a way of getting information about the children of ...
- 3 ways of including JavaScript in HTML
Code written in JavaScript must be executed from a document written in HTML. There are three ways of ...
- 9 RESTful API
1 RESTful API 2 post创建一个视频 put帖子加精,delete删除 3 自定义状态码 4 5 6 7 8
- javascript 实现九九乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...