【bzoj2393】【Cirno的完美算数教室】容斥原理的剪枝应用
(上不了p站我要死了,侵权度娘背锅)
在用容斥定理时,常常会用到dfs的形式,如果枚举完所有的情况可能会超时,其剪枝的优化很是重要。
Description
~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种数字~~
现在Cirno想知道~一个区间中~~有多少个数能被baka数整除~
但是Cirno这么天才的妖精才不屑去数啦
只能依靠聪明的你咯。
Input
一行正整数L R
( 1 < L < R < 10^10)
Output
一个正整数,代表所求的答案
Sample Input
1 100
Sample Output
58
像这种筛数的题很容易想到用容斥原理。
一个数的倍数-两个数的倍数+三个数的倍数……
我们发现,baka数一共有2^10个,但是其中有很多成倍数关系。于是就可以筛去不必要的数(倍数关系的数)。这要就可以减少很多数字了。
dfs时也有剪枝优化,如果此时的lcm已经大于上限,就可以return了。如果先枚举大的,可以减更多的枝。
排序前与排序后的对比
主要是练一下模板
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#ifdef WIN32
#define RIN "%I64d"
#else
#define RIN "%lld"
#endif
ll L,R;
ll num[1<<10],tp[1<<10],cnt1,cnt,ans;
void get_num(ll x){
if(x>R) return;
if(x*10+9<=R) tp[++cnt1]=x*10+9;//,printf("%d ",x*10+9);
get_num(x*10+9);
get_num(x*10+2);
}
ll gcd(ll a,ll b){
if(b==0) return a;
return gcd(b,a%b);
}
void dfs(int step,ll now,ll lim,int state){
if(now>lim||step>cnt) return;
ll tmp=now*num[step]/gcd(now,num[step]);
ans+=lim/tmp*state;
dfs(step+1,tmp,lim,-state);
dfs(step+1,now,lim,state);
}
ll solve(ll x){
ans=0;
dfs(1,1,x,1);
return ans;
}
bool cmp(const ll &a,const ll &b){
return a>b;
}
int main(){
scanf(RIN,&L);
scanf(RIN,&R);
tp[++cnt1]=2;
get_num(0);
for(int i=1;i<=cnt1;i++){
bool bj=0;
for(int j=1;j<=cnt;j++)
if(tp[i]%num[j]==0){
bj=1;break;
}
if(!bj) num[++cnt]=tp[i];
}
sort(num+1,num+cnt+1,cmp);
ll ansl=solve(L-1);
ll ansr=solve(R);
printf(RIN"\n",ansr-ansl);
return 0;
}
【bzoj2393】【Cirno的完美算数教室】容斥原理的剪枝应用的更多相关文章
- [bzoj 2393] Cirno的完美算数教室 (容斥原理+dfs剪枝)
题目描述 CirnoCirnoCirno发现了一种bakabakabaka数,这种数呢只含有222和999两种数字 现在CirnoCirnoCirno想知道[L,R][L,R][L,R]中有多少个数能 ...
- 【BZOJ-1853&2393】幸运数字&Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1817 Solved: 665[Submit][Status] ...
- Bzoj 2393: Cirno的完美算数教室 容斥原理,深搜
2393: Cirno的完美算数教室 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 287 Solved: 175[Submit][Status][ ...
- BZOJ2393: Cirno的完美算数教室
2393: Cirno的完美算数教室 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 83[Submit][Status] D ...
- [BZOJ2393] Cirno的完美算数教室(dfs+容斥原理)
传送门 先通过dfs预处理出来所有只有2和9的数,也就大概2000多个. 想在[L,R]中找到是这些数的倍数的数,可以通过容斥原理 那么如果a % b == 0,那么便可以把 a 去掉,因为 b 的倍 ...
- 【bzoj2393】Cirno的完美算数教室 数论容斥
Description ~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种数字~~ 现在Cirno想知道~一个区间中~~有多少个数能被baka数整除~ 但是Cirno这么天才的妖精才不屑去数 ...
- bzoj 2393 Cirno的完美算数教室(容斥原理+搜索)
[题意] 定义C数为只包含数字2和9的数,求[L,R]内能被C数整除的个数. [思路] Dfs预处理出C数,并去除其中倍数的情况. Dfs搜索出现情况,奇数加,偶数减,当数值大于R时剪枝. [代码] ...
- BZOJ1853:[SCOI2010]幸运数字 & BZOJ2393:Cirno的完美算数教室——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1853 https://www.lydsy.com/JudgeOnline/problem.php? ...
- bzoj 1853: [Scoi2010]幸运数字&&2393: Cirno的完美算数教室【容斥原理】
翻了一些blog,只有我用状压预处理嘛2333,.把二进制位的0当成6,1当成8就行啦.(2393是2和9 然后\( dfs \)容斥,加上一个数的\( lcm \),减去两个数的\( lcm \), ...
随机推荐
- CSU-1170 A Simple Problem
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1170 题目 Description 在一个由N个整数组成的数列中,最 ...
- hdu 1714 RedField
RedField Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 最小生成树 $Kruskal$ 算法
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int h[maxn], v[maxn], ...
- intellij idea 2017 工具使用问题
1.打开idea 打开maven项目报错:Unable to import maven project 2.在idea中Help->Show Log in Explorer->idea.l ...
- Hexo添加字数统计、阅读时长
统计插件 配置 NexT 主题默认已经集成了文章[字数统计].[阅读时长]统计功能,如果我们需要使用,只需要在主题配置文件 _config.yml 中打开 wordcount 统计功能即可.如下所示: ...
- 【Python】- pytharm 中import时无法识别自己写的程序
右键点击自己的工作空间,找下面的Mark Directory as(将目录标记为) 选择Source Root,就可以解决上面的问题了,如图
- [codeforces934D]A Determined Cleanup
[codeforces934D]A Determined Cleanup 试题描述 In order to put away old things and welcome a fresh new ye ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 新blog开张!
以后不出意外就只在新的blog更新了! hexo搭建的新blog 然后的然后是继续奋斗!
- Angular(一)
一.Angular优点: 1.MVC:职责清晰,代码模块化. 2.模块化 3.指定系统——directive:我们已经在模板中看到了一些新的属性,这些属性不属于HTML规范.例如:我们引入了双花括号用 ...