bzoj1853幸运数字——容斥原理
2024-09-04 15:16:27
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1853
dfs实现容斥原理即可。
注意:若在init中写“cnt++”,则出来后需要先cnt--再继续!!
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll l,r,a[],b[],ans,cnt,ct;
bool del[];
void init(ll x)
{
if(x>r)return;
a[cnt++]=x;
init(*x+);
init(*x+);
}
ll gcd(ll x,ll y)
{
return x%y==?y:gcd(y,x%y);
}
void dfs(ll x,ll y,ll z)
{
if(x>ct)
{
if(y&)ans+=r/z-(l-)/z;
else if(y)ans-=r/z-(l-)/z;
return;
}
dfs(x+,y,z);
ll tmp=z/gcd(a[x],z);
if((double)a[x]*tmp<=r)dfs(x+,y+,a[x]*tmp);
}
int main()
{
scanf("%lld%lld",&l,&r);
init();
cnt--;//!!!
sort(a+,a+cnt+);
for(ll i=;i<=cnt;i++)
{
if(del[i])continue;
for(ll j=i+;j<=cnt;j++)
if(a[j]%a[i]==)del[j]=;
b[++ct]=a[i];
}
for(ll i=;i<=ct;i++)//倒序!
a[i]=b[ct-i+];
dfs(,,);
printf("%lld",ans);
return ;
}
最新文章
- ssh整合问题总结--在添加商品模块实现图片(文件)的上传
- slideDoor(学习某编程网站的,仅作记录和学习)
- Caffe proto閱讀
- php性能分析工具 - xhprof的安装使用
- 使用python来调试串口
- bootstrap static popover
- spring校验和文件上传
- Linq--扩展方法
- Egret的VS环境搭配
- SQL中数据类型转换
- 子查询 此处该用AND 而不是 WHERE
- Win7+QTP10.0+IE9无法启动IE的解决方法
- [算法] kmp实现
- Windows Graphics Programming Win32 GDI and DirectDraw第六章疑问
- obj-c编程19:关联对象
- python图形用户
- Shell编程基础知识(一)
- 萨塔尼亚的期末考试(fail)
- 第三个Sprint冲刺总结
- 基音检测算法的性能:Performance Evaluation of Pitch Detection Algorithms
热门文章
- SpringMvc aop before
- java eclipse使用不同jdk版本
- 利用树的先序和后序遍历打印 os 中的目录树
- 常见 WEB 安全漏洞(转)
- 九度OJ 1016:火星A+B (进制转换)
- php验证身份证号码有效性
- When Programmers and Testers Collaborate
- No provisioned iOS devices are available with a compatible iOS version. Connect an iOS device with a
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
- ABAP alv report