Luogu P2567 [SCOI2010]幸运数字 容斥+脑子
2024-09-02 23:07:20
双倍经验:BZOJ 2393 Cirno的完美算数教室
做法:先把$[1,r]$中所有的幸运数字筛出来,然后用这些幸运数字来筛$[l,r]$中的近似幸运号码;
剪枝:当一个幸运数字$a[i]$是另一个幸运数字$a[j]$的倍数时,那么应该把$a[i]$去掉;
贡献用容斥搞一下好了(就是dfs部分)。。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#define ll long long
#define R register ll
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
using namespace std;
inline ll g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} ll q[],l,r,ans; bool vis[]; int tot;
inline void PRE(ll x) {if(x>r) return ; if(x) q[++tot]=x; PRE(x*+); PRE(x*+);}
inline ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
inline bool cmp(const ll& a,const ll& b) {return a>b;}
inline void dfs(int x,int d,ll L) {
if(x==tot) return ; ++x; dfs(x,d,L); d^=;
if(vis[x]) return ; L=L*q[x]/gcd(L,q[x]);
if(L<=||L>r) return ; if(d) ans+=r/L-(l-)/L;
else ans-=r/L-(l-)/L; dfs(x,d,L);
}
signed main() {
l=g(),r=g(); PRE(); sort(q+,q+tot+,cmp);
for(R i=;i<=tot;++i) for(R j=i+;j<=tot;++j) if(q[i]%q[j]==) {vis[i]=; break; }
dfs(,,); printf("%lld\n",ans);
}
2019.06.04别颓。
最新文章
- Oracle update和order by
- SQL Server 数据缓存
- 【译】Java中的对象序列化
- UWP开发入门(十四)—— UserControl中Adaptive UI的小技巧
- Button控件常用api
- WCF学习
- [备忘]Visio中连接线交叉时跨线小弯的去掉方法
- Maven依赖(转)
- 黑马程序员——有关protocol的小结
- asp:保留两位小数:
- CSS 设计彻底研究(四)盒子的浮动与定位
- sublime3下载安装及常用插件
- [CF337D]邪恶古籍-树状dp
- .Net Core配置文件介绍
- .net core Kestrel宿主服务器自定义监听端口配置
- nmap的使用
- python base64加解密
- 20165228 2017-2018-2 《Java程序设计》第4周学习总结
- c#day05
- HTTP响应状态码