http://www.lydsy.com/JudgeOnline/problem.php?id=1853

题意:

在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种“近似幸运号码”。lxhgww规定,凡是“幸运号码”的倍数都是“近似幸运号码”,当然,任何的“幸运号码”也都是“近似幸运号码”,比如12,16,666都是“近似幸运号码”。 现在lxhgww想知道在一段闭区间[a, b]内,“近似幸运号码”的个数。

思路:

首先要明白一点的是,在N范围内找a或b的倍数的个数为:

那么推广到这题的话也是差不多的,也就是容斥原理的运用。

首先可以先找出所有带6、8的数,可以优化一下删去有倍数关系的数,然后就是从中找lcm,也就是容斥原理的奇加偶减。在寻找的过程中,优先找值大的,这样一来剪枝会剪得更多一些。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + ; int n;
ll l, r;
ll cnt;
ll ans;
ll a[maxn];
ll b[maxn];
int vis[maxn]; void init(ll x)
{
if(x>r) return;
if(x!=) a[cnt++]=x;
init(x*+);
init(x*+);
} ll gcd(ll a, ll b)
{
return b==?a:gcd(b,a%b);
} void dfs(int cur, int cnt, ll val)
{
if(cur>=n)
{
if(!cnt) return;
if(cnt&) ans+=(r/val-(l-)/val);
else ans-=(r/val-(l-)/val);
return;
}
dfs(cur+,cnt,val);
ll tmp=val/(gcd(a[cur],val));
if((double)tmp*a[cur]<=r)
{
dfs(cur+,cnt+,tmp*a[cur]);
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%lld%lld",&l,&r))
{
cnt=;
init(); n=;
sort(a,a+cnt);
memset(vis,,sizeof(vis));
for(int i=;i<cnt;i++)
{
if(!vis[i])
{
b[n++]=a[i];
for(int j=i+;j<cnt;j++)
{
if(a[j]%a[i]==)
vis[j]=;
}
}
}
for(int i=;i<n;i++)
a[i]=b[n-i-]; ans=;
dfs(,,);
printf("%lld\n",ans);
}
return ;
}

BZOJ 1853: [Scoi2010]幸运数字(容斥原理)的更多相关文章

  1. Bzoj 1853: [Scoi2010]幸运数字 容斥原理,深搜

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1774  Solved: 644[Submit][Status] ...

  2. BZOJ 1853: [Scoi2010]幸运数字

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2117  Solved: 779[Submit][Status] ...

  3. bzoj 1853: [Scoi2010]幸运数字 容斥

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1170  Solved: 406[Submit][Status] ...

  4. 1853: [Scoi2010]幸运数字[容斥原理]

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2405  Solved: 887[Submit][Status] ...

  5. bzoj 1853: [Scoi2010]幸运数字&&2393: Cirno的完美算数教室【容斥原理】

    翻了一些blog,只有我用状压预处理嘛2333,.把二进制位的0当成6,1当成8就行啦.(2393是2和9 然后\( dfs \)容斥,加上一个数的\( lcm \),减去两个数的\( lcm \), ...

  6. ●BZOJ 1853 [Scoi2010]幸运数字

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题解: 容斥原理,暴力搜索,剪枝(这剪枝剪得真玄学) 首先容易发现,幸运号码不超过 2 ...

  7. 【BZOJ 1853】 1853: [Scoi2010]幸运数字 (容斥原理)

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2472  Solved: 911 Description 在中国 ...

  8. BZOJ2393 & 1853 [Scoi2010]幸运数字 【搜索 + 容斥】

    题目 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是" ...

  9. BZOJ1853 [Scoi2010]幸运数字 容斥原理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1853 题意概括 求一个区间范围内,近似幸运数字的个数. 定义: 幸运数字:仅由6或者8组成的数字. ...

随机推荐

  1. navicat 激活流程

    Navicat Premium 12激活 我自己测试了一下可以激活,很好用 原作链接:https://blog.csdn.net/loveer0/article/details/82016644 Na ...

  2. centos7上安装iptables

    centos7上安装iptables的步骤 注意:CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #安装iptables ...

  3. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  4. 【Android】Android软件开发之ListView 详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xys289187120.blog.51cto.com/3361352/65717 ...

  5. Nexus私有仓库简介

    1.    Nexus中的仓库 1.1  类型介绍 登陆Nexus,在左边菜单栏里选择Repositories,然后会出现右边的画面,右边上半部分是列出来的repository,黑体字是类型为grou ...

  6. 使用jquery的$.post()时浏览器崩溃

    代码: function verifyStepOne() { var phoneNumber = $("#phoneNumber"); var username = $(" ...

  7. Mercurial

    Contributing Changes http://nginx.org/en/docs/contributing_changes.html Mercurial is used to store s ...

  8. D. Little Artem and Dance---cf669D(模拟)

    题目链接:http://codeforces.com/problemset/problem/669/D 给你n个数,一开始是1 2 3 4 5 6 ... n 这样的 现在有两个操作,第一个操作是所有 ...

  9. 奔小康赚大钱---hdu2255(最大带权匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 带权匹配问题的模板: 运用KM算法: #include<stdio.h> #incl ...

  10. Redundant Paths---poj3177(双连通分量)

    题目链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使 ...