BZOJ 1853: [Scoi2010]幸运数字(容斥原理)
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]幸运数字(容斥原理)的更多相关文章
- 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: 2117 Solved: 779[Submit][Status] ...
- bzoj 1853: [Scoi2010]幸运数字 容斥
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1170 Solved: 406[Submit][Status] ...
- 1853: [Scoi2010]幸运数字[容斥原理]
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 2405 Solved: 887[Submit][Status] ...
- 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 在中国 ...
- BZOJ2393 & 1853 [Scoi2010]幸运数字 【搜索 + 容斥】
题目 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是" ...
- BZOJ1853 [Scoi2010]幸运数字 容斥原理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1853 题意概括 求一个区间范围内,近似幸运数字的个数. 定义: 幸运数字:仅由6或者8组成的数字. ...
随机推荐
- 二、微信小游戏开发 多线程Worker
微信多线程Worker教程 微信多线程Worker API 一.创建Worker,并和当前线程通讯 多线程worker只能创建1个.能和当前线程互传数据. 创建worker 在微信开发者工具中,在当前 ...
- Egret P2 入门学习资料
1 p2库下载: https://github.com/egret-labs/egret-game-library/tree/rc/4.1.0 2 p2 作者demo:https://github.c ...
- 在ScrollView嵌套GridView,使GridView不滚动
<ScrollView> …… <LinearLayout> </LinearLayout> ……</ScrollView> ...
- T-SQL数据库备份
/*1.--得到数据库的文件目录 @dbname 指定要取得目录的数据库名 如果指定的数据不存在,返回安装SQL时设置的默认数据目录 如果指定NULL,则返回默认的SQL备份目录名 */ /*--调用 ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- 利用burpsuite实现重放攻击
1.什么是重放攻击? 顾名思义,重复的会话请求就是重放攻击.可能是因为用户重复发起请求,也可能是因为请求被攻击者获取,然后重新发给服务器. 附上详细的解释:http://blog.csdn.net/k ...
- python高级之Flask框架
目录: Flask基本使用 Flask配置文件 Flask路由系统 Flask模版 Flask请求与响应 Flask之Session Flask之蓝图 Flask之message 中间件 Flask插 ...
- Openstack(十七)部署快存储cinder
官方部署文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/cinder.html OpenStack的存储组件—Cinder和Sw ...
- 【PGM】Representation--Knowledge Engineering,不同的模型表示,变量的类型,structure & parameters
Part 1. 重要的区别: Template based vs. specific Directed vs. undirected Generative vs. discrimina ...
- C#字母转换成数字/数字转换成字母 - ASCII码转换
字母转换成数字 byte[] array = new byte[1]; //定义一组数组arrayarray = System.Text.Encoding.ASCII.GetBytes(strin ...